1context("Facet Strips") 2 3strip_layout <- function(p) { 4 data <- ggplot_build(p) 5 plot <- data$plot 6 layout <- data$layout 7 data <- data$data 8 theme <- plot_theme(plot) 9 10 geom_grobs <- Map(function(l, d) l$draw_geom(d, layout), plot$layers, data) 11 12 facet <- layout$render(geom_grobs, data, theme, plot$labels) 13 layout <- facet$layout 14 strip_layout <- layout[grepl("^strip", layout$name), 1:4] 15 as.list(strip_layout) 16} 17 18p <- ggplot(mtcars, aes(disp, drat)) + geom_point() 19 20 21test_that("facet_wrap() builds correct output", { 22 wrap <- p + facet_wrap(~cyl) 23 24 wrap_expected <- list( 25 t = c(3, 3, 3), 26 l = c(3, 7, 11), 27 b = c(3, 3, 3), 28 r = c(3, 7, 11) 29 ) 30 31 expect_equal(strip_layout(wrap), wrap_expected) 32}) 33 34test_that("facet_wrap() switches to 'bottom'", { 35 wrap_b <- p + facet_wrap(~cyl, strip.position = "bottom") 36 37 wrap_b_expected <- list( 38 t = c(4, 4, 4), 39 l = c(3, 7, 11), 40 b = c(4, 4, 4), 41 r = c(3, 7, 11) 42 ) 43 44 expect_equal(strip_layout(wrap_b), wrap_b_expected) 45}) 46 47test_that("facet_wrap() switches to 'left'", { 48 wrap_l <- p + facet_wrap(~cyl, strip.position = "left") 49 50 wrap_l_expected <- list( 51 t = c(3, 3, 3), 52 l = c(13, 8, 3), 53 b = c(3, 3, 3), 54 r = c(13, 8, 3) 55 ) 56 57 expect_equal(strip_layout(wrap_l), wrap_l_expected) 58}) 59 60test_that("facet_wrap() switches to 'right'", { 61 wrap_r <- p + facet_wrap(~cyl, strip.position = "right") 62 63 wrap_r_expected <- list( 64 t = c(3, 3, 3), 65 l = c(14, 9, 4), 66 b = c(3, 3, 3), 67 r = c(14, 9, 4) 68 ) 69 70 expect_equal(strip_layout(wrap_r), wrap_r_expected) 71}) 72 73test_that("facet_grid() builds correct output", { 74 grid <- p + facet_grid(~cyl) 75 76 grid_expected <- list( 77 t = c(3, 3, 3), 78 l = c(3, 5, 7), 79 b = c(3, 3, 3), 80 r = c(3, 5, 7) 81 ) 82 83 expect_equal(strip_layout(grid), grid_expected) 84}) 85 86test_that("facet_grid() switches to 'x'", { 87 grid_x <- p + facet_grid(am ~ cyl, switch = "x") 88 89 grid_x_expected <- list( 90 t = c(6, 6, 6, 3, 5), 91 l = c(3, 5, 7, 8, 8), 92 b = c(6, 6, 6, 3, 5), 93 r = c(3, 5, 7, 8, 8) 94 ) 95 96 expect_equal(strip_layout(grid_x), grid_x_expected) 97}) 98 99test_that("facet_grid() switches to 'y'", { 100 grid_y <- p + facet_grid(am ~ cyl, switch = "y") 101 102 grid_y_expected <- list( 103 t = c(3, 3, 3, 4, 6), 104 l = c(4, 6, 8, 3, 3), 105 b = c(3, 3, 3, 4, 6), 106 r = c(4, 6, 8, 3, 3) 107 ) 108 109 expect_equal(strip_layout(grid_y), grid_y_expected) 110}) 111 112test_that("facet_grid() switches to both 'x' and 'y'", { 113 grid_xy <- p + facet_grid(am ~ cyl, switch = "both") 114 115 grid_xy_expected <- list( 116 t = c(6, 6, 6, 3, 5), 117 l = c(4, 6, 8, 3, 3), 118 b = c(6, 6, 6, 3, 5), 119 r = c(4, 6, 8, 3, 3) 120 ) 121 122 expect_equal(strip_layout(grid_xy), grid_xy_expected) 123}) 124 125test_that("strips can be removed", { 126 dat <- data_frame(a = rep(LETTERS[1:10], 10), x = rnorm(100), y = rnorm(100)) 127 g <- ggplot(dat, aes(x = x, y = y)) + 128 geom_point() + 129 facet_wrap(~a) + 130 theme(strip.background = element_blank(), strip.text = element_blank()) 131 g_grobs <- ggplotGrob(g) 132 strip_grobs <- g_grobs$grobs[grepl('strip-', g_grobs$layout$name)] 133 expect_true(all(sapply(strip_grobs, inherits, 'zeroGrob'))) 134}) 135 136test_that("y strip labels are rotated when strips are switched", { 137 switched <- p + facet_grid(am ~ cyl, switch = "both") 138 139 expect_doppelganger("switched facet strips", switched) 140}) 141