diff --git a/NEWS.md b/NEWS.md index 0fd950dcf6..3e92547b2a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,6 +9,7 @@ * Allow `stat` in `geom_hline`, `geom_vline`, and `geom_abline`. (@sierrajohnson, #6559) +* `stat_boxplot()` treats `width` as an optional aesthetic (@Yunuuuu, #6575) # ggplot2 4.0.0 diff --git a/R/stat-boxplot.R b/R/stat-boxplot.R index ff51b8354d..17635cd35b 100644 --- a/R/stat-boxplot.R +++ b/R/stat-boxplot.R @@ -5,6 +5,7 @@ StatBoxplot <- ggproto("StatBoxplot", Stat, required_aes = c("y|x"), non_missing_aes = "weight", + optional_aes = "width", # either the x or y aesthetic will get dropped during # statistical transformation, depending on the orientation dropped_aes = c("x", "y", "weight"), @@ -69,9 +70,11 @@ StatBoxplot <- ggproto("StatBoxplot", Stat, if (any(outliers)) { stats[c(1, 5)] <- range(c(stats[2:4], data$y[!outliers]), na.rm = TRUE) } - - if (vec_unique_count(data$x) > 1) + if (length(data$width) > 0L) { + width <- data$width[1L] + } else if (vec_unique_count(data$x) > 1) { width <- diff(range(data$x)) * 0.9 + } df <- data_frame0(!!!as.list(stats)) df$outliers <- list(data$y[outliers])