Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alpha and fill legends in ggplot2 boxplots?

I'm trying to combine alpha and fill aesthetics. 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.

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

A simpler version can be done with no fill - it seems like bar defaults to gray/lightgray, and boxplot defaults to 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 really sure how to fix this. Any thoughts?

like image 538
benjamin Avatar asked Nov 15 '16 14:11

benjamin


People also ask

How do you fill a boxplot color in R?

To change the color of box of boxplot in base R, we can use col argument inside boxplot function.

How can you create a boxplot using ggplot2?

In ggplot2, geom_boxplot() is used to create a boxplot. Let us first create a regular boxplot, for that we first have to import all the required libraries and dataset in use. Then simply put all the attributes to plot by in ggplot() function along with geom_boxplot.

What variables does Stat_boxplot () Compute?

Computed variables stat_boxplot() provides the following variables, some of which depend on the orientation: width. width of boxplot. ymin or xmin.

Does Ggplot boxplot show median or mean?

A boxplot summarizes the distribution of a continuous variable and notably displays the median of each group.


1 Answers

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

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.

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 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:

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

enter image description here

like image 144
eipi10 Avatar answered Oct 22 '22 23:10

eipi10