Skip to content

Commit b41e1b9

Browse files
Ignore empty layers when facetting (#3868)
Fix #3853
1 parent bca6105 commit b41e1b9

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* Data columns can now contain `Vector` S4 objects, which are widely used in the
1717
Bioconductor project. (@teunbrand, #3837)
1818

19+
* Facets now handle layers containing no data (@yutannihilation, #3853).
20+
1921
# ggplot2 3.3.0
2022

2123
This is a minor release but does contain a range of substantial new features,

R/utilities.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ snake_class <- function(x) {
278278
}
279279

280280
empty <- function(df) {
281-
is.null(df) || nrow(df) == 0 || ncol(df) == 0
281+
is.null(df) || nrow(df) == 0 || ncol(df) == 0 || is.waive(df)
282282
}
283283

284284
is.discrete <- function(x) {

tests/testthat/test-facet-.r

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ test_that("wrap_as_facets_list() and grid_as_facets_list() accept empty specs",
7474
expect_identical(grid_as_facets_list(list(NULL), NULL), list(rows = quos(), cols = quos()))
7575
})
7676

77-
df <- data_frame(x = 1:3, y = 3:1, z = letters[1:3])
78-
7977
test_that("facets split up the data", {
80-
l1 <- ggplot(df, aes(x, y)) + geom_point() + facet_wrap(~z)
81-
l2 <- ggplot(df, aes(x, y)) + geom_point() + facet_grid(. ~ z)
82-
l3 <- ggplot(df, aes(x, y)) + geom_point() + facet_grid(z ~ .)
78+
df <- data_frame(x = 1:3, y = 3:1, z = letters[1:3])
79+
p <- ggplot(df, aes(x, y)) + geom_point()
80+
81+
l1 <- p + facet_wrap(~z)
82+
l2 <- p + facet_grid(. ~ z)
83+
l3 <- p + facet_grid(z ~ .)
8384

8485
d1 <- layer_data(l1)
8586
d2 <- layer_data(l2)
@@ -88,11 +89,23 @@ test_that("facets split up the data", {
8889
expect_equal(d1, d2)
8990
expect_equal(d1, d3)
9091
expect_equal(d1$PANEL, factor(1:3))
92+
93+
# Handle empty layers
94+
p_empty <- ggplot() + geom_point(aes(x, y), df) + geom_line()
95+
l4 <- p_empty + facet_wrap(~z)
96+
l5 <- p_empty + facet_grid(. ~ z)
97+
98+
d4 <- layer_data(l4)
99+
d5 <- layer_data(l5)
100+
101+
expect_equal(d1, d4)
102+
expect_equal(d1, d5)
91103
})
92104

105+
93106
test_that("facet_wrap() accepts vars()", {
107+
df <- data_frame(x = 1:3, y = 3:1, z = letters[1:3])
94108
p <- ggplot(df, aes(x, y)) + geom_point()
95-
p2 <- p + facet_wrap(vars(z))
96109

97110
p1 <- p + facet_wrap(~z)
98111
p2 <- p + facet_wrap(vars(Z = z), labeller = label_both)
@@ -136,44 +149,52 @@ test_that("vars() accepts optional names", {
136149
expect_named(wrap$params$facets, c("A", "b"))
137150
})
138151

139-
test_that("facets_wrap() compacts the facet spec and accept empty spec", {
140-
p <- ggplot(df, aes(x, y)) + geom_point() + facet_wrap(vars(NULL))
141-
d <- layer_data(p)
152+
test_that("facet_wrap()/facet_grid() compact the facet spec, and accept empty spec", {
153+
df <- data_frame(x = 1:3, y = 3:1, z = letters[1:3])
154+
p <- ggplot(df, aes(x, y)) + geom_point()
142155

143-
expect_equal(d$PANEL, factor(c(1L, 1L, 1L)))
144-
expect_equal(d$group, structure(c(-1L, -1L, -1L), n = 1L))
145-
})
156+
# facet_wrap()
157+
p_wrap <- p + facet_wrap(vars(NULL))
158+
d_wrap <- layer_data(p_wrap)
159+
160+
expect_equal(d_wrap$PANEL, factor(c(1L, 1L, 1L)))
161+
expect_equal(d_wrap$group, structure(c(-1L, -1L, -1L), n = 1L))
146162

147-
test_that("facets_grid() compacts the facet spec and accept empty spec", {
148-
p <- ggplot(df, aes(x, y)) + geom_point() + facet_grid(vars(NULL))
149-
d <- layer_data(p)
163+
# facet_grid()
164+
p_grid <- p + facet_grid(vars(NULL))
165+
d_grid <- layer_data(p_grid)
150166

151-
expect_equal(d$PANEL, factor(c(1L, 1L, 1L)))
152-
expect_equal(d$group, structure(c(-1L, -1L, -1L), n = 1L))
167+
expect_equal(d_grid$PANEL, factor(c(1L, 1L, 1L)))
168+
expect_equal(d_grid$group, structure(c(-1L, -1L, -1L), n = 1L))
153169
})
154170

155171

156172
test_that("facets with free scales scale independently", {
157-
l1 <- ggplot(df, aes(x, y)) + geom_point() +
158-
facet_wrap(~z, scales = "free")
173+
df <- data_frame(x = 1:3, y = 3:1, z = letters[1:3])
174+
p <- ggplot(df, aes(x, y)) + geom_point()
175+
176+
# facet_wrap()
177+
l1 <- p + facet_wrap(~z, scales = "free")
159178
d1 <- cdata(l1)[[1]]
160179
expect_true(sd(d1$x) < 1e-10)
161180
expect_true(sd(d1$y) < 1e-10)
162181

163-
l2 <- ggplot(df, aes(x, y)) + geom_point() +
164-
facet_grid(. ~ z, scales = "free")
182+
# RHS of facet_grid()
183+
l2 <- p + facet_grid(. ~ z, scales = "free")
165184
d2 <- cdata(l2)[[1]]
166185
expect_true(sd(d2$x) < 1e-10)
167186
expect_equal(length(unique(d2$y)), 3)
168187

169-
l3 <- ggplot(df, aes(x, y)) + geom_point() +
170-
facet_grid(z ~ ., scales = "free")
188+
# LHS of facet_grid()
189+
l3 <- p + facet_grid(z ~ ., scales = "free")
171190
d3 <- cdata(l3)[[1]]
172191
expect_equal(length(unique(d3$x)), 3)
173192
expect_true(sd(d3$y) < 1e-10)
174193
})
175194

176195
test_that("shrink parameter affects scaling", {
196+
df <- data_frame(x = 1:3, y = 3:1, z = letters[1:3])
197+
177198
l1 <- ggplot(df, aes(1, y)) + geom_point()
178199
r1 <- pranges(l1)
179200

0 commit comments

Comments
 (0)