From ceba7a187339392e6e4be282be6c9261ad144d48 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 6 Feb 2025 10:43:29 +0100 Subject: [PATCH 1/4] invert logic --- R/stat-bin.R | 17 +++++++++-------- man/geom_histogram.Rd | 8 ++++---- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/R/stat-bin.R b/R/stat-bin.R index 711b0c5ad7..484218b889 100644 --- a/R/stat-bin.R +++ b/R/stat-bin.R @@ -26,10 +26,10 @@ #' or left edges of bins are included in the bin. #' @param pad If `TRUE`, adds empty bins at either end of x. This ensures #' frequency polygons touch 0. Defaults to `FALSE`. -#' @param drop Treatment of zero count bins. If `"all"` (default), such -#' bins are kept as-is. If `"none"`, all zero count bins are filtered out. -#' If `"inner"` only zero count bins at the flanks are filtered out, but not -#' in the middle. `TRUE` is shorthand for `"all"` and `FALSE` is shorthand +#' @param drop Treatment of zero count bins. If `"none"` (default), such +#' bins are kept as-is. If `"all"`, all zero count bins are filtered out. +#' If `"extremes"` only zero count bins at the flanks are filtered out, but +#' not in the middle. `TRUE` is shorthand for `"all"` and `FALSE` is shorthand #' for `"none"`. #' @eval rd_computed_vars( #' count = "number of points in bin.", @@ -100,9 +100,10 @@ StatBin <- ggproto("StatBin", Stat, if (is.logical(params$drop)) { params$drop <- if (isTRUE(params$drop)) "all" else "none" } + drop <- params$drop params$drop <- arg_match0( - params$drop %||% "all", - c("all", "none", "inner"), arg_nm = "drop" + params$drop %||% "none", + c("all", "none", "extremes"), arg_nm = "drop" ) has_x <- !(is.null(data$x) && is.null(params$x)) @@ -146,8 +147,8 @@ StatBin <- ggproto("StatBin", Stat, keep <- switch( drop, - none = bins$count != 0, - inner = inner_runs(bins$count != 0), + all = bins$count != 0, + extremes = inner_runs(bins$count != 0), TRUE ) bins <- vec_slice(bins, keep) diff --git a/man/geom_histogram.Rd b/man/geom_histogram.Rd index 0a27e87c10..1c09d758ac 100644 --- a/man/geom_histogram.Rd +++ b/man/geom_histogram.Rd @@ -174,10 +174,10 @@ or left edges of bins are included in the bin.} \item{pad}{If \code{TRUE}, adds empty bins at either end of x. This ensures frequency polygons touch 0. Defaults to \code{FALSE}.} -\item{drop}{Treatment of zero count bins. If \code{"all"} (default), such -bins are kept as-is. If \code{"none"}, all zero count bins are filtered out. -If \code{"inner"} only zero count bins at the flanks are filtered out, but not -in the middle. \code{TRUE} is shorthand for \code{"all"} and \code{FALSE} is shorthand +\item{drop}{Treatment of zero count bins. If \code{"none"} (default), such +bins are kept as-is. If \code{"all"}, all zero count bins are filtered out. +If \code{"extremes"} only zero count bins at the flanks are filtered out, but +not in the middle. \code{TRUE} is shorthand for \code{"all"} and \code{FALSE} is shorthand for \code{"none"}.} } \description{ From b9508bc81ee1803c61382be4ca759569a4c8549e Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 6 Feb 2025 11:17:25 +0100 Subject: [PATCH 2/4] swap default --- R/stat-bin.R | 2 +- man/geom_histogram.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/stat-bin.R b/R/stat-bin.R index 484218b889..756b37deb8 100644 --- a/R/stat-bin.R +++ b/R/stat-bin.R @@ -60,7 +60,7 @@ stat_bin <- function(mapping = NULL, data = NULL, closed = c("right", "left"), pad = FALSE, na.rm = FALSE, - drop = "all", + drop = "none", orientation = NA, show.legend = NA, inherit.aes = TRUE) { diff --git a/man/geom_histogram.Rd b/man/geom_histogram.Rd index 1c09d758ac..f0532d8b25 100644 --- a/man/geom_histogram.Rd +++ b/man/geom_histogram.Rd @@ -46,7 +46,7 @@ stat_bin( closed = c("right", "left"), pad = FALSE, na.rm = FALSE, - drop = "all", + drop = "none", orientation = NA, show.legend = NA, inherit.aes = TRUE From 39adb1b0ebdb2da32d0810762253df606ee7b81a Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 6 Feb 2025 11:17:31 +0100 Subject: [PATCH 3/4] update tests --- tests/testthat/test-stat-bin.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-stat-bin.R b/tests/testthat/test-stat-bin.R index 3df87821b8..de1c941b1e 100644 --- a/tests/testthat/test-stat-bin.R +++ b/tests/testthat/test-stat-bin.R @@ -122,13 +122,13 @@ test_that("stat_bin(drop) options work as intended", { p <- ggplot(data.frame(x = c(1, 2, 2, 3, 5, 6, 6, 7)), aes(x)) + scale_x_continuous(limits = c(-1, 9)) - ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "all")) + ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "none")) expect_equal(ld$x, -1:9) - ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "inner")) + ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "extremes")) expect_equal(ld$x, c(1:7)) - ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "none")) + ld <- layer_data(p + geom_histogram(binwidth = 1, drop = "all")) expect_equal(ld$x, c(1:3, 5:7)) }) From 3140f20936250de2b844b473fe58d20bda42ab52 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 6 Feb 2025 11:45:28 +0100 Subject: [PATCH 4/4] sync defaults --- R/stat-bin.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/stat-bin.R b/R/stat-bin.R index 756b37deb8..f65b54857b 100644 --- a/R/stat-bin.R +++ b/R/stat-bin.R @@ -133,7 +133,7 @@ StatBin <- ggproto("StatBin", Stat, compute_group = function(data, scales, binwidth = NULL, bins = NULL, center = NULL, boundary = NULL, closed = c("right", "left"), pad = FALSE, - breaks = NULL, flipped_aes = FALSE, drop = "all", + breaks = NULL, flipped_aes = FALSE, drop = "none", # The following arguments are not used, but must # be listed so parameters are computed correctly origin = NULL, right = NULL) {