Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion R/guides-.R
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,8 @@ Guides <- ggproto(
direction = directions[i], params = params[[i]]
)
}
split(grobs, positions)
keep <- !vapply(grobs, is.zero, logical(1))
split(grobs[keep], positions[keep])
},

package_box = function(grobs, position, theme) {
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,30 @@ test_that("guide_legend uses key.spacing correctly", {
expect_doppelganger("legend with widely spaced keys", p)
})

test_that("empty guides are dropped", {

df <- data.frame(x = 1:2)
# Making a guide where all breaks are out-of-bounds
p <- ggplot(df, aes(x, x, colour = x)) +
geom_point() +
scale_colour_continuous(
limits = c(0.25, 0.75),
breaks = c(1, 2),
guide = "legend"
)
p <- ggplot_build(p)

# Empty guide that survives most steps
gd <- get_guide_data(p, "colour")
expect_equal(nrow(gd), 0)

# Draw guides
guides <- p$plot$guides$draw(theme_gray(), direction = "vertical")

# All guide-boxes should be empty
expect_equal(lengths(guides, use.names = FALSE), rep(0, 5))
})

# Visual tests ------------------------------------------------------------

test_that("axis guides are drawn correctly", {
Expand Down