From 262e2be6c58c68d868d0eb0a396ed181ea55e28b Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 3 Jul 2024 10:05:22 +0200 Subject: [PATCH 1/3] `stat_summary_bin()` prefers provided `width` over a computed one --- R/stat-summary-bin.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/stat-summary-bin.R b/R/stat-summary-bin.R index ce57b6def9..048075f150 100644 --- a/R/stat-summary-bin.R +++ b/R/stat-summary-bin.R @@ -74,7 +74,7 @@ StatSummaryBin <- ggproto("StatSummaryBin", Stat, fun.max = NULL, fun.min = NULL, fun.args = list(), bins = 30, binwidth = NULL, breaks = NULL, origin = NULL, right = FALSE, na.rm = FALSE, - flipped_aes = FALSE) { + flipped_aes = FALSE, width = NULL) { data <- flip_data(data, flipped_aes) fun <- make_summary_fun(fun.data, fun, fun.max, fun.min, fun.args) x <- flipped_names(flipped_aes)$x @@ -85,7 +85,7 @@ StatSummaryBin <- ggproto("StatSummaryBin", Stat, locs <- bin_loc(breaks, out$bin) out$x <- locs$mid - out$width <- if (scales[[x]]$is_discrete()) 0.9 else locs$length + out$width <- width %||% if (scales[[x]]$is_discrete()) 0.9 else locs$length out$flipped_aes <- flipped_aes flip_data(out, flipped_aes) } From 73e8194d8c487246599ff23f4381c8d38e6d0d0f Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 3 Jul 2024 10:27:48 +0200 Subject: [PATCH 2/3] add test --- tests/testthat/test-stat-summary.R | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-stat-summary.R b/tests/testthat/test-stat-summary.R index 825efd981c..abc2ffe5dd 100644 --- a/tests/testthat/test-stat-summary.R +++ b/tests/testthat/test-stat-summary.R @@ -40,8 +40,16 @@ test_that("stat_summary(_bin) work with lambda expressions", { }) +test_that("stat_summary_bin takes user's `width` argument (#4647)", { + p <- ggplot(mtcars, aes(mpg, disp)) + + stat_summary_bin( + fun.data = mean_se, na.rm = TRUE, + binwidth = 1, width = 2 + ) - + ld <- layer_data(p) + expect_equal(unique(ld$width), 2) +}) test_that("stat_summary_(2d|hex) work with lambda expressions", { From 1b710b37c2ac6fb1aafff187a72a3143756ac8da Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 3 Jul 2024 10:29:08 +0200 Subject: [PATCH 3/3] add news bullet --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index d553c7f924..900972760a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # ggplot2 (development version) +* `stat_summary_bin()` no longer ignores `width` parameter (@teunbrand, #4647). * `stat_bin()` now accepts functions for argument `breaks` (@aijordan, #4561) * (internal) The plot's layout now has a coord parameter that is used to prevent setting up identical panel parameters (#5427)