GIGPLATE:关于形态美学的两个截然不同的传说 [英] ggplot: two distinct legends on shape aesthetics

查看:0
本文介绍了GIGPLATE:关于形态美学的两个截然不同的传说的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能在ggploy中为形状美学添加第二个图例?

该图表显示了不同的起始类型(形状)、不同的进程(颜色)和不同的 端面类型(再次为形状)。目前,我只能显示两个图例:一个用于颜色,另一个用于形状(合并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)

推荐答案

编辑图例可能很痛苦,而且很容易损坏,但如果您必须尝试,这里有一个快速而肮脏的方法

require(gtable)
g = ggplotGrob(u.plot) # calls ggplot_build and ggplot_gtable
a = g$grobs[[8]][["grobs"]][[2]] # extract the legend (a gtable itself)
a = gtable_add_rows(a, unit(c(0.5,1),"line"), 6) # add two rows
new = editGrob(a[["grobs"]][[2]], label = "I'm in") # copy legend title with new text

a = gtable_add_grobs(a, new, 8, l=2, r=5) # place the new title
g$grobs[[8]][["grobs"]][[2]] = a # replace with new legend in the plot
grid.newpage()
grid.draw(g)

这篇关于GIGPLATE:关于形态美学的两个截然不同的传说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆