1
2context("stat-contour")
3
4test_that("a warning is issued when there is more than one z per x+y", {
5  tbl <- data_frame(x = c(1, 1, 2), y = c(1, 1, 2), z = 3)
6  p <- ggplot(tbl, aes(x, y, z = z)) + geom_contour()
7  expect_warning(ggplot_build(p), "Zero contours were generated")
8})
9
10test_that("contouring sparse data results in a warning", {
11  tbl <- data_frame(x = c(1, 27, 32), y = c(1, 1, 30), z = c(1, 2, 3))
12  p <- ggplot(tbl, aes(x, y, z = z)) + geom_contour()
13  expect_warning(ggplot_build(p), "Zero contours were generated")
14})
15
16test_that("contouring irregularly spaced data works", {
17  tbl <- expand.grid(x = c(1, 10, 100, 1000), y = 1:3)
18  tbl$z <- 1
19  tbl[c(6, 7), ]$z <- 10
20  p <- ggplot(tbl, aes(x, y, z = z)) + geom_contour(breaks = c(4, 8))
21
22  # we're testing for set equality here because contour lines are not
23  # guaranteed to start and end at the same point on all architectures
24  d <- layer_data(p)
25  d4 <- d[d$level == 4,]
26  expect_equal(nrow(d4), 7)
27  expect_setequal(d4$x, c(4, 10, 100, 700))
28  expect_setequal(d4$y, c(2, 8/3, 4/3))
29  d8 <- d[d$level == 8,]
30  expect_equal(nrow(d8), 7)
31  expect_setequal(d8$x, c(8, 10, 100, 300))
32  expect_setequal(d8$y, c(2, 20/9, 16/9))
33})
34
35test_that("contour breaks can be set manually and by bins and binwidth", {
36  range <- c(0, 1)
37  expect_equal(contour_breaks(range), pretty(range, 10))
38  expect_identical(contour_breaks(range, breaks = 1:3), 1:3)
39  expect_length(contour_breaks(range, bins = 5), 6)
40  # shifting the range by 0.2 hits another execution branch in contour_breaks()
41  expect_length(contour_breaks(range + 0.2, bins = 5), 6)
42  expect_equal(resolution(contour_breaks(range, binwidth = 0.3)), 0.3)
43})
44
45test_that("geom_contour_filled() and stat_contour_filled() result in identical layer data", {
46  p <- ggplot(faithfuld, aes(waiting, eruptions, z = density))
47  p1 <- p + stat_contour_filled()
48  p2 <- p + geom_contour_filled()
49  expect_identical(layer_data(p1), layer_data(p2))
50})
51
52test_that("geom_contour() and stat_contour() result in identical layer data", {
53  p <- ggplot(faithfuld, aes(waiting, eruptions, z = density))
54  p1 <- p + stat_contour()
55  p2 <- p + geom_contour()
56  expect_identical(layer_data(p1), layer_data(p2))
57})
58
59test_that("basic stat_contour() plot builds", {
60  p <- ggplot(faithfuld, aes(waiting, eruptions)) +
61    geom_contour(aes(z = density, col = factor(stat(level))))
62
63  # stat_contour() visual tests are unstable due to the
64  # implementation in isoband
65  expect_silent(ggplot_build(p))
66})
67
68test_that("basic stat_contour_filled() plot builds", {
69  p <- ggplot(faithfuld, aes(waiting, eruptions)) +
70    stat_contour_filled(aes(z = density))
71
72  # stat_contour() visual tests are unstable due to the
73  # implementation in isoband
74  expect_silent(ggplot_build(p))
75})
76