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

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

问题描述

我有一个有三个不同图例的情节:一个用于 linetype ,一个用于 color ,另一个用于。在颜色填充传说中,还有一些我希望删除的行,但是如何?



以下是一些示例代码:

 #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'))

#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指南也有红线,应该删除。



我可以用
$ b修改部分图例$ b

 指南(fill = guide_legend(overrid e.aes = list(color = NULL)),
color = guide_legend(override.aes = list(color = NULL)))

这会消除颜色,但是线条仍然存在。



预先感谢!

解决方案

您可以设置 linetype = 0 blank(不同于 linetype s 在这里)为填充颜色 指南 s中的 override.aes 调用。



另外请注意,我移动了填充 ggplot geom_bar中 aes

  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) ='red',show_guide = TRUE)+
guides(fill = guide_legend(override.aes = list(linetype = 0)),
color = guide_legend(override.aes = list(linetype = 0)))


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?

Here is some example code:

# 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)

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.

I could modify parts of the legend with

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.

Thanks in advance!

解决方案

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

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天全站免登陆