Use mean in ggplot boxplots instead of median

2019-02-21 06:24发布

问题:

Is it possible to use the mean in a ggplot boxplot instead of the median? Reason I ask is that in my data the median = 0.0 and mean = 0.40 and I am interested in the mean.

回答1:

From the help ?geom_boxplot:

library(ggplot2)
# It's possible to draw a boxplot with your own computations if you
# use stat = "identity":
y <- rnorm(100)
df <- data.frame(
  x = 1,
  y0 = min(y),
  y25 = quantile(y, 0.25),
  y50 = median(y),   # <=== replace by mean
  y75 = quantile(y, 0.75),
  y100 = max(y)
)
ggplot(df, aes(x)) +
  geom_boxplot(
    aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
    stat = "identity"
  )

So you could pre-compute the box values, use stat="identity" and replace median by mean.