【发布时间】:2014-07-21 19:22:52
【问题描述】:
如何在 ggplot 中为形状美学添加第二个图例?
图表显示不同的开始类型(形状)、不同的过程(颜色)和不同的 结束类型(再次塑造)。目前,我只能显示两个图例:一个用于颜色,一个用于形状(合并 start.type 和 end.tpye)。关于如何获得单独的第三个图例(用于 end.type)的任何帮助?引入线型作为额外的美学不是一种选择。非常感谢。
id <- c("1","2","3")
start.date <- c("01/01/2010","05/05/2004","01/08/2006")
end.date <- c("31/12/2012","05/05/2007","01/09/2009")
start.type <- c("x1","x2","x3")
end.type <- c("y1","y2","y3")
process.type <- c("p1","p2","p3")
u <- data.frame(id,start.date, end.date,start.type,end.type,process.type)
u.plot <- ggplot(u)+
geom_segment(aes(color=process.type, x=start.date, xend=end.date, y=id, yend=id), size=1)+
geom_point(aes(shape=start.type, x=start.date, y=id), size=3)+
geom_point(aes(shape=end.type, x=end.date, y=id), size=3)
plot(u.plot)
【问题讨论】:
-
标准 ggplot 只允许每个美学一个图例。如果你愿意,你将不得不大量破解它。那是你想做的事情吗?事后在图形程序中编辑它以分割图例的一部分可能会更容易。这取决于这对你有多重要。
-
我已经担心会是这种情况。对此没有标准方法感到非常惊讶。问题是这个(类似的)代码在许多数据帧上循环运行。不过还是谢谢。
标签: r ggplot2 legend gantt-chart