I'm working on some boxplots. Here is a working example:
data(mtcars)
# Compute means for each group
mpgmn <- aggregate(mpg ~ cyl, mtcars, mean)
mpgmn$mpg <- round(mpgmn$mpg, 2)
# Same thing for 50th and 75th %tiles
mpglims <- mtcars %>% group_by(cyl) %>%
summarize(q50 = quantile(mpg, probs = 0.50),
q75 = quantile(mpg, probs = 0.75))
# Plot
library(ggplot2)
g <- ggplot(mtcars, aes(x = as.factor(cyl), y = mpg,
fill = as.factor(cyl)))
g <- g + geom_boxplot()
g <- g + stat_summary(fun = mean, color = "white", geom = "point",
shape = 18, size = 3, show.legend = FALSE)
g <- g + geom_text(data = mpgmn,
aes(label = paste("mean = ", mpg),
y = mpg + 0.5), color = "white")
g
All of this works. However, I would like to use the mpglims that I computed (that look correct to me) to place the white text inside each of the boxplots (i.e., instead of the current vertical position argument: y = mpg + 0.05). Is there a way to tell R to pick the halfway point between the two values that I computed for each group for the vertical position?
