Alpha and fill legends in ggplot2 boxplots?

I am trying to combine alpha and populate ggplot2. It works when I use geom_bar (or geom_points, for color), but the alpha legend does not work when I use geom_boxplot.

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

enter image description here

But not for boxplot - the alpha legend is empty.

 ggplot(dt, aes(x=x, y=y, fill=tag1, alpha=tag2, group=interaction(x,tag1,tag2))) + geom_boxplot() 

enter image description here

The simplest version can be executed without filling - it seems that by default for bars the default is gray / light, and by default for boxplot it is white / lightwhite:

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

enter image description here

 ggplot(dt, aes(x=x, y=y, alpha=tag2, group=interaction(x,tag2))) + geom_boxplot() 

enter image description here

But I'm not quite sure how to fix this. Any thoughts?

+5
source share
1 answer

I'm not sure why ggplot does not actually provide alpha levels in the legend for boxplots, but you can hardcode it with override.aes . (Editorial note: I find alpha aesthetics a bit confusing for both the drawer and the tablet. It's hard to mentally separate transparency from the fill color, and the gray alpha legend exacerbates the problem because nothing is grayed out in the plot.)

In the code below, to improve the visibility of the legend, I removed the lines from the alpha legend and increased the height of the legend key. I also edited aesthetics to remove the need for the group argument.

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

enter image description here

Another option is to use interaction for fill and alpha aesthetics, but it turns out that ggplot does not include any colors in this case:

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

enter image description here

So, instead, you can do all this with aesthetics of filling, but include transparency in the color specification. This works, but, again, since transparency and color are somewhat mixed in visual perception, it might be 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") 

enter image description here

+3
source

Source: https://habr.com/ru/post/1259772/


All Articles