<p>Couple of things regarding your code:</p>
<ul>
<li><p>It would be good to have your data frame in <a href="https://rpubs.com/kaz_yos/ggplot-dataformat" rel="nofollow noreferrer">long format</a> for ggplot2</p>
</li>
<li><p>For having the x-axis ordered chronologically, you have to specify it manually, otherwise it is ordered alphabetically.</p>
</li>
</ul>
<p>Then, here is my solution:</p>
<pre><code>library(ggplot2)
library(reshape2)

# I have substituted your &lt;- by = and changed the name of the groups
df2 &lt;- data.frame(Time = c(&quot;28Jan&quot;,&quot;13Feb&quot;,&quot;16Mar&quot;,&quot;1Apr&quot;,&quot;17Apr&quot;,&quot;3May&quot;,&quot;19May&quot;,&quot;4Jun&quot;,&quot;20Jun&quot;,&quot;6Jul&quot;,&quot;22Jul&quot;,&quot;7Aug&quot;,&quot;23Aug&quot;,&quot;8Sep&quot;,&quot;24Sep&quot;,&quot;10Oct&quot;,&quot;26Oct&quot;,&quot;11Nov&quot;,&quot;13Dec&quot;,&quot;29Dec&quot;),
                  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 &lt;- melt(df2)

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

# 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(&quot;NDVI&quot;) + xlab(&quot;Time&quot;)
</code></pre>
<p><a href="https://i.sstatic.net/S8SVC.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/S8SVC.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2019-07-31T08:38:00.423Z
Very cool answer and succinct code
M__
CC BY-SA 4.0
2019-07-31T14:49:31.683Z
Thank you I really appreciate your help.
Sharbanoo
CC BY-SA 4.0
2019-07-31T15:44:23.923Z
You are welcome @Sharbanoo! If the answer was useful to you please consider to accept it
jxx_fa
CC BY-SA 4.0
2019-07-31T07:34:25.177Z
2019-07-31T07:34:25.177Z
2020-06-18T08:30:41.743Z
jxx_fa
<p>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.</p>

<p>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?</p>

<pre><code>library(ggplot2)
library(tidyverse)

df2 &lt;- data.frame(Time&lt;-c("28Jan","13Feb","16Mar","1Apr","17Apr","3May","19May","4Jun","20Jun","6Jul","22Jul","7Aug","23Aug","8Sep","24Sep","10Oct","26Oct","11Nov","13Dec","29Dec"),
                  A&lt;-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&lt;-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&lt;-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&lt;-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&lt;-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&lt;-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")
</code></pre>

<p><a href="https://i.sstatic.net/A8J8C.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/A8J8C.jpg" alt="enter image description here"></a></p>

<r><ggplot2>
Multiple line chart
# Multiple line chart

(Asked by: username_0 on 2019-07-31)

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")

**Tags:** r, ggplot2

---

## 1 answers

### Answer by username_1 on 2019-07-31. Score: 3

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")

> **Comments:**
> **username_2**: Very cool answer and succinct code  
> **username_0**: Thank you I really appreciate your help.  
> **username_1**: You are welcome @Sharbanoo! If the answer was useful to you please consider to accept it  

---

