Skip to content

Commit c32fea1

Browse files
authored
stat_summary_bin(width) of preferred over default width (#5970)
* `stat_summary_bin()` prefers provided `width` over a computed one * add test * add news bullet
1 parent 3109d11 commit c32fea1

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
* {tibble} is now suggested instead of imported (@teunbrand, #5986)
176176
* The ellipsis argument is now checked in `fortify()`, `get_alt_text()`,
177177
`labs()` and several guides (@teunbrand, #3196).
178+
* `stat_summary_bin()` no longer ignores `width` parameter (@teunbrand, #4647).
178179

179180
# ggplot2 3.5.1
180181

R/stat-summary-bin.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ StatSummaryBin <- ggproto("StatSummaryBin", Stat,
7979
compute_group = function(data, scales, fun = NULL,
8080
bins = 30, binwidth = NULL, breaks = NULL,
8181
origin = NULL, right = FALSE, na.rm = FALSE,
82-
flipped_aes = FALSE) {
82+
flipped_aes = FALSE, width = NULL) {
8383
data <- flip_data(data, flipped_aes)
8484
x <- flipped_names(flipped_aes)$x
8585
breaks <- bin2d_breaks(scales[[x]], breaks, origin, binwidth, bins, right = right)
@@ -89,7 +89,7 @@ StatSummaryBin <- ggproto("StatSummaryBin", Stat,
8989

9090
locs <- bin_loc(breaks, out$bin)
9191
out$x <- locs$mid
92-
out$width <- if (scales[[x]]$is_discrete()) 0.9 else locs$length
92+
out$width <- width %||% if (scales[[x]]$is_discrete()) 0.9 else locs$length
9393
out$flipped_aes <- flipped_aes
9494
flip_data(out, flipped_aes)
9595
}

tests/testthat/test-stat-summary.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,16 @@ test_that("stat_summary(_bin) work with lambda expressions", {
4040

4141
})
4242

43+
test_that("stat_summary_bin takes user's `width` argument (#4647)", {
44+
p <- ggplot(mtcars, aes(mpg, disp)) +
45+
stat_summary_bin(
46+
fun.data = mean_se, na.rm = TRUE,
47+
binwidth = 1, width = 2
48+
)
4349

44-
50+
ld <- layer_data(p)
51+
expect_equal(unique(ld$width), 2)
52+
})
4553

4654
test_that("stat_summary_(2d|hex) work with lambda expressions", {
4755

0 commit comments

Comments
 (0)