Skip to content

Commit b5f3039

Browse files
committed
necromancy: resurrect stat_bin(drop) by sacrificing stat_bin(keep.zeroes)
1 parent 4873a0c commit b5f3039

File tree

5 files changed

+25
-22
lines changed

5 files changed

+25
-22
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
* The ellipsis argument is now checked in `fortify()`, `get_alt_text()`,
226226
`labs()` and several guides (@teunbrand, #3196).
227227
* `stat_summary_bin()` no longer ignores `width` parameter (@teunbrand, #4647).
228-
* Added `keep.zeroes` argument to `stat_bin()` (@teunbrand, #3449)
228+
* Reintroduced `drop` argument to `stat_bin()` (@teunbrand, #3449)
229229
* (internal) removed barriers for using 2D structures as aesthetics
230230
(@teunbrand, #4189).
231231
* `coord_sf()` no longer errors when dealing with empty graticules (@teunbrand, #6052)

R/bin.R

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,6 @@ bin_out <- function(count = integer(0), x = numeric(0), width = numeric(0),
227227

228228
fix_bin_params = function(params, fun, version) {
229229

230-
if (!is.null(params$drop)) {
231-
args <- paste0(fun, c("(drop)", "(pad)"))
232-
deprecate_warn0(version, args[1], args[2])
233-
params$drop <- NULL
234-
}
235-
236230
if (!is.null(params$origin)) {
237231
args <- paste0(fun, c("(origin)", "(boundary)"))
238232
deprecate_warn0(version, args[1], args[2])

R/stat-bin.R

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
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 keep.zeroes Treatment of zero count bins. If `"all"` (default), such
29+
#' @param drop Treatment of zero count bins. If `"all"` (default), such
3030
#' bins are kept as-is. If `"none"`, all zero count bins are filtered out.
3131
#' If `"inner"` only zero count bins at the flanks are filtered out, but not
32-
#' in the middle.
32+
#' in the middle. `TRUE` is shorthand for `"all"` and `FALSE` is shorthand
33+
#' for `"none"`.
3334
#' @eval rd_computed_vars(
3435
#' count = "number of points in bin.",
3536
#' density = "density of points in bin, scaled to integrate to 1.",
@@ -59,7 +60,7 @@ stat_bin <- function(mapping = NULL, data = NULL,
5960
closed = c("right", "left"),
6061
pad = FALSE,
6162
na.rm = FALSE,
62-
keep.zeroes = "all",
63+
drop = "all",
6364
orientation = NA,
6465
show.legend = NA,
6566
inherit.aes = TRUE) {
@@ -82,7 +83,7 @@ stat_bin <- function(mapping = NULL, data = NULL,
8283
pad = pad,
8384
na.rm = na.rm,
8485
orientation = orientation,
85-
keep.zeroes = keep.zeroes,
86+
drop = drop,
8687
...
8788
)
8889
)
@@ -95,9 +96,13 @@ stat_bin <- function(mapping = NULL, data = NULL,
9596
StatBin <- ggproto("StatBin", Stat,
9697
setup_params = function(self, data, params) {
9798
params$flipped_aes <- has_flipped_aes(data, params, main_is_orthogonal = FALSE)
98-
params$keep.zeroes <- arg_match0(
99-
params$keep.zeroes %||% "all",
100-
c("all", "none", "inner"), arg_nm = "keep.zeroes"
99+
100+
if (is.logical(params$drop)) {
101+
params$drop <- if (isTRUE(params$drop)) "all" else "none"
102+
}
103+
params$drop <- arg_match0(
104+
params$drop %||% "all",
105+
c("all", "none", "inner"), arg_nm = "drop"
101106
)
102107

103108
has_x <- !(is.null(data$x) && is.null(params$x))
@@ -127,10 +132,10 @@ StatBin <- ggproto("StatBin", Stat,
127132
compute_group = function(data, scales, binwidth = NULL, bins = NULL,
128133
center = NULL, boundary = NULL,
129134
closed = c("right", "left"), pad = FALSE,
130-
breaks = NULL, flipped_aes = FALSE, keep.zeroes = "all",
135+
breaks = NULL, flipped_aes = FALSE, drop = "all",
131136
# The following arguments are not used, but must
132137
# be listed so parameters are computed correctly
133-
origin = NULL, right = NULL, drop = NULL) {
138+
origin = NULL, right = NULL) {
134139
x <- flipped_names(flipped_aes)$x
135140
bins <- compute_bins(
136141
data[[x]], scales[[x]],
@@ -140,7 +145,7 @@ StatBin <- ggproto("StatBin", Stat,
140145
bins <- bin_vector(data[[x]], bins, weight = data$weight, pad = pad)
141146

142147
keep <- switch(
143-
keep.zeroes,
148+
drop,
144149
none = bins$count != 0,
145150
inner = inner_runs(bins$count != 0),
146151
TRUE

R/stat-bin2d.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ StatBin2d <- ggproto("StatBin2d", Stat,
5353
required_aes = c("x", "y"),
5454

5555
setup_params = function(self, data, params) {
56-
params <- fix_bin_params(params, fun = snake_class(self), version = "3.5.2")
5756

57+
if (is.character(params$drop)) {
58+
params$drop <- !identical(params$drop, "none")
59+
}
60+
61+
params <- fix_bin_params(params, fun = snake_class(self), version = "3.5.2")
5862
vars <- c("origin", "binwidth", "breaks", "center", "boundary")
5963
params[vars] <- lapply(params[vars], dual_param)
6064
params$closed <- dual_param(params$closed, list(x = "right", y = "right"))

tests/testthat/test-stat-bin.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,17 @@ test_that("stat_bin() provides width (#3522)", {
118118
expect_equal(out$xmax - out$xmin, rep(binwidth, 10))
119119
})
120120

121-
test_that("stat_bin(keep.zeroes) options work as intended", {
121+
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, keep.zeroes = "all"))
125+
ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "all"))
126126
expect_equal(ld$x, -1:9)
127127

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

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

0 commit comments

Comments
 (0)