ggplot传说中的斜线 [英] ggplot legend slashes

查看:1530
本文介绍了ggplot传说中的斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这肯定会让过去的人恼火,所以如果这是重复的,我会删除它。使用 geom_bar 时可能会令人讨厌。例如:

  x < -  c(a,b)
y < - c(1 ,2)
df < - as.data.frame(cbind(x,y))
a < - ggplot(df,aes(x = x,y = y,fill = x))
a + geom_bar(color =black)+ scale_fill_manual(values = c(white,black))



当我使用彩色条纹时,我使用这个工作,首先绘制没有颜色的条,例如

  a + geom_bar()+ geom_bar(color =black,show_guide = FALSE)+ 
scale_fill_manual(values = c(white,black))

然而,当填充为白色时,这会在图例中留下不带边框的令人满意的空白框。例如





过去我使用图形软件手动修复了这个问题,但是现在我认为这对于有足够的人在这里提出问题必须有用。我们可以让 ggplot 只绘制黑色轮廓但没有斜线的图例吗?

解决方案

  a + geom_bar()+ geom_bar(color =black,show_guide = FALSE)+ 
scale_fill_manual(values = c(white,black))+
opts(legend.key = theme_rect(fill ='black'))

给了我这个,

感谢本网站



Alos,使用 color 而不是填充

  a + geom_bar()+ geom_bar(color =black)(可能有人认为其中一种比较好) ,show_guide = FALSE)+ 
scale_fill_manual(values = c(white,black))+
opts(legend.key = theme_rect(color ='black'))

重要说明:现代版本的ggplot2 opts 已被弃用,并被替换为 theme ,并且 theme_rect 已被替换为 element_rect


This must have annoyed someone in the past so excuse me if this is a duplicate and I will remove it. Slashes across legends when using geom_bar can be annoying. e.g.:

x  <- c("a","b")
y  <- c(1,2)
df <- as.data.frame(cbind(x,y))
a  <- ggplot(df,aes(x=x,y=y,fill=x))
a + geom_bar(colour="black") + scale_fill_manual(values=c("white", "black"))

When I use coloured bars I use this work around, plotting bars without colours first e.g.

a + geom_bar() + geom_bar(colour="black",show_guide=FALSE) +
 scale_fill_manual(values=c("white", "black"))

However when the fill is white this leaves an unsatisfying empty white box in the legend without a border. e.g.

I have fixed this in the past manually using graphics software but now I think this must be of use to enough people to ask a question here. Can we make ggplot plot the legend with the black outline only but without the slash?

解决方案

this,

 a + geom_bar() + geom_bar(colour="black",show_guide=FALSE) +
 scale_fill_manual(values=c("white", "black")) + 
 opts(legend.key = theme_rect(fill = 'black'))

gave me this, thanks to this site.

Alos, you get the same result using colour instead of fill (it might be argued that one is better than).

a + geom_bar() + geom_bar(colour="black",show_guide=FALSE) + 
scale_fill_manual(values=c("white", "black")) + 
opts(legend.key = theme_rect(colour = 'black'))

Important note: In modern versions of ggplot2 opts has been deprecated and replaced with theme, and theme_rect has been replaced by element_rect.

这篇关于ggplot传说中的斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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