Skip to content

Commit 0404eb3

Browse files
committed
Write test with more than one segment
1 parent 50f80bc commit 0404eb3

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

tests/testthat/test-ggplot-segment.R

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,39 @@ test_that("segments become one path", {
1111
tr <- info$data[[1]]
1212
expect_true(any(is.na(tr$x)))
1313
expect_true(any(is.na(tr$y)))
14-
14+
1515
save_outputs(gg, "segment")
1616
})
17+
18+
test_that("with non-numeric data, we can have more than one segment", {
19+
df <- data.frame(donation=c(102.35377, 98.80028, 102.34715, 103.71195,
20+
107.74814, 92.21549, 103.54709, 93.52689,
21+
104.32014, 93.23326, 123.76597, 128.53826,
22+
125.36151, 116.29949, 125.65676, 118.60371,
23+
117.60477, 128.28911, 121.93446, 127.63119,
24+
97.61806, 94.25784, 102.66568, 100.75126,
25+
96.08688, 89.15305, 100.29993, 89.76010,
26+
103.79008, 96.71342, 95.31541, 107.68345,
27+
94.42277, 98.91443, 100.55720, 104.00674,
28+
91.39054, 94.11684, 102.08854, 97.04515),
29+
campaign=c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
30+
2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
31+
2, 2, 2, 2, 2, 2))
32+
33+
seg1 <- data.frame(x=0.85, xend=1.15, y=100.2, yend=100.2)
34+
seg2 <- data.frame(x=1.85, xend=2.15, y=123.5, yend=123.5)
35+
36+
gg <- ggplot() +
37+
geom_point(data=df, aes(x=campaign, y=donation, colour=campaign)) +
38+
geom_segment(data=seg1, aes(x, y, xend=xend, yend=yend)) +
39+
geom_segment(data=seg2, aes(x, y, xend=xend, yend=yend))
40+
41+
fig <- gg2list(gg)
42+
expect_equal(length(fig$data), 4)
43+
expect_equal(fig$data[[3]]$x[1], seg1$x)
44+
expect_equal(fig$data[[3]]$x[2], seg1$xend)
45+
expect_equal(fig$data[[4]]$x[1], seg2$x)
46+
expect_equal(fig$data[[4]]$x[2], seg2$xend)
47+
48+
save_outputs(gg, "segment-multiple-non-numeric")
49+
})

0 commit comments

Comments
 (0)