在R中使用ggplot绘制时,有没有办法获得两个系列的图例? 可能是我在函数中遗漏了一些愚蠢的(应该已知的)参数。我没有在互联网上找到答案。
以下是数据:
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")
以下是代码:
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)
答案 0 :(得分:2)
具体来说,实施@ baptiste的评论:
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)
答案 1 :(得分:2)
使用@ Ben的回答和评论,删除extra-legend,然后重命名我写的legend title:
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))
得到了