diff --git a/NEWS.md b/NEWS.md index 713d2198f8..40bdb3d156 100644 --- a/NEWS.md +++ b/NEWS.md @@ -175,6 +175,7 @@ * {tibble} is now suggested instead of imported (@teunbrand, #5986) * The ellipsis argument is now checked in `fortify()`, `get_alt_text()`, `labs()` and several guides (@teunbrand, #3196). +* `stat_summary_bin()` no longer ignores `width` parameter (@teunbrand, #4647). # ggplot2 3.5.1 diff --git a/R/stat-summary-bin.R b/R/stat-summary-bin.R index 3a4fea585e..d94b6ddab9 100644 --- a/R/stat-summary-bin.R +++ b/R/stat-summary-bin.R @@ -79,7 +79,7 @@ StatSummaryBin <- ggproto("StatSummaryBin", Stat, compute_group = function(data, scales, fun = NULL, 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) x <- flipped_names(flipped_aes)$x breaks <- bin2d_breaks(scales[[x]], breaks, origin, binwidth, bins, right = right) @@ -89,7 +89,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) } 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", {