From 22b85955bda32ce08c316d08a7343bf612b96dde Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Sat, 23 Aug 2025 22:35:48 +0800 Subject: [PATCH 1/4] fix: respect `width` aesthetic --- R/stat-boxplot.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/stat-boxplot.R b/R/stat-boxplot.R index 41819e5e1e..8d767cb6ce 100644 --- a/R/stat-boxplot.R +++ b/R/stat-boxplot.R @@ -63,9 +63,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 (vec_size(data) > 0L && !is.null(data$width)) { + 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]) From 67d134dfaf3dd02977bc05e2a15698442b6ad5aa Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Thu, 25 Sep 2025 15:24:54 +0800 Subject: [PATCH 2/4] Apply suggestion from @teunbrand Co-authored-by: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> --- R/stat-boxplot.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/stat-boxplot.R b/R/stat-boxplot.R index 8d767cb6ce..492df1eade 100644 --- a/R/stat-boxplot.R +++ b/R/stat-boxplot.R @@ -63,7 +63,7 @@ StatBoxplot <- ggproto("StatBoxplot", Stat, if (any(outliers)) { stats[c(1, 5)] <- range(c(stats[2:4], data$y[!outliers]), na.rm = TRUE) } - if (vec_size(data) > 0L && !is.null(data$width)) { + if (length(data$width) > 0L) { width <- data$width[1L] } else if (vec_unique_count(data$x) > 1) { width <- diff(range(data$x)) * 0.9 From bba1d9a035093cf1e1bbc40aac138631459cdde5 Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Thu, 25 Sep 2025 15:30:25 +0800 Subject: [PATCH 3/4] feat(StatBoxplot): support width as an optional aesthetic --- R/stat-boxplot.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/stat-boxplot.R b/R/stat-boxplot.R index 492df1eade..fc7a334931 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"), From e57ea270210a02f2efe171b5c38128ce4a20de1f Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 25 Sep 2025 11:22:53 +0200 Subject: [PATCH 4/4] add news bullet --- NEWS.md | 1 + 1 file changed, 1 insertion(+) 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