ggplot2 @ R中两个不同大小系列的图例
当在R中使用ggplot进行绘图时,有两种方法可以获得两个系列的图例吗? 可能是我在函数中缺少一些愚蠢的(应该已知的)参数。我在网上找不到答案。ggplot2 @ R中两个不同大小系列的图例
下面是数据:
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)
具体实施@巴蒂斯特的评论:
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)
谢谢。使用你的答案,我使用'scale_color_manual'函数玩弄了它,它帮助我实现了我想要的 – 2013-05-13 14:39:27
OK。我会说你应该把这个答案作为一个单独的/额外的答案来回答你的问题,而不是作为问题的一部分。如果你将有两种不同的线条尺寸,我认为你应该允许它并入图例中(即将'scale_size_manual'参数设置为与'scale_colour_manual'相同,以便合并图例,而不是使用'guide =“none”'来压制图例) – 2013-05-13 15:07:37
使用@ 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))
,并得到
你应该阅读介绍GGPLOT2。基本上,你需要结合两个数据集,并使用审美映射。 – baptiste 2013-05-12 21:27:22