Skip to content

Commit d5c2f1f

Browse files
authored
Drop zeroGrob() guides (#5645)
* drop empty guides * add test
1 parent 2d1f31c commit d5c2f1f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

R/guides-.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,8 @@ Guides <- ggproto(
547547
direction = directions[i], params = params[[i]]
548548
)
549549
}
550-
split(grobs, positions)
550+
keep <- !vapply(grobs, is.zero, logical(1))
551+
split(grobs[keep], positions[keep])
551552
},
552553

553554
package_box = function(grobs, position, theme) {

tests/testthat/test-guides.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,30 @@ test_that("guide_legend uses key.spacing correctly", {
477477
expect_doppelganger("legend with widely spaced keys", p)
478478
})
479479

480+
test_that("empty guides are dropped", {
481+
482+
df <- data.frame(x = 1:2)
483+
# Making a guide where all breaks are out-of-bounds
484+
p <- ggplot(df, aes(x, x, colour = x)) +
485+
geom_point() +
486+
scale_colour_continuous(
487+
limits = c(0.25, 0.75),
488+
breaks = c(1, 2),
489+
guide = "legend"
490+
)
491+
p <- ggplot_build(p)
492+
493+
# Empty guide that survives most steps
494+
gd <- get_guide_data(p, "colour")
495+
expect_equal(nrow(gd), 0)
496+
497+
# Draw guides
498+
guides <- p$plot$guides$draw(theme_gray(), direction = "vertical")
499+
500+
# All guide-boxes should be empty
501+
expect_equal(lengths(guides, use.names = FALSE), rep(0, 5))
502+
})
503+
480504
# Visual tests ------------------------------------------------------------
481505

482506
test_that("axis guides are drawn correctly", {

0 commit comments

Comments
 (0)