File: test-ggplot-crossbar.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 (49 lines) | stat: -rw-r--r-- 1,109 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



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

p <- ggplot(d, aes(trt, resp)) + 
  geom_crossbar(aes(ymin = lower, ymax = upper), width = 0.2)


test_that("Basic geom_crossbar() works", {
  
  l <- plotly_build(p)$x
  
  # one trace for each rect and one trace for middle segments,
  # but does it _really_ matter?
  expect_length(l$data, 5)
  
})


p <- ggplot(d, aes(trt, resp, color = group, linetype = group)) + 
  geom_crossbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  scale_colour_manual(values = c("red", "purple"))

test_that("geom_crossbar() with aesthetics", {
  
  l <- plotly_build(p)$x
  
  # one trace for each rect and 2 traces for middle segments...
  expect_length(l$data, 6)
  
  colors <- vapply(l$data, function(x) x$line$color, character(1))
  dashes <- vapply(l$data, function(x) x$line$dash, character(1))
  
  expect_equivalent(
    unique(colors), toRGB(c("red", "purple"))
  )
  
  expect_equivalent(
    unique(dashes), lty2dash(1:2)
  )
  
})