1context("geom_boxplot") 2 3# thanks wch for providing the test code 4test_that("geom_boxplot range includes all outliers", { 5 dat <- data_frame(x = 1, y = c(-(1:20) ^ 3, (1:20) ^ 3) ) 6 p <- ggplot_build(ggplot(dat, aes(x,y)) + geom_boxplot()) 7 8 miny <- p$layout$panel_params[[1]]$y.range[1] 9 maxy <- p$layout$panel_params[[1]]$y.range[2] 10 11 expect_true(miny <= min(dat$y)) 12 expect_true(maxy >= max(dat$y)) 13}) 14 15test_that("geom_boxplot works in both directions", { 16 dat <- data_frame(x = 1, y = c(-(1:20) ^ 3, (1:20) ^ 3) ) 17 18 p <- ggplot(dat, aes(x, y)) + geom_boxplot() 19 x <- layer_data(p) 20 expect_false(x$flipped_aes[1]) 21 22 p <- ggplot(dat, aes(y, x)) + geom_boxplot() 23 y <- layer_data(p) 24 expect_true(y$flipped_aes[1]) 25 26 x$flipped_aes <- NULL 27 y$flipped_aes <- NULL 28 expect_identical(x, flip_data(y, TRUE)) 29}) 30 31test_that("geom_boxplot for continuous x gives warning if more than one x (#992)", { 32 dat <- expand.grid(x = 1:2, y = c(-(1:5) ^ 3, (1:5) ^ 3) ) 33 34 bplot <- function(aes = NULL, extra = list()) { 35 ggplot_build(ggplot(dat, aes) + geom_boxplot(aes) + extra) 36 } 37 38 expect_warning(bplot(aes(x, y)), "Continuous x aesthetic") 39 expect_warning(bplot(aes(x, y), facet_wrap(~x)), "Continuous x aesthetic") 40 expect_warning(bplot(aes(Sys.Date() + x, y)), "Continuous x aesthetic") 41 42 expect_warning(bplot(aes(x, group = x, y)), NA) 43 expect_warning(bplot(aes(1, y)), NA) 44 expect_warning(bplot(aes(factor(x), y)), NA) 45 expect_warning(bplot(aes(x == 1, y)), NA) 46 expect_warning(bplot(aes(as.character(x), y)), NA) 47}) 48 49test_that("can use US spelling of colour", { 50 df <- data_frame(x = 1, y = c(1:5, 100)) 51 plot <- ggplot(df, aes(x, y)) + geom_boxplot(outlier.color = "red") 52 53 gpar <- layer_grob(plot)[[1]]$children[[1]]$children[[1]]$gp 54 expect_equal(gpar$col, "#FF0000FF") 55}) 56 57test_that("boxes with variable widths do not overlap", { 58 df <- data_frame( 59 value = 1:12, 60 group = rep(c("a", "b", "c"), each = 4L), 61 subgroup = rep(c("A", "B"), times = 6L) 62 ) 63 64 p <- ggplot(df, aes(group, value, colour = subgroup)) + 65 geom_boxplot(varwidth = TRUE) 66 d <- layer_data(p)[c("xmin", "xmax")] 67 xid <- find_x_overlaps(d) 68 69 expect_false(any(duplicated(xid))) 70}) 71 72test_that("boxplots with a group size >1 error", { 73 p <- ggplot( 74 data_frame(x = "one value", y = 3, value = 4:6), 75 aes(x, ymin = 0, lower = 1, middle = y, upper = value, ymax = 10) 76 ) + 77 geom_boxplot(stat = "identity") 78 79 expect_equal(nrow(layer_data(p, 1)), 3) 80 expect_error(layer_grob(p, 1), "Can't draw more than one boxplot") 81}) 82 83# Visual tests ------------------------------------------------------------ 84 85test_that("boxplot draws correctly", { 86 expect_doppelganger("outlier colours", 87 ggplot(mtcars, aes(x = factor(cyl), y = drat, colour = factor(cyl))) + geom_boxplot(outlier.size = 5) 88 ) 89}) 90