2
$\begingroup$

I am trying to create a multiple coloured line graph based on six lines. Essentially I have data that identifies different units of vegetation by the variable” NDVI (vegetation index)” I want to create a plot with differently coloured lines for each units.

The following is a screen shot of the data structure and my code. It is plotting all of the data. But the time data on X-axis is disrupted and not ordered. What am I missing? And also how do I get legend for my chart?

library(ggplot2)
library(tidyverse)

df2 <- data.frame(Time<-c("28Jan","13Feb","16Mar","1Apr","17Apr","3May","19May","4Jun","20Jun","6Jul","22Jul","7Aug","23Aug","8Sep","24Sep","10Oct","26Oct","11Nov","13Dec","29Dec"),
                  A<-c(0.209,0.225,0.237,0.312,0.377,0.468,0.499,0.492,0.45,0.43,0.405,0.405,0.378,0.358,0.337,0.297,0.179,0.305,0.241,0.183),
                  B<-c(-0.472,-0.589,-0.422,-0.479,-0.371,-0.112,-0.03,-0.134,-0.132,-0.12,-0.1,-0.227,-0.137,-0.025,-0.02,-0.011,-0.024,-0.377,-0.494,-0.49),
                  C<-c(-0.278,-0.385,-0.31,-0.3,-0.279,-0.033,0.089,0.154,0.231,0.308,0.341,0.4,0.397, 0.383,0.37,0.309,0.248, 0.271,-0.212,-0.3),
                  D<-c(-0.544,-0.604,-0.505,-0.541,-0.475,-0.173,-0.054,-0.026,0.098,0.199,0.257,0.338,0.37,0.372,0.36,0.274,0.217,-0.007,-0.552,-0.528),
                  E<-c( 0.213,0.239,0.277,0.304,0.384,0.47,0.537,0.547,0.5,0.489,0.452,0.469,0.439,0.419,0.408,0.372,0.049,0.335,0.304,0.282),
                  F<-c(0.234,0.248,0.3,0.324,0.391,0.444,0.512,0.503,0.448,0.434,0.407,0.421,0.4,0.385,0.376,0.335,0.087,0.323,0.295,0.275))
head(df2)

ggplot(df2, aes(x = Time, group = 1)) +
  geom_line(aes(y = A), colour="blue") +
  geom_line(aes(y = B), colour = "red") +
  geom_line(aes(y = C), colour = "green") +
  geom_line(aes(y = D), colour = "black") +
  geom_line(aes(y = E), colour = "yellow") +
  geom_line(aes(y = F), colour = "brown") +
  ylab(label="NDVI") + xlab("Time")

enter image description here

$\endgroup$
3
$\begingroup$

Couple of things regarding your code:

  • It would be good to have your data frame in long format for ggplot2

  • For having the x-axis ordered chronologically, you have to specify it manually, otherwise it is ordered alphabetically.

Then, here is my solution:

library(ggplot2)
library(reshape2)

# I have substituted your <- by = and changed the name of the groups
df2 <- data.frame(Time = c("28Jan","13Feb","16Mar","1Apr","17Apr","3May","19May","4Jun","20Jun","6Jul","22Jul","7Aug","23Aug","8Sep","24Sep","10Oct","26Oct","11Nov","13Dec","29Dec"),
                  g_A = c(0.209,0.225,0.237,0.312,0.377,0.468,0.499,0.492,0.45,0.43,0.405,0.405,0.378,0.358,0.337,0.297,0.179,0.305,0.241,0.183),
                  g_B = c(-0.472,-0.589,-0.422,-0.479,-0.371,-0.112,-0.03,-0.134,-0.132,-0.12,-0.1,-0.227,-0.137,-0.025,-0.02,-0.011,-0.024,-0.377,-0.494,-0.49),
                  g_C = c(-0.278,-0.385,-0.31,-0.3,-0.279,-0.033,0.089,0.154,0.231,0.308,0.341,0.4,0.397, 0.383,0.37,0.309,0.248, 0.271,-0.212,-0.3),
                  g_D = c(-0.544,-0.604,-0.505,-0.541,-0.475,-0.173,-0.054,-0.026,0.098,0.199,0.257,0.338,0.37,0.372,0.36,0.274,0.217,-0.007,-0.552,-0.528),
                  g_E = c( 0.213,0.239,0.277,0.304,0.384,0.47,0.537,0.547,0.5,0.489,0.452,0.469,0.439,0.419,0.408,0.372,0.049,0.335,0.304,0.282),
                  g_F = c(0.234,0.248,0.3,0.324,0.391,0.444,0.512,0.503,0.448,0.434,0.407,0.421,0.4,0.385,0.376,0.335,0.087,0.323,0.295,0.275))

# Convert to the proper format
df2.melted <- melt(df2)

# Order of the factor in x-axis
df2.melted$Time <- factor(df2.melted$Time, levels = c("28Jan","13Feb","16Mar","1Apr","17Apr","3May","19May","4Jun","20Jun","6Jul","22Jul","7Aug","23Aug","8Sep","24Sep","10Oct","26Oct","11Nov","13Dec","29Dec"))

# Note group = variable to connect the points of the different groups
ggplot(df2.melted, aes(x = Time, y = value, colour = variable, group = variable)) + 
  geom_line() + 
  ylab("NDVI") + xlab("Time")

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Very cool answer and succinct code $\endgroup$ – Michael G. Jul 31 at 8:38
  • 1
    $\begingroup$ Thank you I really appreciate your help. $\endgroup$ – Sharbanoo Jul 31 at 14:49
  • 1
    $\begingroup$ You are welcome @Sharbanoo! If the answer was useful to you please consider to accept it $\endgroup$ – plat Jul 31 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.