Skip to content

Commit 016b78f

Browse files
authored
Align stat_bin(drop) with meaning (#6323)
* invert logic * swap default * update tests * sync defaults
1 parent f672845 commit 016b78f

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

R/stat-bin.R

+11-10
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
#' or left edges of bins are included in the bin.
2727
#' @param pad If `TRUE`, adds empty bins at either end of x. This ensures
2828
#' frequency polygons touch 0. Defaults to `FALSE`.
29-
#' @param drop Treatment of zero count bins. If `"all"` (default), such
30-
#' bins are kept as-is. If `"none"`, all zero count bins are filtered out.
31-
#' If `"inner"` only zero count bins at the flanks are filtered out, but not
32-
#' in the middle. `TRUE` is shorthand for `"all"` and `FALSE` is shorthand
29+
#' @param drop Treatment of zero count bins. If `"none"` (default), such
30+
#' bins are kept as-is. If `"all"`, all zero count bins are filtered out.
31+
#' If `"extremes"` only zero count bins at the flanks are filtered out, but
32+
#' not in the middle. `TRUE` is shorthand for `"all"` and `FALSE` is shorthand
3333
#' for `"none"`.
3434
#' @eval rd_computed_vars(
3535
#' count = "number of points in bin.",
@@ -60,7 +60,7 @@ stat_bin <- function(mapping = NULL, data = NULL,
6060
closed = c("right", "left"),
6161
pad = FALSE,
6262
na.rm = FALSE,
63-
drop = "all",
63+
drop = "none",
6464
orientation = NA,
6565
show.legend = NA,
6666
inherit.aes = TRUE) {
@@ -100,9 +100,10 @@ StatBin <- ggproto("StatBin", Stat,
100100
if (is.logical(params$drop)) {
101101
params$drop <- if (isTRUE(params$drop)) "all" else "none"
102102
}
103+
drop <- params$drop
103104
params$drop <- arg_match0(
104-
params$drop %||% "all",
105-
c("all", "none", "inner"), arg_nm = "drop"
105+
params$drop %||% "none",
106+
c("all", "none", "extremes"), arg_nm = "drop"
106107
)
107108

108109
has_x <- !(is.null(data$x) && is.null(params$x))
@@ -132,7 +133,7 @@ StatBin <- ggproto("StatBin", Stat,
132133
compute_group = function(data, scales, binwidth = NULL, bins = NULL,
133134
center = NULL, boundary = NULL,
134135
closed = c("right", "left"), pad = FALSE,
135-
breaks = NULL, flipped_aes = FALSE, drop = "all",
136+
breaks = NULL, flipped_aes = FALSE, drop = "none",
136137
# The following arguments are not used, but must
137138
# be listed so parameters are computed correctly
138139
origin = NULL, right = NULL) {
@@ -146,8 +147,8 @@ StatBin <- ggproto("StatBin", Stat,
146147

147148
keep <- switch(
148149
drop,
149-
none = bins$count != 0,
150-
inner = inner_runs(bins$count != 0),
150+
all = bins$count != 0,
151+
extremes = inner_runs(bins$count != 0),
151152
TRUE
152153
)
153154
bins <- vec_slice(bins, keep)

man/geom_histogram.Rd

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-stat-bin.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ test_that("stat_bin(drop) options work as intended", {
122122
p <- ggplot(data.frame(x = c(1, 2, 2, 3, 5, 6, 6, 7)), aes(x)) +
123123
scale_x_continuous(limits = c(-1, 9))
124124

125-
ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "all"))
125+
ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "none"))
126126
expect_equal(ld$x, -1:9)
127127

128-
ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "inner"))
128+
ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "extremes"))
129129
expect_equal(ld$x, c(1:7))
130130

131-
ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "none"))
131+
ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "all"))
132132
expect_equal(ld$x, c(1:3, 5:7))
133133
})
134134

0 commit comments

Comments
 (0)