简体   繁体   中英

Ggplot boxplot by group, change summary statistics shown

I want to change the summary statistics shown in the following 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)

EDIT:

The answer accepted does work. My data-frame is really big and as such the method provided takes a bit of time. I found another solution here: SOLUTION that works for large datasets and my specific need.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM