Skip to content

Commit 743bad0

Browse files
committed
unit tests
1 parent 0bf1261 commit 743bad0

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

R/stat-contour.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ stat_contour <- function(mapping = NULL, data = NULL,
4949
#' @rdname geom_contour
5050
#' @export
5151
stat_contour_filled <- function(mapping = NULL, data = NULL,
52-
geom = "polygon", position = "identity",
52+
geom = "contour_filled", position = "identity",
5353
...,
5454
bins = NULL,
5555
binwidth = NULL,

man/geom_contour.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-stat-density2d.R

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,72 @@ test_that("uses scale limits, not data limits", {
1616
expect_true(max(ret$y) > 35)
1717
})
1818

19-
# Visual tests --------------------------------------
20-
2119
test_that("stat_density2d can produce contour and raster data", {
2220
p <- ggplot(faithful, aes(x = eruptions, y = waiting))
2321

24-
p_contour <- p + stat_density_2d()
22+
p_contour_lines <- p + stat_density_2d()
23+
p_contour_bands <- p + stat_density_2d(contour_type = "bands")
2524
p_raster <- p + stat_density_2d(contour = FALSE)
2625

27-
expect_true("level" %in% names(layer_data(p_contour)))
28-
expect_true("density" %in% names(layer_data(p_raster)))
26+
d_lines <- layer_data(p_contour_lines)
27+
expect_true("level" %in% names(d_lines))
28+
expect_false("level_low" %in% names(d_lines))
29+
expect_true(is.numeric(d_lines$level))
30+
31+
d_bands <- layer_data(p_contour_bands)
32+
expect_true("level" %in% names(d_bands))
33+
expect_true("level_low" %in% names(d_bands))
34+
expect_true(is.ordered(d_bands$level))
35+
36+
d_raster <- layer_data(p_raster)
37+
expect_true("density" %in% names(d_raster))
38+
expect_true("ndensity" %in% names(d_raster))
39+
expect_true("count" %in% names(d_raster))
40+
expect_true(unique(d_raster$level) == 1)
41+
expect_true(unique(d_raster$piece) == 1)
42+
43+
# stat_density_2d() with contouring is the same as stat_contour() on calculated density
44+
p_lines2 <- ggplot(d_raster, aes(x, y, z = density)) + stat_contour()
45+
d_lines2 <- layer_data(p_lines2)
46+
expect_identical(d_lines$x, d_lines2$x)
47+
expect_identical(d_lines$y, d_lines2$y)
48+
expect_identical(d_lines$piece, d_lines2$piece)
49+
expect_identical(d_lines$group, d_lines2$group)
50+
expect_identical(d_lines$level, d_lines2$level)
51+
52+
# same for stat_density_2d_filled()
53+
p_bands2 <- ggplot(d_raster, aes(x, y, z = density)) + stat_contour_filled()
54+
d_bands2 <- layer_data(p_bands2)
55+
expect_identical(d_bands$x, d_bands2$x)
56+
expect_identical(d_bands$y, d_bands2$y)
57+
expect_identical(d_bands$piece, d_bands2$piece)
58+
expect_identical(d_bands$group, d_bands2$group)
59+
expect_identical(d_bands$level, d_bands2$level)
60+
expect_identical(d_bands$level_mid, d_bands2$level_mid)
61+
62+
# and for contour_var = "ndensity"
63+
p_contour_lines <- p + stat_density_2d(contour_var = "ndensity")
64+
d_lines <- layer_data(p_contour_lines)
65+
p_lines2 <- ggplot(d_raster, aes(x, y, z = ndensity)) + stat_contour()
66+
d_lines2 <- layer_data(p_lines2)
67+
expect_identical(d_lines$x, d_lines2$x)
68+
expect_identical(d_lines$y, d_lines2$y)
69+
expect_identical(d_lines$piece, d_lines2$piece)
70+
expect_identical(d_lines$group, d_lines2$group)
71+
expect_identical(d_lines$level, d_lines2$level)
72+
73+
# and for contour_var = "count"
74+
p_contour_bands <- p + stat_density_2d(contour_var = "count", contour_type = "bands")
75+
d_bands <- layer_data(p_contour_bands)
76+
p_bands2 <- ggplot(d_raster, aes(x, y, z = count)) + stat_contour_filled()
77+
d_bands2 <- layer_data(p_bands2)
78+
expect_identical(d_bands$x, d_bands2$x)
79+
expect_identical(d_bands$y, d_bands2$y)
80+
expect_identical(d_bands$piece, d_bands2$piece)
81+
expect_identical(d_bands$group, d_bands2$group)
82+
expect_identical(d_bands$level, d_bands2$level)
83+
expect_identical(d_bands$level_mid, d_bands2$level_mid)
84+
85+
# error on incorrect contouring variable
86+
expect_error(ggplot_build(p + stat_density_2d(contour_var = "abcd")))
2987
})

0 commit comments

Comments
 (0)