From 59dc42e51d4b8505e087be2a7860d1c110f89192 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 2 Apr 2025 10:08:03 +0200 Subject: [PATCH 1/5] cleanup branch --- man/stat_connect.Rd | 10 +++++----- .../{scale-colour-continuous.md => scale-colour.md} | 0 2 files changed, 5 insertions(+), 5 deletions(-) rename tests/testthat/_snaps/{scale-colour-continuous.md => scale-colour.md} (100%) diff --git a/man/stat_connect.Rd b/man/stat_connect.Rd index d62ba610a5..24166744d8 100644 --- a/man/stat_connect.Rd +++ b/man/stat_connect.Rd @@ -128,11 +128,11 @@ Connect successive points with lines of different shapes. } \section{Aesthetics}{ -\code{stat_connect()} understands the following aesthetics (required aesthetics are in bold): -\itemize{ -\item \strong{\code{\link[=aes_position]{x}} \emph{or} \code{\link[=aes_position]{xmin}} \emph{or} \code{\link[=aes_position]{xmax}}} -\item \strong{\code{\link[=aes_position]{y}} \emph{or} \code{\link[=aes_position]{ymin}} \emph{or} \code{\link[=aes_position]{ymax}}} -\item \code{\link[=aes_group_order]{group}} +\code{stat_connect()} understands the following aesthetics. Required aesthetics are displayed in bold and defaults are displayed for optional aesthetics: +\tabular{rll}{ +• \tab \strong{\code{\link[=aes_position]{x}} \emph{or} \code{\link[=aes_position]{xmin}} \emph{or} \code{\link[=aes_position]{xmax}}} \tab \cr\cr +• \tab \strong{\code{\link[=aes_position]{y}} \emph{or} \code{\link[=aes_position]{ymin}} \emph{or} \code{\link[=aes_position]{ymax}}} \tab \cr\cr +• \tab \code{\link[=aes_group_order]{group}} \tab → inferred \cr\cr } Learn more about setting these aesthetics in \code{vignette("ggplot2-specs")}. } diff --git a/tests/testthat/_snaps/scale-colour-continuous.md b/tests/testthat/_snaps/scale-colour.md similarity index 100% rename from tests/testthat/_snaps/scale-colour-continuous.md rename to tests/testthat/_snaps/scale-colour.md From 3fcfbe374617cf04b93c3d8d6880ce1f1dae8c8d Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 2 Apr 2025 10:13:26 +0200 Subject: [PATCH 2/5] For backward compatibility, `zeroGrob()` returns a `nullGrob()` subclass --- NAMESPACE | 5 ----- R/grob-null.R | 20 ++------------------ R/utilities.R | 9 +++++++++ 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 088da2ba6c..b0d3fc0713 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -19,7 +19,6 @@ S3method(as.list,ggproto) S3method(autolayer,default) S3method(autoplot,default) S3method(c,mapped_discrete) -S3method(drawDetails,zeroGrob) S3method(element_grob,element_blank) S3method(element_grob,element_line) S3method(element_grob,element_point) @@ -76,9 +75,7 @@ S3method(ggplot_gtable,ggplot_built) S3method(grid.draw,absoluteGrob) S3method(grid.draw,ggplot) S3method(grobHeight,absoluteGrob) -S3method(grobHeight,zeroGrob) S3method(grobWidth,absoluteGrob) -S3method(grobWidth,zeroGrob) S3method(grobX,absoluteGrob) S3method(grobY,absoluteGrob) S3method(guide_gengrob,default) @@ -87,7 +84,6 @@ S3method(guide_merge,default) S3method(guide_train,default) S3method(guide_transform,default) S3method(heightDetails,titleGrob) -S3method(heightDetails,zeroGrob) S3method(limits,Date) S3method(limits,POSIXct) S3method(limits,POSIXlt) @@ -149,7 +145,6 @@ S3method(vec_ptype2,mapped_discrete.factor) S3method(vec_ptype2,mapped_discrete.integer) S3method(vec_ptype2,mapped_discrete.mapped_discrete) S3method(widthDetails,titleGrob) -S3method(widthDetails,zeroGrob) export("%+%") export("%+replace%") export(.data) diff --git a/R/grob-null.R b/R/grob-null.R index 217c5a7560..193255a824 100644 --- a/R/grob-null.R +++ b/R/grob-null.R @@ -5,22 +5,6 @@ zeroGrob <- function() .zeroGrob .zeroGrob <- NULL -on_load(.zeroGrob <- grob(cl = "zeroGrob", name = "NULL")) +on_load(.zeroGrob <- add_class(nullGrob(), "zeroGrob")) -#' @export -#' @method widthDetails zeroGrob -widthDetails.zeroGrob <- function(x) unit(0, "cm") -#' @export -#' @method heightDetails zeroGrob -heightDetails.zeroGrob <- function(x) unit(0, "cm") -#' @export -#' @method grobWidth zeroGrob -grobWidth.zeroGrob <- function(x) unit(0, "cm") -#' @export -#' @method grobHeight zeroGrob -grobHeight.zeroGrob <- function(x) unit(0, "cm") -#' @export -#' @method drawDetails zeroGrob -drawDetails.zeroGrob <- function(x, recording) {} - -is.zero <- function(x) is.null(x) || inherits(x, "zeroGrob") +is.zero <- function(x) is.null(x) || inherits(x, "null") diff --git a/R/utilities.R b/R/utilities.R index 8f0672c142..96ebd1e59c 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -959,3 +959,12 @@ compute_data_size <- function(data, size, default = 0.9, data[[target]] <- res * (default %||% 0.9) data } + +add_class <- function(x, new_class) { + new_class <- setdiff(new_class, class(x)) + if (length(new_class) < 1) { + return(x) + } + class(x) <- union(new_class, class(x)) + x +} From 5d55ae88b14d1c091ad5120832a08956961eb5a7 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 2 Apr 2025 10:14:29 +0200 Subject: [PATCH 3/5] adjust tests --- tests/testthat/test-facet-strips.R | 2 +- tests/testthat/test-stat-density2d.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-facet-strips.R b/tests/testthat/test-facet-strips.R index d13f8d500c..8bc684dbe7 100644 --- a/tests/testthat/test-facet-strips.R +++ b/tests/testthat/test-facet-strips.R @@ -132,7 +132,7 @@ test_that("strips can be removed", { theme(strip.background = element_blank(), strip.text = element_blank()) g_grobs <- ggplotGrob(g) strip_grobs <- g_grobs$grobs[grepl('strip-', g_grobs$layout$name)] - expect_true(all(sapply(strip_grobs, inherits, 'zeroGrob'))) + expect_true(all(sapply(strip_grobs, inherits, 'null'))) }) test_that("padding is only added if axis is present", { diff --git a/tests/testthat/test-stat-density2d.R b/tests/testthat/test-stat-density2d.R index 43a99e9513..2e2896cc47 100644 --- a/tests/testthat/test-stat-density2d.R +++ b/tests/testthat/test-stat-density2d.R @@ -100,5 +100,5 @@ test_that("stat_density_2d handles faulty bandwidth", { p <- ggplot(faithful, aes(eruptions, waiting)) + stat_density_2d(h = c(0, NA)) expect_snapshot_warning(b <- ggplot_build(p)) - expect_s3_class(layer_grob(b)[[1]], "zeroGrob") + expect_s3_class(layer_grob(b)[[1]], "null") }) From a8306d36ca109d50a6909a9abd8d94954dd63bd5 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 2 Apr 2025 11:05:57 +0200 Subject: [PATCH 4/5] add news bullet --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 363a20cc28..a33dc3a529 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # ggplot2 (development version) +* (internal) `zeroGrob()` now returns a `grid::nullGrob()` (#6390). * `position_fill()` avoids stacking observations of zero (@teunbrand, #6338) * New `layer(layout)` argument to interact with facets (@teunbrand, #3062) * New `stat_connect()` to connect points via steps or other shapes From 674b6d0bd79453fcd7d1d775e51cbaac25b4067e Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 2 Apr 2025 11:08:02 +0200 Subject: [PATCH 5/5] migrate to grob utilities --- DESCRIPTION | 1 - R/grob-null.R | 10 ---------- R/utilities-grid.R | 11 +++++++++++ man/zeroGrob.Rd | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) delete mode 100644 R/grob-null.R diff --git a/DESCRIPTION b/DESCRIPTION index 82266c740f..83ca78ce8e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -173,7 +173,6 @@ Collate: 'ggplot2-package.R' 'grob-absolute.R' 'grob-dotstack.R' - 'grob-null.R' 'grouping.R' 'theme-elements.R' 'guide-.R' diff --git a/R/grob-null.R b/R/grob-null.R deleted file mode 100644 index 193255a824..0000000000 --- a/R/grob-null.R +++ /dev/null @@ -1,10 +0,0 @@ -#' The zero grob draws nothing and has zero size. -#' -#' @keywords internal -#' @export -zeroGrob <- function() .zeroGrob - -.zeroGrob <- NULL -on_load(.zeroGrob <- add_class(nullGrob(), "zeroGrob")) - -is.zero <- function(x) is.null(x) || inherits(x, "null") diff --git a/R/utilities-grid.R b/R/utilities-grid.R index a935d5b38f..3bd29e9ceb 100644 --- a/R/utilities-grid.R +++ b/R/utilities-grid.R @@ -48,6 +48,17 @@ gg_par <- function(..., stroke = NULL, pointsize = NULL) { inject(gpar(!!!args)) } +#' The zero grob draws nothing and has zero size. +#' +#' @keywords internal +#' @export +zeroGrob <- function() .zeroGrob + +.zeroGrob <- NULL +on_load(.zeroGrob <- add_class(nullGrob(), "zeroGrob")) + +is.zero <- function(x) is.null(x) || inherits(x, "null") + width_cm <- function(x) { if (is.grob(x)) { convertWidth(grobWidth(x), "cm", TRUE) diff --git a/man/zeroGrob.Rd b/man/zeroGrob.Rd index e4aac88eb0..6fcfe56218 100644 --- a/man/zeroGrob.Rd +++ b/man/zeroGrob.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/grob-null.R +% Please edit documentation in R/utilities-grid.R \name{zeroGrob} \alias{zeroGrob} \title{The zero grob draws nothing and has zero size.}