I'm trying to combine alpha and fill in ggplot2. It works when I'm using geom_bar (or geom_points, for color), but the alpha legend doesn't work in when I'm using geom_boxplot.
我正在尝试将alpha和填充ggplot2结合起来。当我使用geom_bar(或geom_points,用于颜色)时它可以工作,但是当我使用geom_boxplot时,alpha图例不起作用。
library(data.table)
library(ggplot2)
dt = data.table(x = rep(1:5,6), y = rnorm(30), tag1 = rep(c('hey', 'what'), 15), tag2 = rep(c('yeah', 'yeah', 'so', 'so', 'so'), 6))
It works for bars:
适用于酒吧:
ggplot(dt[, list(y=mean(y)), by=list(x, tag1, tag2)], aes(x=x, y=y, fill=tag1, alpha=tag2, group=interaction(x,tag1,tag2))) + geom_bar(stat = 'identity', position = 'dodge')
But not for boxplot - the alpha legend is empty.
但不是boxplot - alpha图例是空的。
ggplot(dt, aes(x=x, y=y, fill=tag1, alpha=tag2, group=interaction(x,tag1,tag2))) + geom_boxplot()
A simpler version can be done with no fill - it seems like bar defaults to gray/lightgray, and boxplot defaults to white/lightwhite:
更简单的版本可以在没有填充的情况下完成 - 看起来条形图默认为灰色/浅灰色,而boxplot默认为白色/浅白色:
ggplot(dt[, list(y=mean(y)), by=list(x, tag2)], aes(x=x, y=y, alpha=tag2, group=interaction(x,tag2))) + geom_bar(stat = 'identity')
ggplot(dt, aes(x=x, y=y, alpha=tag2, group=interaction(x,tag2))) + geom_boxplot()
But I'm not really sure how to fix this.. Any thoughts?
但我真的不确定如何解决这个问题..有什么想法吗?
1 个解决方案
#1
4
I'm not sure why ggplot doesn't actually provide the alpha levels in the legend for boxplots, but you can hard code it using override.aes
. (Editorial note: I find the alpha aesthetic a bit confusing for either the boxplot or the bar plot. It's hard to mentally separate the transparency from the fill color and the grey-scale alpha legend exacerbates the problem, because nothing is mapped to grey in the plot.)
我不确定为什么ggplot实际上没有在箱图的图例中提供alpha级别,但你可以使用override.aes对其进行硬编码。 (编者注:我发现对于箱形图或条形图而言,alpha美学有点令人困惑。难以在心理上将透明度与填充颜色分开,而灰度alpha图例会加剧问题,因为没有任何内容映射到灰色剧情。)
In the code below, to improve visibility of the legend, I've removed the box lines from the alpha
legend and increased the legend key height. I've also edited the aesthetics to remove the need for the group
argument.
在下面的代码中,为了提高图例的可见性,我从alpha图例中删除了框线并增加了图例键高度。我还编辑了美学,以消除对群体论证的需要。
ggplot(dt, aes(x=factor(x), y=y, fill=tag1, alpha=tag2)) +
geom_boxplot() +
scale_alpha_manual(values=c(0.2,0.7)) +
guides(alpha=guide_legend(override.aes=list(fill=hcl(c(15,195),100,0,alpha=c(0.2,0.7)),
colour=NA))) +
theme(legend.key.height=unit(1,"cm"))
Another option would be to use interaction
for both the fill and alpha aesthetics, but it turns out ggplot doesn't include any colors in that case:
另一个选择是使用填充和alpha美学的交互,但事实证明ggplot在这种情况下不包括任何颜色:
ggplot(dt, aes(x=factor(x), y=y, alpha=interaction(tag1,tag2)),
fill=interaction(tag1,tag2)) +
geom_boxplot() +
scale_fill_manual(values=rep(hcl(c(15,195),100,65), 2)) +
scale_alpha_manual(values=rep(c(0.3, 1), each=2)) +
theme(legend.key.height=unit(2,"cm"))
So, instead you can do it all with the fill aesthetic, but include transparency in the colour specification. This works, but, once again, because transparency and color are somewhat intermingled in visual perception, it's probably better to just go with four different colors.
因此,您可以使用填充美学来完成所有操作,但在颜色规范中包含透明度。这是有效的,但是,再一次,因为透明度和颜色在视觉感知上有些混合,所以最好只选择四种不同的颜色。
ggplot(dt, aes(x=factor(x), y=y, fill=interaction(tag1,tag2,sep="-"))) +
geom_boxplot() +
scale_fill_manual(values=hcl(c(15,195,15,195),100,65, alpha=c(0.4,0.4,1,1))) +
theme(legend.key.height=unit(1,"cm")) +
labs(fill="Tag 1 - Tag 2")
#1
4
I'm not sure why ggplot doesn't actually provide the alpha levels in the legend for boxplots, but you can hard code it using override.aes
. (Editorial note: I find the alpha aesthetic a bit confusing for either the boxplot or the bar plot. It's hard to mentally separate the transparency from the fill color and the grey-scale alpha legend exacerbates the problem, because nothing is mapped to grey in the plot.)
我不确定为什么ggplot实际上没有在箱图的图例中提供alpha级别,但你可以使用override.aes对其进行硬编码。 (编者注:我发现对于箱形图或条形图而言,alpha美学有点令人困惑。难以在心理上将透明度与填充颜色分开,而灰度alpha图例会加剧问题,因为没有任何内容映射到灰色剧情。)
In the code below, to improve visibility of the legend, I've removed the box lines from the alpha
legend and increased the legend key height. I've also edited the aesthetics to remove the need for the group
argument.
在下面的代码中,为了提高图例的可见性,我从alpha图例中删除了框线并增加了图例键高度。我还编辑了美学,以消除对群体论证的需要。
ggplot(dt, aes(x=factor(x), y=y, fill=tag1, alpha=tag2)) +
geom_boxplot() +
scale_alpha_manual(values=c(0.2,0.7)) +
guides(alpha=guide_legend(override.aes=list(fill=hcl(c(15,195),100,0,alpha=c(0.2,0.7)),
colour=NA))) +
theme(legend.key.height=unit(1,"cm"))
Another option would be to use interaction
for both the fill and alpha aesthetics, but it turns out ggplot doesn't include any colors in that case:
另一个选择是使用填充和alpha美学的交互,但事实证明ggplot在这种情况下不包括任何颜色:
ggplot(dt, aes(x=factor(x), y=y, alpha=interaction(tag1,tag2)),
fill=interaction(tag1,tag2)) +
geom_boxplot() +
scale_fill_manual(values=rep(hcl(c(15,195),100,65), 2)) +
scale_alpha_manual(values=rep(c(0.3, 1), each=2)) +
theme(legend.key.height=unit(2,"cm"))
So, instead you can do it all with the fill aesthetic, but include transparency in the colour specification. This works, but, once again, because transparency and color are somewhat intermingled in visual perception, it's probably better to just go with four different colors.
因此,您可以使用填充美学来完成所有操作,但在颜色规范中包含透明度。这是有效的,但是,再一次,因为透明度和颜色在视觉感知上有些混合,所以最好只选择四种不同的颜色。
ggplot(dt, aes(x=factor(x), y=y, fill=interaction(tag1,tag2,sep="-"))) +
geom_boxplot() +
scale_fill_manual(values=hcl(c(15,195,15,195),100,65, alpha=c(0.4,0.4,1,1))) +
theme(legend.key.height=unit(1,"cm")) +
labs(fill="Tag 1 - Tag 2")