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