R ggplot2 – why does geom_boxplot ignore aesthetics "ymin", "lower", "middle", "upper", "ymax"?
For some testing purposes, I am trying to make boxplots where the upper and lower whiskers extend to the max and min data points (respectively), instead of treating them like outliers.
Not completely sure how this can be done best, but I figured I would just change my definition of upper and lower whisker to
min(), and pass them to
ymax aesthetics (as indicated here: https://ggplot2.tidyverse.org/reference/geom_boxplot.html)
However, even when I get no warnings or errors, those aesthetics seem to be completely ignored.
See the MWE below with
data("iris") iris_long <- as.data.frame(tidyr::pivot_longer(subset(iris, Species=="setosa"), -Species, names_to = "Var", values_to = "Value")) iris_long_dt <- data.table::data.table(iris_long) ddf <- as.data.frame(iris_long_dt[,list(y0=min(Value, na.rm=T), y100=max(Value, na.rm=T)), by=list(Var=Var)]) iris_long <- merge(iris_long, ddf, by="Var") grDevices::png(filename="test.png", height=600, width=600) P <- ggplot2::ggplot(iris_long, ggplot2::aes(x=Var, y=Value)) + ggplot2::geom_boxplot(ggplot2::aes(fill=Var, ymin=y0, ymax=y100), position=ggplot2::position_dodge(.9)) + ggplot2::stat_summary(fun=mean, geom="point", shape=5, size=2) + ggplot2::theme_light() print(P) grDevices::dev.off()
This is what I get (which is identical to not passing
What I would expect is that the whiskers would extend to the min and max outlier data points (and hence the outliers not plotted, since they would not be outliers anymore).
Why is this happening? Am I doing something wrong? Thanks!
Solution – 1
Unfortunately it’s not possible to provide only some of the boxplot stats. If you want to draw a boxplot manually you have to provide all stats and use
But to extend the whiskers over the whole data range you could use
library(ggplot2) ggplot(iris_long, aes(x = Var, y = Value)) + geom_boxplot(aes(fill = Var), position = position_dodge(.9), coef = Inf ) + stat_summary(fun = mean, geom = "point", shape = 5, size = 2) + theme_light()