The issue is that the quantiles are computed for y
instead of the variable which is mapped on x
. The reason is that stat_summary
makes a guess about which variable to use for computing the quantiles but fails in your case as both x and y=0.5
are numeric (see the section Orientation in ?stat_summary
). To prevent that you have to tell stat_summary
which variable or axis to use for the computations of the quantiles by setting the orientation argument. Or you could add an auxiliary categorical variable by mapping e.g "1"
on y
. In that case stat_summary
will by default pick the numeric variable (i.e. the one mapped on x
) to compute the quantiles.
library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = "1")) +
geom_boxplot(width = 0.6) +
stat_summary(geom = "text", fun = quantile,
aes(label = ..x.., y = 1.5), size = 3.5)
ggplot(mtcars, aes(x = mpg)) +
geom_boxplot(width = 0.6) +
stat_summary(geom = "text", fun = quantile,
aes(label = ..x.., y = 0.5), size = 3.5, orientation = "y")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…