1context('Empty data') 2 3df0 <- data_frame(mpg = numeric(0), wt = numeric(0), am = numeric(0), cyl = numeric(0)) 4 5test_that("layers with empty data are silently omitted", { 6 # Empty data (no visible points) 7 d <- ggplot(df0, aes(mpg,wt)) + geom_point() 8 expect_equal(nrow(layer_data(d)), 0) 9 10 d <- ggplot() + geom_point(data = df0, aes(mpg,wt)) 11 expect_equal(nrow(layer_data(d)), 0) 12 13 # Regular mtcars data, x=mpg, y=wt, normal points and points from empty data frame 14 d <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + geom_point(data = df0) 15 expect_equal(nrow(layer_data(d, 1)), nrow(mtcars)) 16 expect_equal(nrow(layer_data(d, 2)), 0) 17 18 # Regular mtcars data, but points only from empty data frame 19 d <- ggplot(mtcars, aes(mpg, wt)) + geom_point(data = df0) 20 expect_equal(nrow(layer_data(d, 1)), 0) 21}) 22 23test_that("plots with empty data and vectors for aesthetics work", { 24 d <- ggplot(NULL, aes(1:5, 1:5)) + geom_point() 25 expect_equal(nrow(layer_data(d)), 5) 26 27 d <- ggplot(data_frame(), aes(1:5, 1:5)) + geom_point() 28 expect_equal(nrow(layer_data(d)), 5) 29 30 d <- ggplot() + geom_point(aes(1:5, 1:5)) 31 expect_equal(nrow(layer_data(d)), 5) 32}) 33 34test_that("layers with empty data are silently omitted with facet_wrap", { 35 # Empty data, facet_wrap, throws error 36 d <- ggplot(df0, aes(mpg, wt)) + 37 geom_point() + 38 facet_wrap(~cyl) 39 expect_error(layer_data(d), "must have at least one value") 40 41 d <- d + geom_point(data = mtcars) 42 expect_equal(nrow(layer_data(d, 1)), 0) 43 expect_equal(nrow(layer_data(d, 2)), nrow(mtcars)) 44}) 45 46test_that("layers with empty data are silently omitted with facet_grid", { 47 d <- ggplot(df0, aes(mpg, wt)) + 48 geom_point() + 49 facet_grid(am ~ cyl) 50 expect_error(layer_data(d), "must have at least one value") 51 52 d <- d + geom_point(data = mtcars) 53 expect_equal(nrow(layer_data(d, 1)), 0) 54 expect_equal(nrow(layer_data(d, 2)), nrow(mtcars)) 55}) 56 57test_that("empty data overrides plot defaults", { 58 # Should error when totally empty data frame because there's no x and y 59 d <- ggplot(mtcars, aes(mpg, wt)) + 60 geom_point() + 61 geom_point(data = data_frame()) 62 expect_error(layer_data(d), "not found") 63 64 # No extra points when x and y vars don't exist but are set 65 d <- ggplot(mtcars, aes(mpg, wt)) + 66 geom_point() + 67 geom_point(data = data_frame(), x = 20, y = 3) 68 expect_equal(nrow(layer_data(d, 1)), nrow(mtcars)) 69 expect_equal(nrow(layer_data(d, 2)), 0) 70 71 # No extra points when x and y vars are empty, even when aesthetics are set 72 d <- ggplot(mtcars, aes(mpg, wt)) + 73 geom_point() + 74 geom_point(data = df0, x = 20, y = 3) 75 expect_equal(nrow(layer_data(d, 1)), nrow(mtcars)) 76 expect_equal(nrow(layer_data(d, 2)), 0) 77}) 78 79test_that("layer inherits data from plot when data = NULL", { 80 d <- ggplot(mtcars, aes(mpg, wt)) + 81 geom_point(data = NULL) 82 expect_equal(nrow(layer_data(d)), nrow(mtcars)) 83}) 84 85test_that("empty layers still generate one grob per panel", { 86 df <- data_frame(x = 1:3, y = c("a", "b", "c")) 87 88 d <- ggplot(df, aes(x, y)) + 89 geom_point(data = df[0, ]) + 90 geom_point() + 91 facet_wrap(~y) 92 93 expect_equal(length(layer_grob(d)), 3) 94}) 95 96test_that("missing layers generate one grob per panel", { 97 df <- data_frame(x = 1:4, y = rep(1:2, 2), g = rep(1:2, 2)) 98 base <- ggplot(df, aes(x, y)) + geom_point(shape = NA, na.rm = TRUE) 99 100 expect_equal(length(layer_grob(base)), 1) 101 expect_equal(length(layer_grob(base + facet_wrap(~ g))), 2) 102}) 103