Skip to content

Simplify zeroGrob() #6391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -173,7 +173,6 @@ Collate:
'ggplot2-package.R'
'grob-absolute.R'
'grob-dotstack.R'
'grob-null.R'
'grouping.R'
'theme-elements.R'
'guide-.R'
5 changes: 0 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
26 changes: 0 additions & 26 deletions R/grob-null.R

This file was deleted.

11 changes: 11 additions & 0 deletions R/utilities-grid.R
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not removed this function altogether because there is some optimisation value in only constructing the .zeroGrob object once per session instead of all over the place. Moreover, I expect it might be prevalent in extensions.


.zeroGrob <- NULL
on_load(.zeroGrob <- add_class(nullGrob(), "zeroGrob"))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The zeroGrob class is appended here, because inherits(x, "zeroGrob") is sometimes used instead of is.zero(). I expect the situation to be the same in extension packages.


is.zero <- function(x) is.null(x) || inherits(x, "null")

width_cm <- function(x) {
if (is.grob(x)) {
convertWidth(grobWidth(x), "cm", TRUE)
9 changes: 9 additions & 0 deletions R/utilities.R
Original file line number Diff line number Diff line change
@@ -959,3 +959,12 @@
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)

Check warning on line 966 in R/utilities.R

Codecov / codecov/patch

R/utilities.R#L964-L966

Added lines #L964 - L966 were not covered by tests
}
class(x) <- union(new_class, class(x))
x

Check warning on line 969 in R/utilities.R

Codecov / codecov/patch

R/utilities.R#L968-L969

Added lines #L968 - L969 were not covered by tests
}
10 changes: 5 additions & 5 deletions man/stat_connect.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/zeroGrob.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
2 changes: 1 addition & 1 deletion tests/testthat/test-facet-strips.R
Original file line number Diff line number Diff line change
@@ -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", {
2 changes: 1 addition & 1 deletion tests/testthat/test-stat-density2d.R
Original file line number Diff line number Diff line change
@@ -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")
})
Loading
Oops, something went wrong.