Skip to content

Commit

Permalink
stat_summary_bin(width) of preferred over default width (#5970)
Browse files Browse the repository at this point in the history
* `stat_summary_bin()` prefers provided `width` over a computed one

* add test

* add news bullet
  • Loading branch information
teunbrand committed Aug 28, 2024
1 parent 3109d11 commit c32fea1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions R/stat-summary-bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/test-stat-summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {

Expand Down

0 comments on commit c32fea1

Please sign in to comment.