在 ggplot boxplots 中使用均值而不是中值

Gui*_*167 -2 r mean median ggplot2

是否可以在 ggplot boxplot 中使用平均值而不是中位数?我问的原因是在我的数据中,中位数 = 0.0,平均值 = 0.40,我对平均值感兴趣。

luk*_*keA 5

从帮助?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"
  )
Run Code Online (Sandbox Code Playgroud)

因此,您可以预先计算框值,使用stat="identity"并替换medianmean