-2

Is there a way to get legends for two series when plotted using ggplot in R? May be I am missing some silly (should have known) argument in the function. I did not find the answer on the internet.

Here is the data:

df1 <- structure(list(time = structure(c(1352804400, 1352804430, 1352804460, 
1352804490, 1352804520, 1352804550, 1352804580, 1352804610, 1352804640, 
1352804670, 1352804700, 1352804730, 1352804760, 1352804790, 1352804820, 
1352804850, 1352804880, 1352804910, 1352804940, 1352804970, 1352805000, 
1352805030, 1352805060, 1352805090, 1352805120, 1352805150, 1352805180, 
1352805210, 1352805240, 1352805270, 1352805300, 1352805330, 1352805360, 
1352805390, 1352805420, 1352805450, 1352805480, 1352805510, 1352805540, 
1352805570), class = c("POSIXct", "POSIXt"), tzone = ""), VE = c(36L, 
31L, 32L, 55L, 39L, 45L, 46L, 60L, 56L, 53L, 58L, 60L, 30L, 38L, 
55L, 40L, 47L, 52L, 33L, 34L, 58L, 38L, 39L, 33L, 39L, 50L, 38L, 
32L, 32L, 41L, 44L, 35L, 48L, 51L, 59L, 35L, 51L, 56L, 39L, 35L
)), .Names = c("time", "VE"), row.names = c(NA, -40L), class = "data.frame")

df2 <- structure(list(time = structure(c(1352804400, 1352804430, 1352804460, 
1352804490, 1352804520, 1352804550, 1352804580, 1352804610, 1352804640, 
1352804670, 1352804700, 1352804730, 1352804760, 1352804790, 1352804820, 
1352804850, 1352804880, 1352804910, 1352804940, 1352804970, 1352805000, 
1352805030, 1352805060, 1352805090, 1352805120, 1352805150, 1352805180, 
1352805210, 1352805240, 1352805270), class = c("POSIXct", "POSIXt"
), tzone = ""), VE = c(47L, 45L, 45L, 40L, 42L, 40L, 48L, 48L, 
43L, 44L, 44L, 46L, 42L, 49L, 41L, 48L, 47L, 44L, 44L, 48L, 47L, 
42L, 42L, 40L, 47L, 46L, 50L, 49L, 46L, 49L)), .Names = c("time", 
"VE"), row.names = c(NA, -30L), class = "data.frame")

Here is the code:

ggplot(df1,aes(x=time, y=VE))+geom_line(color='red',size=1)+geom_line(data=df2,aes(x=time, y=VE),colour="blue",size=2)
Stat-R
  • 5,040
  • 8
  • 42
  • 68
  • 4
    you should read an introduction to ggplot2. Basically, you need to combine both datasets, and use an aesthetic mapping. – baptiste May 12 '13 at 21:27

2 Answers2

2

Specifically, implementing @baptiste's comment:

dff <- rbind(data.frame(s=factor(1),df1),
             data.frame(s=factor(2),df2))
ggplot(dff,aes(x=time, y=VE,colour=s,size=s))+
    geom_line()+
    scale_colour_manual(values=c("red","blue"))+
    scale_size_manual(values=1:2)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks. Using your answer I played around with the `scale_color_manual` function and it helped me to achieve what I wanted – Stat-R May 13 '13 at 14:39
  • OK. I'd say you should take that answer and post it as a separate/additional answer to your question rather than as part of the question ... If you're going to have two different line sizes, I think you should allow that to be incorporated in the legend (i.e. set your `scale_size_manual` parameters the same as your `scale_colour_manual` ones so that the legends get merged, rather than using `guide="none"` to suppress the legend) – Ben Bolker May 13 '13 at 15:07
2

Using @Ben's answer and comments, removing the extra-legend, and then renaming the legend title I wrote:

dff <- rbind(data.frame(s=factor(1),df1),data.frame(s=factor(2),df2))
ggplot(dff,aes(x=time, y=VE,colour=s))+geom_line()+scale_colour_manual(values=c("red","blue"),labels=c('My label-1','My label-2'),name='Legend Title')+  scale_size_manual(values=c("red","blue"))+theme(axis.title.x = element_text(face="bold", size=16),axis.title.y= element_text(face="bold",size=16),axis.text.x  = element_text(angle=0, vjust=0.5, size=14),axis.text.y  = element_text(angle=0, vjust=0.5, size=14))

and got

enter image description here

Community
  • 1
  • 1
Stat-R
  • 5,040
  • 8
  • 42
  • 68