File: test-ggplot-errorbar.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 (42 lines) | stat: -rw-r--r-- 1,208 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


test_that("geom_errorbar gives errorbars", {

  d <- dplyr::summarise(
    dplyr::group_by(mtcars, cyl), 
    q1 = quantile(mpg, 0.25),
    m = mean(mpg),
    q3 = quantile(mpg, 0.75)
  )

  g <- ggplot(d, aes(x = cyl, y = m)) + geom_line() +
    geom_errorbar(aes(ymin = q1, ymax = q3))

  L <- expect_doppelganger_built(g, "errorbar")
  
  # 1 trace should have error_y
  idx <- vapply(L$data, function(x) is.null(x$error_y), logical(1))
  expect_true(sum(idx) == 1)
  # right data for errorbar ymax
  expect_equivalent(L$data[!idx][[1]]$error_y$array, d$q3 - d$m)
  expect_equivalent(L$data[!idx][[1]]$error_y$arrayminus, d$m - d$q1)
})

df <- data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 3, 4)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)
)

p <- ggplot(df, aes(trt, resp, colour = group))
g <- p + geom_errorbar(aes(ymin = lower, ymax = upper))

test_that("geom_errorbar boxes an array of length 1", {
  L <- expect_doppelganger_built(g, "errorbar-unique-groups")
  expect_equivalent(L$data[[1]]$error_y$array, I(0.1))
  expect_equivalent(L$data[[1]]$error_y$arrayminus, I(0.2))
})

# TODO fix and add a test for width of errorbars