-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathtest-gglegend.R
102 lines (82 loc) · 2.38 KB
/
test-gglegend.R
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
91
92
93
94
95
96
97
98
99
100
101
102
context("gglegend")
expect_print <- function(p, ...) {
testthat::expect_silent(print(p))
}
test_that("examples", {
library(ggplot2)
histPlot <- qplot(
x = Sepal.Length,
data = iris,
fill = Species,
geom = "histogram",
binwidth = 1 / 4
)
(right <- histPlot)
(bottom <- histPlot + theme(legend.position = "bottom"))
(top <- histPlot + theme(legend.position = "top"))
(left <- histPlot + theme(legend.position = "left"))
expect_legend <- function(p) {
plotLegend <- grab_legend(p)
expect_true(inherits(plotLegend, "gtable"))
expect_true(inherits(plotLegend, "gTree"))
expect_true(inherits(plotLegend, "grob"))
expect_print(plotLegend)
}
expect_legend(right)
expect_legend(bottom)
expect_legend(top)
expect_legend(left)
})
test_that("legend", {
# display regular plot
expect_print(
ggally_points(iris, ggplot2::aes(Sepal.Length, Sepal.Width, color = Species))
)
# Make a function that will only print the legend
points_legend <- gglegend(ggally_points)
expect_print(points_legend(
iris, ggplot2::aes(Sepal.Length, Sepal.Width, color = Species)
))
# produce the sample legend plot, but supply a string that 'wrap' understands
same_points_legend <- gglegend("points")
expect_identical(
attr(attr(points_legend, "fn"), "original_fn"),
attr(attr(same_points_legend, "fn"), "original_fn")
)
# Complicated examples
custom_legend <- wrap(gglegend("points"), size = 6)
p <- custom_legend(
iris, ggplot2::aes(Sepal.Length, Sepal.Width, color = Species)
)
expect_print(p)
expect_true(inherits(p, "gtable"))
expect_true(inherits(p, "gTree"))
expect_true(inherits(p, "grob"))
# Use within ggpairs
expect_silent({
pm <- ggpairs(
iris, 1:2,
mapping = ggplot2::aes(color = Species),
upper = list(continuous = gglegend("points"))
)
print(pm)
})
# Use within ggpairs
expect_silent({
pm <- ggpairs(
iris, 1:2,
mapping = ggplot2::aes(color = Species)
)
pm[1, 2] <- points_legend(iris, ggplot2::aes(Sepal.Width, Sepal.Length, color = Species))
print(pm)
})
})
test_that("plotNew", {
points_legend <- gglegend(ggally_points)
expect_print(points_legend(
iris, ggplot2::aes(Sepal.Length, Sepal.Width, color = Species)
))
expect_print(points_legend(
iris, ggplot2::aes(Sepal.Length, Sepal.Width, color = Species)
), plotNew = TRUE)
})