【问题标题】:Remove lines from color and fill legends从颜色中删除线条并填充图例
【发布时间】:2015-07-08 15:08:18
【问题描述】:

我有一个包含三个不同图例的情节:一个是linetype,一个是color,一个是fill。在colorfill 的传说中,还有一些我想删除的行,但是如何删除呢?

下面是一些示例代码:

# 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 两次的技巧,在后面和前面的一个很棒!您要发布答案,以便我将其标记为已修复吗?

标签: r ggplot2 colors legend


【解决方案1】:

使用:

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=FALSE) +
  guides(linetype=FALSE,color=FALSE)

给了我这个情节:

【讨论】:

  • 谢谢@Jaap,但我想要所有指南...只是想从concol 指南中删除红线...
  • 所以,基本上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=FALSE)concol 的预期结果,但我也需要红线指南...
【解决方案2】:

根据 user20650 的建议

ggplot(df, aes(x,y)) + 
  geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=TRUE) + 
  geom_point(aes(color=col), size=5) + 
  geom_bar(aes(fill=con), stat='identity') + 
  geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=F) + 
  guides(color = guide_legend(override.aes = list(linetype = 0)))

所以第一个 geom_hline 创建了图例,但线在栏后面...
第二次调用将线放在条形前面,但不打印图例(好主意)。
las 指南正在用 0 覆盖美学线条类型...这样它会从图例中删除线条...我尝试使用 NULL 但这之前没有用...

再次感谢。

【讨论】:

    【解决方案3】:

    您可以在override.aes 调用中为fillcolor guides 设置linetype = 0"blank"(在不同的linetypes here)。

    另请注意,我将fill aesggplot 中的“顶级”移动到geom_bar

    ggplot(df, aes(x, y)) +
      geom_bar(aes(fill = con), stat = 'identity') + 
      geom_point(aes(color = col)) +
      geom_hline(data = hline_df, aes(yintercept = y, linetype = name), color = 'red', show_guide = TRUE) +
      guides(fill = guide_legend(override.aes = list(linetype = 0)),
             color = guide_legend(override.aes = list(linetype = 0)))
    

    【讨论】:

    • 谢谢,这甚至更简洁了,并解释了问题...它只是将 aes 线型设置为 0 用于填充和颜色图例...太好了!
    • 优秀的答案。我正在研究一种解决方案,提取几个图例,然后尝试将它们放在一个情节中。然而,这是一个更好、更优雅的解决方案! +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 2017-03-09
    • 2022-11-22
    • 2016-05-19
    • 1970-01-01
    相关资源
    最近更新 更多