Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying separate means within fill groups in ggplot boxplot

I have a grouped boxplot using data with 3 categories. One category is set as the x-axis of the boxplots, the other is set as the fill, and the last one, as a faceting category. I want to display the means for each fill group, but using stat_summary only gives me the mean for the x-axis category, without separating the means for the fill:

facetted boxplots

Here is the current code:

demoplot<-ggplot(demo,aes(x=variable,y=value))
demoplot+geom_boxplot(aes(fill=category2),position=position_dodge(.9))+
stat_summary(fun.y=mean, colour="black", geom="point", shape=18, size=4,) +
facet_wrap(~category1)

Is there any way to display the mean for each category2 without having to manually compute and plot the points? Adjusting the position dodge doesn't really help, as it's just one computed mean. Would creating conditions within the mean() function be advisable?

For anyone interested, here's the data:

Advanced thanks for any enlightenment on this.

like image 753
dizzygirl Avatar asked Mar 19 '16 08:03

dizzygirl


1 Answers

Ggplot needs to have explicit information on grouping here. You can do that either by using a aes(group=....) in the desired layer, or moving the fill=... to the main call to ggplot. Without explicit grouping for a layer, ggplot will group by the factor on the x-axis. Here's some sample code with fake data:

library(ggplot2)
set.seed(123)

nobs <- 1000
dat <- data.frame(var1=sample(LETTERS[1:3],nobs, T),
                  var2=sample(LETTERS[1:2],nobs,T),
                  var3=sample(LETTERS[1:3],nobs,T),
                  y=rnorm(nobs))

p1 <- ggplot(dat, aes(x=var1, y=y)) +
  geom_boxplot(aes(fill=var2), position=position_dodge(.9)) +
  facet_wrap(~var3) +
  stat_summary(fun.y=mean, geom="point", aes(group=var2), position=position_dodge(.9), 
               color="black", size=4)

enter image description here

like image 99
Heroka Avatar answered Oct 19 '22 04:10

Heroka