Skip to content

Commit 9f1ca5b

Browse files
committed
Merge pull request #1559 from lionel-/switch-bugfix
Fix labels-switching in facet_wrap()
2 parents b0f10d0 + 86ff9be commit 9f1ca5b

File tree

2 files changed

+127
-5
lines changed

2 files changed

+127
-5
lines changed

R/facet-wrap.r

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,22 +345,32 @@ facet_strips.wrap <- function(facet, panel, theme) {
345345

346346
# Adding labels metadata, useful for labellers
347347
attr(labels_df, "facet") <- "wrap"
348-
if (!is.null(facet$switch) && facet$switch == "x") {
348+
if (is.null(facet$switch) || facet$switch == "x") {
349349
dir <- "b"
350350
attr(labels_df, "type") <- "rows"
351351
} else {
352-
dir <- "t"
352+
dir <- "l"
353353
attr(labels_df, "type") <- "cols"
354354
}
355355

356356
strips_table <- build_strip(panel, labels_df, facet$labeller,
357357
theme, dir, switch = facet$switch)
358358

359359
# While grid facetting works with a whole gtable, wrap processes the
360-
# columns separately. So we turn the gtable into a list of columns
361-
strips <- list(t = vector("list", ncol(strips_table)))
360+
# strips separately. So we turn the gtable into a list
361+
if (dir == "b") {
362+
n_strips <- ncol(strips_table)
363+
} else {
364+
n_strips <- nrow(strips_table)
365+
}
366+
367+
strips <- list(t = vector("list", n_strips))
362368
for (i in seq_along(strips$t)) {
363-
strips$t[[i]] <- strips_table[, i]
369+
if (dir == "b") {
370+
strips$t[[i]] <- strips_table[, i]
371+
} else {
372+
strips$t[[i]] <- strips_table[i, ]
373+
}
364374
}
365375
strips
366376
}

tests/testthat/test-facet-strips.r

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
context("Facet Strips")
2+
3+
strip_layout <- function(p) {
4+
data <- ggplot_build(p)
5+
plot <- data$plot
6+
panel <- data$panel
7+
data <- data$data
8+
theme <- plot_theme(plot)
9+
10+
geom_grobs <- Map(function(l, d) l$draw_geom(d, panel, plot$coordinates),
11+
plot$layers, data)
12+
13+
facet <- facet_render(plot$facet, panel, plot$coordinates, theme, geom_grobs)
14+
layout <- facet$layout
15+
strip_layout <- layout[grepl("^strip", layout$name), 1:4]
16+
as.list(strip_layout)
17+
}
18+
19+
p <- ggplot(mtcars, aes(disp, drat)) + geom_point()
20+
21+
22+
test_that("facet_wrap() builds correct output", {
23+
wrap <- p + facet_wrap(~cyl)
24+
25+
wrap_expected <- list(
26+
t = c(1, 1, 1),
27+
l = c(2, 5, 8),
28+
b = c(1, 1, 1),
29+
r = c(2, 5, 8)
30+
)
31+
32+
expect_equal(strip_layout(wrap), wrap_expected)
33+
})
34+
35+
test_that("facet_wrap() switches to 'x'", {
36+
wrap_x <- p + facet_wrap(~cyl, switch = "x")
37+
38+
wrap_x_expected <- list(
39+
t = c(3, 3, 3),
40+
l = c(2, 5, 8),
41+
b = c(3, 3, 3),
42+
r = c(2, 5, 8)
43+
)
44+
45+
expect_equal(strip_layout(wrap_x), wrap_x_expected)
46+
})
47+
48+
test_that("facet_wrap() switches to 'y'", {
49+
wrap_y <- p + facet_wrap(~cyl, switch = "y")
50+
51+
wrap_y_expected <- list(
52+
t = c(1, 1, 1),
53+
l = c(1, 5, 9),
54+
b = c(1, 1, 1),
55+
r = c(1, 5, 9)
56+
)
57+
58+
expect_equal(strip_layout(wrap_y), wrap_y_expected)
59+
})
60+
61+
62+
test_that("facet_grid() builds correct output", {
63+
grid <- p + facet_grid(~cyl)
64+
65+
grid_expected <- list(
66+
t = c(1, 1, 1),
67+
l = c(2, 4, 6),
68+
b = c(1, 1, 1),
69+
r = c(2, 4, 6)
70+
)
71+
72+
expect_equal(strip_layout(grid), grid_expected)
73+
})
74+
75+
test_that("facet_grid() switches to 'x'", {
76+
grid_x <- p + facet_grid(am ~ cyl, switch = "x")
77+
78+
grid_x_expected <- list(
79+
t = c(1, 3, 5),
80+
l = c(7, 7, 2),
81+
b = c(1, 3, 6),
82+
r = c(7, 7, 6)
83+
)
84+
85+
expect_equal(strip_layout(grid_x), grid_x_expected)
86+
})
87+
88+
test_that("facet_grid() switches to 'y'", {
89+
grid_y <- p + facet_grid(am ~ cyl, switch = "y")
90+
91+
grid_y_expected <- list(
92+
t = c(1, 1, 1, 2),
93+
l = c(4, 6, 8, 1),
94+
b = c(1, 1, 1, 4),
95+
r = c(4, 6, 8, 2)
96+
)
97+
98+
expect_equal(strip_layout(grid_y), grid_y_expected)
99+
})
100+
101+
test_that("facet_grid() switches to both 'x' and 'y'", {
102+
grid_xy <- p + facet_grid(am ~ cyl, switch = "both")
103+
104+
grid_xy_expected <- list(
105+
t = c(1, 5),
106+
l = c(1, 4),
107+
b = c(3, 6),
108+
r = c(2, 8)
109+
)
110+
111+
expect_equal(strip_layout(grid_xy), grid_xy_expected)
112+
})

0 commit comments

Comments
 (0)