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.
Asked
Active
Viewed 6,764 times
-2
-
2already answered see http://stackoverflow.com/questions/19876505/boxplot-show-the-value-of-mean – MLavoie Dec 30 '15 at 12:27
-
[Related question/answer](http://stackoverflow.com/questions/25999677/how-to-plot-mean-and-standard-error-in-boxplot-in-r) for how to use your own statistics in the place of standard boxplot statistics in ggplot2. – aosmith Dec 30 '15 at 16:04
1 Answers
5
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
.

lukeA
- 53,097
- 5
- 97
- 100