ggplot:将alpha值添加到整个图层

时间:2016-07-11 11:28:00

标签: r ggplot2 boxplot

我试图将一些箱图绘制为半透明的。当我设置alpha值时,仅调整填充透明度,而不调整边框/笔触/颜色。

知道如何让整个geom层透明吗?

library(ggplot2)
ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot(aes(fill = factor(cyl), color = factor(cyl)), alpha = 0) 

enter image description here

1 个答案:

答案 0 :(得分:4)

这不是开箱即用的,因为多边形ggplot2仅将alpha应用于fill,而不是colour。为了解决这个问题,我们将采用以下特殊补丁,方法是采用低级内部,并在需要时添加alpha映射。

结帐the following gist。不会在这里发布,因为它太长了。

ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot(aes(fill = factor(cyl), color = factor(cyl)), alpha = 0.4, size = 1.4) 

在: enter image description here

在: enter image description here