-
Notifications
You must be signed in to change notification settings - Fork 641
Expand file tree
/
Copy pathtest-ggplot-hline.R
More file actions
90 lines (73 loc) · 2.49 KB
/
test-ggplot-hline.R
File metadata and controls
90 lines (73 loc) · 2.49 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
context("Hline")
# Horizontal line
x <- seq(0, 3.5, by = 0.5)
y <- x * 0.95
df <- data.frame(x, y)
gg <- ggplot(df) + geom_point(aes(x, y))
test_that("second trace be the hline", {
p <- gg + geom_hline(yintercept = 1.1, colour = "green", size = 3)
L <- expect_doppelganger_built(p, "hline")
expect_equivalent(length(L$data), 2)
l <- L$data[[2]]
expect_equivalent(unique(l$y), 1.1)
expect_true(min(l$x) < min(x))
expect_true(max(l$x[2]) > max(x))
expect_identical(l$mode, "lines")
expect_true(l$line$color == "rgba(0,255,0,1)")
})
test_that("vector yintercept results in multiple horizontal lines", {
p <- gg + geom_hline(yintercept = 1:3, colour = "red", size = 3)
L <- expect_doppelganger_built(p, "hline-multiple")
expect_equivalent(length(L$data), 2)
l <- L$data[[2]]
ys <- l$y
expect_equivalent(ys, c(1, 1, NA, 2, 2, NA, 3, 3))
xs <- l$x
expect_true(min(xs, na.rm = TRUE) < min(x))
expect_true(max(xs, na.rm = TRUE) > max(x))
expect_identical(l$mode, "lines")
expect_true(l$line$color == "rgba(255,0,0,1)")
})
test_that("hline can be drawn over range of factors", {
df <- data.frame(
cond = c("control", "treatment"),
result = c(10, 11.5)
)
gg <- ggplot(df, aes(x = cond, y = result)) +
geom_bar(position = "dodge", stat = "identity") +
geom_hline(aes(yintercept = 12))
L <- expect_doppelganger_built(gg, "hline-factor")
expect_equivalent(length(L$data), 2) # 1 trace for bar chart, 1 trace for hline
})
test_that("hline/vline/abline split on linetype/colour/size", {
d <- tibble::tibble(
x = seq(0, 3.5, by = 0.5),
y = x * 0.95
)
gg <- ggplot(d, aes(x, y)) +
geom_vline(xintercept = c(2.5, 3, 3.5), linetype = 1:3) +
geom_hline(yintercept = c(2.5, 3, 3.5), size = 1:3) +
geom_abline(slope = -1, intercept = c(2.5, 3, 3.5), colour = 1:3)
l <- plotly_build(gg)$x
expect_length(l$data, 9)
expect_equivalent(
vapply(l$data, function(x) x$line$dash, character(1)),
lty2dash(c(1:3, rep(1, 6)))
)
expect_equivalent(
unique(vapply(l$data, function(x) x$line$color, character(1))),
c("rgba(0,0,0,1)", "rgba(255,0,0,1)", "rgba(0,205,0,1)")
)
expect_length(
unique(vapply(l$data, function(x) x$line$width, numeric(1))), 4
)
})
test_that("hline works with coord_flip", {
gg <- ggplot() +
geom_point(aes(6, 5)) +
geom_hline(yintercept = 5) +
coord_flip()
l <- plotly_build(gg)$x
expect_equivalent(l$data[[2]]$x, c(5, 5))
expect_equivalent(l$data[[2]]$y, c(5.95, 6.05))
})