File: test-ggplot-ribbon.R

package info (click to toggle)
r-cran-plotly 4.10.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,636 kB
  • sloc: javascript: 195,272; sh: 24; makefile: 6
file content (50 lines) | stat: -rw-r--r-- 1,662 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50


expect_traces <- function(gg, n.traces, name){
  stopifnot(is.numeric(n.traces))
  L <- expect_doppelganger_built(gg, paste0("ribbon-", name))
  all.traces <- L$data
  no.data <- sapply(all.traces, function(tr) {
    is.null(tr[["x"]]) && is.null(tr[["y"]])
  })
  has.data <- all.traces[!no.data]
  expect_equivalent(length(has.data), n.traces)
  list(traces=has.data, layout=L$layout)
}

huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron))
huron$decade <- with(huron, round(year/10) * 10)
huron$diff <- huron$year - huron$decade

p1 <- ggplot(data = huron) + 
  geom_ribbon(aes(x = year, ymin = level-1, ymax = level+1), 
              alpha = 0.1)

test_that("geom_ribbon() creates 1 trace & respects alpha transparency", {
  info <- expect_traces(p1, 1, "alpha")
  tr <- info$traces[[1]]
  expect_match(tr$fillcolor, "0.1)", fixed=TRUE)
})

p2 <- ggplot(data = huron, aes(group = factor(decade))) + 
  geom_ribbon(aes(x = diff, ymin = level-0.1, ymax = level+0.1))

test_that("geom_ribbon() with group aesthetic produces 1 trace", {
  info <- expect_traces(p2, 1, "group")
})

p3 <- ggplot(data = huron, aes(colour = factor(decade))) + 
  geom_ribbon(aes(x = diff, ymin = level-0.1, ymax = level+0.1))

test_that("geom_ribbon() with colour aesthetic produces multiple traces", {
  # 10 traces -- one for each decade
  info <- expect_traces(p3, 10, "colour")
})

p4 <- ggplot(data = huron, aes(fill = factor(decade))) + 
  geom_ribbon(aes(x = diff, ymin = level-0.1, ymax = level+0.1))

test_that("geom_ribbon() with fill aesthetic produces multiple traces", {
  # 10 traces -- one for each decade
  info <- expect_traces(p4, 10, "fill")
})