-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathtest-gglyph.R
141 lines (104 loc) · 3.55 KB
/
test-gglyph.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
context("gglyph")
data(nasa)
nasaLate <- nasa[
nasa$date >= as.POSIXct("1998-01-01") &
nasa$lat >= 20 &
nasa$lat <= 40 &
nasa$long >= -80 &
nasa$long <= -60
, ]
do_glyph <- function(...) {
glyphs(
nasaLate, # no lint
"long", "day", "lat", "surftemp", height = 2.37, width = 2.38, ...
)
}
do_gg <- function(dt) {
ggplot2::ggplot(dt, ggplot2::aes(gx, gy, group = gid)) +
add_ref_lines(dt, color = "red", size = 0.5) +
add_ref_boxes(dt, color = "blue") +
ggplot2::geom_path() +
ggplot2::theme_bw() +
ggplot2::labs(x = "", y = "") +
ggplot2::xlim(-80, -60) + ggplot2::ylim(20, 40)
}
test_that("examples", {
dt <- do_glyph()
expect_true(all(c("gx", "gy", "gid") %in% names(dt)))
expect_true(all(names(nasaLate) %in% names(dt)))
p <- do_gg(dt)
expect_equal(length(p$layers), 3)
expect_equal(as.character(get("aes_params", envir = p$layers[[1]])$colour), "red")
expect_equal(as.character(get("aes_params", envir = p$layers[[2]])$colour), "blue")
})
test_that("message", {
expect_message(glyphs(nasaLate, "long", "day", "lat", "surftemp", height = 1), "Using width 2.38")
expect_message(glyphs(nasaLate, "long", "day", "lat", "surftemp", width = 1), "Using height 2.37")
})
test_that("scales", {
dt <- do_glyph(x_scale = log)
dt$dayLog <- dt$day
dt$day <- NULL
dtm <- merge(dt, nasaLate)
expect_true(all(dtm$dayLog == log(dtm$day)))
dt <- do_glyph(y_scale = log)
dt$surftempLog <- dt$surftemp
dt$surftemp <- NULL
dtm <- merge(dt, nasaLate)
expect_true(all(dtm$surftempLog == log(dtm$surftemp)))
for (scale_fn in c(range01, max1, mean0, min0, rescale01, rescale11)) {
dt <- do_glyph(y_scale = scale_fn)
dt$surftempScaled <- dt$surftemp
dt$surftemp <- NULL
dtm <- merge(dt, nasaLate)
expect_true(all(dtm$surftempScaled != dtm$surftemp))
}
for (scale_fn in c(rescale01, rescale11)) {
scale_fn2 <- function(x) {
scale_fn(x, xlim = c(1 / 4, 3 / 4))
}
dt <- do_glyph(y_scale = scale_fn2)
dt$surftempScaled <- dt$surftemp
dt$surftemp <- NULL
dtm <- merge(dt, nasaLate)
expect_true(all(dtm$surftempScaled != dtm$surftemp))
}
})
test_that("polar", {
dt <- do_glyph(polar = TRUE)
expect_equal(attr(dt, "polar"), TRUE)
# idk how to test that polar happened
p <- do_gg(dt)
expect_equal(length(p$layers), 3)
})
test_that("fill", {
dt <- do_glyph()
# idk how to test that polar happened
do_gg_fill <- function(...){
ggplot2::ggplot(dt, ggplot2::aes(gx, gy, group = gid)) +
add_ref_lines(dt, color = "red", size = 0.5) +
add_ref_boxes(dt, color = "blue", ...) +
ggplot2::geom_path() +
ggplot2::theme_bw() +
ggplot2::labs(x = "", y = "") +
ggplot2::xlim(-80, -60) + ggplot2::ylim(20, 40)
}
p <- do_gg_fill(fill = "green")
expect_equal(as.character(get("aes_params", envir = p$layers[[2]])$fill), "green")
p <- do_gg_fill(var_fill = "gid")
expect_equal(as.character(get("mapping", envir = p$layers[[2]])$fill), "fill")
})
test_that("print", {
dt <- do_glyph()
txt <- capture.output(print(dt))
expect_equal(txt[length(txt) - 2], "Cartesian glyphplot: ")
expect_equal(txt[length(txt) - 1], " Size: [2.38, 2.37]")
expect_equal(txt[length(txt) - 0], " Major axes: long, lat" )
dt <- do_glyph(polar = TRUE)
txt <- capture.output(print(dt))
expect_equal(txt[length(txt) - 2], "Polar glyphplot: ")
expect_equal(txt[length(txt) - 1], " Size: [2.38, 2.37]")
expect_equal(txt[length(txt) - 0], " Major axes: long, lat" )
txt <- capture.output(print(rel(0.95)))
expect_equal(txt, "[1] 0.95 *")
})