从颜色中删除线条并填充图例 [英] Remove lines from color and fill legends

查看:29
本文介绍了从颜色中删除线条并填充图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三种不同图例的图:一种用于 linetype,一种用于 color,一种用于 fill.在 colorfill 图例中,还有一些我想删除的行,但是如何删除?

I have a plot with three different legends: one for linetype, one for color, and one for fill. In the color and fill legends there are also some lines which I wish to remove, but how?

这是一些示例代码:

# 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"指南有红线穿过点,我想删除它们!
骗局"指南也有应该删除的红线.

I get the "name" guide for both red lines, that is fine.
The "col" guide has red lines crossing the dots, I want to remove them!
The "con" guide also has red lines which should be removed.

我可以用

guides(fill = guide_legend(override.aes = list(colour = NULL)),
       color = guide_legend(override.aes = list(colour = NULL)))

这会去除颜色,但线条仍然存在.

This removes the colour, but the lines are still there.

提前致谢!

推荐答案

You may set linetype = 0 or "blank" (on different linetypecode>s 此处) 用于 fillcolor guide 在您的 override.aes 调用中.

You may set linetype = 0 or "blank" (on different linetypes here) for the filland color guides in your override.aes call.

另请注意,我将 fill aesggplot 中的顶级"移动到了 geom_bar.

Also note that I moved the fill aes from the 'top level' in ggplot to 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)))

这篇关于从颜色中删除线条并填充图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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