|
1 | 1 | context("Probability density")
|
2 | 2 |
|
| 3 | +expect_traces <- function(gg, n.traces, name) { |
| 4 | + stopifnot(is.ggplot(gg)) |
| 5 | + stopifnot(is.numeric(n.traces)) |
| 6 | + save_outputs(gg, paste0("density-", name)) |
| 7 | + L <- gg2list(gg) |
| 8 | + is.trace <- names(L) == "" |
| 9 | + all.traces <- L[is.trace] |
| 10 | + no.data <- sapply(all.traces, function(tr) { |
| 11 | + is.null(tr[["x"]]) && is.null(tr[["y"]]) |
| 12 | + }) |
| 13 | + has.data <- all.traces[!no.data] |
| 14 | + expect_equal(length(has.data), n.traces) |
| 15 | + list(traces=has.data, kwargs=L$kwargs) |
| 16 | +} |
| 17 | + |
3 | 18 | # Draw a probability density estimation using geom_density
|
4 |
| -m <- ggplot(movies) + geom_density(aes(rating)) |
5 |
| -L <- gg2list(m) |
6 |
| - |
7 |
| -test_that("geom_density is translated to a normalized histogram", { |
8 |
| - expect_equal(length(L), 2) |
9 |
| - expect_identical(L[[1]]$type, "histogram") |
10 |
| - expect_true(L[[1]]$autobinx) |
11 |
| - expect_identical(L[[1]]$histnorm, "probability density") |
12 |
| - expect_equal(L[[2]]$layout$bargap, 0) |
| 19 | +base <- ggplot(mtcars, aes(wt)) |
| 20 | + |
| 21 | +test_that("geom_density() is translated to area chart", { |
| 22 | + info <- expect_traces(base + geom_density(), 1, "simple") |
| 23 | + tr <- info$traces[[1]] |
| 24 | + expect_identical(tr$type, "scatter") |
| 25 | + expect_identical(tr$fill, "tozeroy") |
| 26 | + expect_identical(tr$fillcolor, "rgba(51,51,51,0)") |
13 | 27 | })
|
14 | 28 |
|
15 |
| -save_outputs(m, "density") |
| 29 | +test_that("geom_density() respects fill aesthetic", { |
| 30 | + info <- expect_traces(base + geom_density(aes(fill=factor(vs))), 2, "fill") |
| 31 | + trs <- info$traces |
| 32 | + type <- unique(sapply(trs, "[[", "type")) |
| 33 | + fill <- unique(sapply(trs, "[[", "fill")) |
| 34 | + expect_identical(type, "scatter") |
| 35 | + expect_identical(fill, "tozeroy") |
| 36 | +}) |
| 37 | + |
| 38 | +test_that("geom_density() respects colour aesthetic", { |
| 39 | + info <- expect_traces(base + geom_density(aes(colour=factor(vs))), 2, "color") |
| 40 | + trs <- info$traces |
| 41 | + type <- unique(sapply(trs, "[[", "type")) |
| 42 | + fill <- unique(sapply(trs, "[[", "fill")) |
| 43 | + expect_identical(type, "scatter") |
| 44 | + expect_identical(fill, "tozeroy") |
| 45 | +}) |
0 commit comments