For completeness, you can also overpay:
set.seed(753) df <- data.frame(y=rt(100, 4), x=gl(5, 20)) bx.p <- boxplot(y~x, df) bx.p$stats[3, ] <- unclass(with(df, by(y, x, FUN = mean))) bxp(bx.p, add=T, boxfill="transparent", medcol="red", axes=F, outpch = NA, outlty="blank", boxlty="blank", whisklty="blank", staplelty="blank")
Explanation via @scs:
bxp$stats returns a matrix containing the lower whisker, lower hinge, median, upper loop and extreme upper thread for each box. The solution above overwrites the median specified in bx.p$stats[3, ] with an average value. The bxp function is a function for constructing boxplot objects.
Result:

lukeA
source share