【发布时间】:2015-07-08 15:08:18
【问题描述】:
我有一个包含三个不同图例的情节:一个是linetype,一个是color,一个是fill。在color 和fill 的传说中,还有一些我想删除的行,但是如何删除呢?
下面是一些示例代码:
# some data
hline_df <- data.frame(name = c('a', 'b'), y = c(1, 2))
df <- data.frame(x = c(1, 2), y = c(0.5, 1.5), con = c('a', 'b'), col = c('d', 'e'))
# the plot
ggplot(df, aes(x, y, fill = con)) +
geom_bar(stat = 'identity') +
geom_point(aes(color = col)) +
geom_hline(data = hline_df, aes(yintercept = y, linetype = name),
color = 'red', show_guide = TRUE)
我得到两条红线的“名称”指南,这很好。
“col”指南有红线交叉点,我想删除它们!
“con”指南也有应该删除的红线。
我可以用
修改部分图例guides(fill = guide_legend(override.aes = list(colour = NULL)),
color = guide_legend(override.aes = list(colour = NULL)))
这会去除颜色,但线条仍然存在。
提前致谢!
【问题讨论】:
-
嗯,重新排列到
ggplot(df, aes(x,y,fill=con)) + geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=TRUE) + geom_point(aes(color=col)) + geom_bar(stat='identity') + geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=F)似乎对con有效,但红线仍在col中。老实说,我不明白它为什么起作用:-) -
啊,我尝试设置
linetype=NULL,但这并没有按预期工作......另外,绘制 hline 两次的技巧,在后面和前面的一个很棒!您要发布答案,以便我将其标记为已修复吗?