Home > OS >  Ggplot boxplot by group, change summary statistics shown
Ggplot boxplot by group, change summary statistics shown

Time:10-19

I want to change the summary statistics shown in the following boxplot:

boxplot

I have created the boxplot as follows:

ggplot(as.data.frame(beta2), aes(y=var1,x=as.factor(Year)))   
  geom_boxplot(outlier.shape = NA) 
  ylab(expression(beta[1])) 
  xlab("\nYear") 
  theme_bw()

The default is for the box is the first and third quantile. I want the box to show the 2.5% and 97.5% quantiles. I know one can easily change what is shown when one boxplot is visualised by adding the following to geom_boxplot:

aes(
    ymin= min(var1),
    lower = quantile(var1,0.025),
    middle = mean(var1),
    upper = quantile(var1,0.975),
    ymax=max(var1))

However, this does not work for when boxplots are generated by group. Any idea how one would do this? You can use the Iris data set:

ggplot(iris, aes(y=Sepal.Length,x=Species))   
  geom_boxplot(outlier.shape = NA)

CodePudding user response:

This could be achieved via stat_summary by setting geom="boxplot". and passing to fun.data a function which returns a data frame with the summary statistics you want to display as ymin, lower, ... in your boxplot:

library(ggplot2)

ggplot(iris, aes(x = Species, y = Sepal.Length))  
  stat_summary(geom = "boxplot", fun.data = function(x) {
    data.frame(
      ymin = min(x),
      lower = quantile(x, 0.025),
      middle = mean(x),
      upper = quantile(x, 0.975),
      ymax = max(x) 
    )}, outlier.shape = NA)

  • Related