|
| 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