Skip to content

Commit 37fb4c2

Browse files
committed
Better computation for missing layers.
Add unit test
1 parent 2f270c0 commit 37fb4c2

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

R/geom-.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Geom <- ggproto("Geom",
6868

6969
draw_layer = function(self, data, params, panel, coord) {
7070
if (empty(data)) {
71-
n <- nlevels(data$PANEL)
71+
n <- if (is.factor(data$PANEL)) nlevels(data$PANEL) else 1L
7272
return(rep(list(zeroGrob()), n))
7373
}
7474

tests/testthat/test-empty-data.r

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,11 @@ test_that("empty layers still generate one grob per panel", {
9595

9696
expect_equal(length(layer_grob(d)), 3)
9797
})
98+
99+
test_that("missing layers generate one grob per panel", {
100+
df <- data.frame(x = 1:4, y = 1:2, g = 1:2)
101+
base <- ggplot(df, aes(x, y)) + geom_point(shape = NA, na.rm = TRUE)
102+
103+
expect_equal(length(layer_grob(base)), 1)
104+
expect_equal(length(layer_grob(base + facet_wrap(~ g))), 2)
105+
})

0 commit comments

Comments
 (0)