File: test-ggplot-vline.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 (53 lines) | stat: -rw-r--r-- 1,526 bytes parent folder | download
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
51
52
53

x <- seq(0, 3.5, by = 0.5)
y <- x * 0.95
df <- data.frame(x, y)
gg <- ggplot(df, aes(x, y)) + geom_point()

test_that("second trace be the vline", {
  p <- gg + geom_vline(xintercept = 1.1, colour = "green", size = 3)
  
  L <- expect_doppelganger_built(p, "vline")
  l <- L$data[[2]]
  
  expect_equivalent(length(L$data), 2)
  expect_equivalent(l$x[1], 1.1)
  expect_true(l$y[1] <= 0)
  expect_true(l$y[2] >= 3.325)
  expect_true(l$mode == "lines")
  expect_true(l$line$color == "rgba(0,255,0,1)")
})

test_that("vector xintercept results in multiple vertical lines", {
  p <- gg + geom_vline(xintercept = 1:2, colour = "blue", size = 3)
  
  L <- expect_doppelganger_built(p, "vline-multiple")
  expect_equivalent(length(L$data), 2)
  l <- L$data[[2]]
  xs <- unique(l$x)
  ys <- unique(l$y)
  expect_identical(xs, c(1, NA, 2))
  expect_true(min(ys, na.rm = TRUE) <= min(y))
  expect_true(max(ys, na.rm = TRUE) >= max(y))
  expect_true(l$mode == "lines")
  expect_true(l$line$color == "rgba(0,0,255,1)")
})



test_that("vline works with coord_flip", {
  
  gg <- ggplot() + 
    geom_point(aes(5, 6)) + 
    geom_vline(xintercept = 5) + 
    coord_flip()
  
  l <- plotly_build(gg)$x
  expect_equivalent(l$data[[2]]$x, c(5.95, 6.05))
  expect_equivalent(l$data[[2]]$y, c(5, 5))
})

test_that("geom_vline/geom_hline does not throw an error with ggplotly when no lines are found", {
  p <- ggplot(df) + geom_vline(aes(xintercept = x), data = data.frame(x = 1)[F, , drop = FALSE])
  expect_error(plotly::ggplotly(p), NA)
})