3

I would like to plot 3 columns data (x,y,z) by time in one graph, with labels of 3 column names ("x", "y", "z").

data df

I use the following code to plot, but don't know how to add labels

p <- ggplot() + 
   geom_line(aes(time, x), df, colour = "red") + 
   geom_line(aes(time, y), df, colour = "blue") + 
   geom_line(aes(time, z), df,colour = I("darkgreen")) + 
   xlab("Time") + ylab("value")

Output

4
  • 1
    It is better to convert it to long format i.e. library(tidyverse);df %>% gather(key, val, -time) %>% ggplot(aes(x = time, y = val, color = key)) + geom_line() Commented Dec 19, 2017 at 5:48
  • I used the long version before, just wonder if there are any other ways of doing it as a wide format. Your code works perfectly~! Commented Dec 19, 2017 at 5:56
  • Just like you did, you can do it with the wide format. You may have to check for geom_label for the label Commented Dec 19, 2017 at 5:58
  • Couldn't figure it out, I was trying to label the lines by color as well by geom_label(label=colnames(df[,-1]), but no change to the existing plot. Thanks for the suggestions~! Commented Dec 19, 2017 at 6:38

2 Answers 2

2

Is this what you want?

ggplot(data = df) +
  geom_line(aes(time, x, color = "X")) +
  geom_line(aes(time, y, color = "Y")) +
  geom_line(aes(time, z, color = "Z")) +
  xlab("Time") +
  ylab("value") +
  labs(color = "YOUR LEGEND TITLE")

enter image description here

although @akrun is right, converting your data to long format would be the way to go here:

library(reshape2)
df <- melt(df, id.vars = "time")

> df
   time variable value
1     1        x     1
2     2        x     2
3     3        x     3
4     4        x     4
5     1        y     2
6     2        y     4
7     3        y     6
8     4        y     8
9     1        z     1
10    2        z     3
11    3        z     5
12    4        z     7

and then

ggplot(data = df, aes(x = time, y = value, color = variable)) +
  geom_line() +
  xlab("Time") +
  ylab("value") +
  labs(color = "YOUR LEGEND TITLE")

The legend-title is optional btw:

ggplot(data = df, aes(x = time, y = value, color = variable)) +
  geom_line() +
  xlab("Time") +
  ylab("value") +
  labs(color = "")
Sign up to request clarification or add additional context in comments.

7 Comments

Exactly~! I didn't know the labs() before, does it work like combining all colors with labels in each geom_lines(..., color = "labels")?
you don't really need the labs(color = "...") command, it only changes the title of the color legend. Can also be used for linetype or shape, etc. To get the labels to show up, I put the color inside the aes of geom_line, like geom_line(aes(color= ...)). If you put it outside the aes like so geom_line(aes(), color = ...) you only change the color of the line. I hope I understood your question correctly!
Long version does make more sense~ thank you so much for helping me, and recall the melt function, seems easier than gather, which I need to figure out the key and value.
I see... so color = "X" here color is actually automatically be given, and "X" is the way to label those systems generated colors, might be a way to understand this? haha, anyway, I think I am pretty clear of all the methods here, super helpful~
Yes the color is chosen automatically, although you can change it manually; check out this link: sthda.com/english/wiki/…
|
1

You can use scale_colour_manual to show the legend. The modified code will look like:

p <- ggplot(df, aes(x,y,z)) + 
  geom_line(aes(time, x, colour = "x")) + 
 geom_line(aes(time, y, colour = "y")) + 
 geom_line(aes(time, z,colour = "z")) + 
 scale_colour_manual("", 
                      breaks = c("x", "y", "z"),
                      values = c("red", "blue", "green")) +
 xlab("Time") + ylab("value")

2 Comments

Hahaha, I was just thinking how to select the color manually rather than automatically be given the color. Tried your code, works perfectly~ Thx
@ys3006 Solution delivered beforehand. I knew that will be next thing you will be looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.