1context("geom_ribbon") 2 3test_that("NAs are not dropped from the data", { 4 df <- data_frame(x = 1:5, y = c(1, 1, NA, 1, 1)) 5 6 p <- ggplot(df, aes(x))+ 7 geom_ribbon(aes(ymin = y - 1, ymax = y + 1)) 8 9 expect_equal(layer_data(p)$ymin, c(0, 0, NA, 0, 0)) 10}) 11 12test_that("geom_ribbon works in both directions", { 13 dat <- data_frame(x = seq_len(5), 14 ymin = c(1, 2, 1.5, 1.8, 1), 15 ymax = c(4, 6, 5, 4.5, 5.2)) 16 17 p <- ggplot(dat, aes(x, ymin = ymin, ymax = ymax)) + geom_ribbon() 18 x <- layer_data(p) 19 expect_false(x$flipped_aes[1]) 20 21 p <- ggplot(dat, aes(y = x, xmin = ymin, xmax = ymax)) + geom_ribbon() 22 y <- layer_data(p) 23 expect_true(y$flipped_aes[1]) 24 25 x$flipped_aes <- NULL 26 y$flipped_aes <- NULL 27 expect_identical(x, flip_data(y, TRUE)[,names(x)]) 28}) 29 30test_that("outline.type option works", { 31 df <- data_frame(x = 1:4, y = c(1, 1, 1, 1)) 32 33 p <- ggplot(df, aes(x, ymin = -y, ymax = y)) 34 35 g_ribbon_default <- layer_grob(p + geom_ribbon())[[1]] 36 g_ribbon_upper <- layer_grob(p + geom_ribbon(outline.type = "upper"))[[1]] 37 g_ribbon_lower <- layer_grob(p + geom_ribbon(outline.type = "lower"))[[1]] 38 g_ribbon_full <- layer_grob(p + geom_ribbon(outline.type = "full"))[[1]] 39 g_area_default <- layer_grob(ggplot(df, aes(x, y)) + geom_area())[[1]] 40 41 # default 42 expect_s3_class(g_ribbon_default$children[[1]]$children[[1]], "polygon") 43 expect_s3_class(g_ribbon_default$children[[1]]$children[[2]], "polyline") 44 expect_equal(g_ribbon_default$children[[1]]$children[[2]]$id, rep(c(1L, 2L), each = 4)) 45 46 # upper 47 expect_s3_class(g_ribbon_upper$children[[1]]$children[[1]], "polygon") 48 expect_s3_class(g_ribbon_upper$children[[1]]$children[[2]], "polyline") 49 expect_equal(g_ribbon_upper$children[[1]]$children[[2]]$id, rep(1L, each = 4)) 50 51 # lower 52 expect_s3_class(g_ribbon_lower$children[[1]]$children[[1]], "polygon") 53 expect_s3_class(g_ribbon_lower$children[[1]]$children[[2]], "polyline") 54 expect_equal(g_ribbon_lower$children[[1]]$children[[2]]$id, rep(2L, each = 4)) 55 56 # full 57 expect_s3_class(g_ribbon_full$children[[1]], "polygon") 58 59 # geom_area()'s default is upper 60 expect_s3_class(g_area_default$children[[1]]$children[[1]], "polygon") 61 expect_s3_class(g_area_default$children[[1]]$children[[2]], "polyline") 62 expect_equal(g_area_default$children[[1]]$children[[2]]$id, rep(1L, each = 4)) 63}) 64