Debian Codegraph @

tests/testthat/test-plot-coordmap.R

raw
1 skip_if_not_installed("ggplot2")
2 library(ggplot2)
3
4 # Sort a list by the names of its keys
5 sortList <- function(x) {
6 x[sort(names(x))]
7 }
8
9 # This will create print.ggplot in the current environment
10 # print.ggplot is for ggplot2 < 4.0.0
11 # print.ggplot2::ggplot is for ggplot2 >= 4.0.0
12 print.ggplot <- `print.ggplot2::ggplot` <- custom_print.ggplot
13
14
15 test_that("ggplot coordmap", {
16 dat <- data.frame(xvar = c(0, 5), yvar = c(10, 20))
17
18 tmpfile <- tempfile("test-shiny", fileext = ".png")
19 on.exit(unlink(tmpfile))
20
21 # Basic scatterplot
22 p <- ggplot(dat, aes(xvar, yvar)) + geom_point() +
23 scale_x_continuous(expand = c(0, 0)) +
24 scale_y_continuous(expand = c(0, 0))
25 png(tmpfile, width = 500, height = 500)
26 m <- getGgplotCoordmap(print(p), 500, 500, 72)
27 dev.off()
28
29 expect_equal(m$dims, list(width = 500, height = 500))
30
31 # Check mapping vars
32 expect_equal(m$panels[[1]]$mapping, list(x = "xvar", y = "yvar"))
33 # Check domain
34 expect_equal(
35 sortList(m$panels[[1]]$domain),
36 sortList(list(left=0, right=5, bottom=10, top=20))
37 )
38 # Check for no log bases
39 expect_equal(
40 sortList(m$panels[[1]]$log),
41 sortList(list(x=NULL, y=NULL))
42 )
43 # panel_vars should be an empty named list
44 expect_identical(m$panels[[1]]$panel_vars, list(a=1)[0])
45 # Sanity check for ranges. Checking exact range values isn't feasible due to
46 # variations in graphics devices, and possible changes to positioning in
47 # ggplot2.
48 expect_true(m$panels[[1]]$range$left > 20 && m$panels[[1]]$range$left < 70)
49 expect_true(m$panels[[1]]$range$right > 480 && m$panels[[1]]$range$right < 499)
50 expect_true(m$panels[[1]]$range$bottom > 450 && m$panels[[1]]$range$bottom < 490)
51 expect_true(m$panels[[1]]$range$top > 1 && m$panels[[1]]$range$top < 20)
52
53
54 # Scatterplot where aes() is declared in geom
55 p <- ggplot(dat, aes(xvar)) + geom_point(aes(y=yvar))
56 png(tmpfile)
57 m <- getGgplotCoordmap(print(p), 500, 500, 72)
58 dev.off()
59
60 # Check mapping vars
61 expect_equal(sortList(m$panels[[1]]$mapping), list(x = "xvar", y = "yvar"))
62
63
64 # Plot with an expression in aes, and a computed variable (histogram)
65 p <- ggplot(dat, aes(xvar/2)) + geom_histogram(binwidth=1)
66 png(tmpfile)
67 m <- getGgplotCoordmap(print(p), 500, 500, 72)
68 dev.off()
69
70 # Check mapping vars - no value for y
71 expect_equal(sortList(m$panels[[1]]$mapping), list(x = "xvar/2", y = NULL))
72 })
73
74
75 test_that("ggplot coordmap with facet_wrap", {
76 dat <- data.frame(xvar = c(0, 5, 10), yvar = c(10, 20, 30),
77 g = c("a", "b", "c"))
78
79 tmpfile <- tempfile("test-shiny", fileext = ".png")
80 on.exit(unlink(tmpfile))
81
82 # facet_wrap
83 p <- ggplot(dat, aes(xvar, yvar)) + geom_point() +
84 scale_x_continuous(expand = c(0, 0)) +
85 scale_y_continuous(expand = c(0, 0)) +
86 facet_wrap(~ g, ncol = 2)
87 png(tmpfile)
88 m <- getGgplotCoordmap(print(p), 500, 400, 72)
89 dev.off()
90
91 # Should have 3 panels
92 expect_equal(length(m$panels), 3)
93 expect_equal(m$panels[[1]]$panel, 1)
94 expect_equal(m$panels[[1]]$row, 1)
95 expect_equal(m$panels[[1]]$col, 1)
96 expect_equal(m$panels[[2]]$panel, 2)
97 expect_equal(m$panels[[2]]$row, 1)
98 expect_equal(m$panels[[2]]$col, 2)
99 expect_equal(m$panels[[3]]$panel, 3)
100 expect_equal(m$panels[[3]]$row, 2)
101 expect_equal(m$panels[[3]]$col, 1)
102
103 # Check mapping vars
104 expect_equal(m$panels[[1]]$mapping, list(x = "xvar", y = "yvar", panelvar1 = "g"))
105 expect_equal(m$panels[[1]]$mapping, m$panels[[2]]$mapping)
106 expect_equal(m$panels[[2]]$mapping, m$panels[[3]]$mapping)
107 # Check domain
108 expect_equal(
109 sortList(m$panels[[1]]$domain),
110 sortList(list(left=0, right=10, bottom=10, top=30))
111 )
112 expect_equal(sortList(m$panels[[1]]$domain), sortList(m$panels[[2]]$domain))
113 expect_equal(sortList(m$panels[[2]]$domain), sortList(m$panels[[3]]$domain))
114
115 # Check panel vars
116 factor_vals <- dat$g
117 expect_equal(m$panels[[1]]$panel_vars, list(panelvar1 = factor_vals[1]))
118 expect_equal(m$panels[[2]]$panel_vars, list(panelvar1 = factor_vals[2]))
119 expect_equal(m$panels[[3]]$panel_vars, list(panelvar1 = factor_vals[3]))
120 })
121
122
123 test_that("ggplot coordmap with facet_grid", {
124 dat <- data.frame(xvar = c(0, 5, 10), yvar = c(10, 20, 30),
125 g = c("a", "b", "c"))
126
127 tmpfile <- tempfile("test-shiny", fileext = ".png")
128 on.exit(unlink(tmpfile))
129
130 p <- ggplot(dat, aes(xvar, yvar)) + geom_point() +
131 scale_x_continuous(expand = c(0, 0)) +
132 scale_y_continuous(expand = c(0, 0))
133
134 # facet_grid horizontal
135 p1 <- p + facet_grid(. ~ g)
136 png(tmpfile)
137 m <- getGgplotCoordmap(print(p1), 500, 400, 72)
138 dev.off()
139
140 # Should have 3 panels
141 expect_equal(length(m$panels), 3)
142 expect_equal(m$panels[[1]]$panel, 1)
143 expect_equal(m$panels[[1]]$row, 1)
144 expect_equal(m$panels[[1]]$col, 1)
145 expect_equal(m$panels[[2]]$panel, 2)
146 expect_equal(m$panels[[2]]$row, 1)
147 expect_equal(m$panels[[2]]$col, 2)
148 expect_equal(m$panels[[3]]$panel, 3)
149 expect_equal(m$panels[[3]]$row, 1)
150 expect_equal(m$panels[[3]]$col, 3)
151
152 # Check mapping vars
153 expect_equal(m$panels[[1]]$mapping, list(x = "xvar", y = "yvar", panelvar1 = "g"))
154 expect_equal(m$panels[[1]]$mapping, m$panels[[2]]$mapping)
155 expect_equal(m$panels[[2]]$mapping, m$panels[[3]]$mapping)
156 # Check domain
157 expect_equal(
158 sortList(m$panels[[1]]$domain),
159 sortList(list(left=0, right=10, bottom=10, top=30))
160 )
161 expect_equal(sortList(m$panels[[1]]$domain), sortList(m$panels[[2]]$domain))
162 expect_equal(sortList(m$panels[[2]]$domain), sortList(m$panels[[3]]$domain))
163
164 # Check panel vars
165 factor_vals <- dat$g
166 expect_equal(m$panels[[1]]$panel_vars, list(panelvar1 = factor_vals[1]))
167 expect_equal(m$panels[[2]]$panel_vars, list(panelvar1 = factor_vals[2]))
168 expect_equal(m$panels[[3]]$panel_vars, list(panelvar1 = factor_vals[3]))
169
170
171 # facet_grid vertical
172 p1 <- p + facet_grid(g ~ .)
173 png(tmpfile)
174 m <- getGgplotCoordmap(print(p1), 500, 400, 72)
175 dev.off()
176
177 # Should have 3 panels
178 expect_equal(length(m$panels), 3)
179 expect_equal(m$panels[[1]]$panel, 1)
180 expect_equal(m$panels[[1]]$row, 1)
181 expect_equal(m$panels[[1]]$col, 1)
182 expect_equal(m$panels[[2]]$panel, 2)
183 expect_equal(m$panels[[2]]$row, 2)
184 expect_equal(m$panels[[2]]$col, 1)
185 expect_equal(m$panels[[3]]$panel, 3)
186 expect_equal(m$panels[[3]]$row, 3)
187 expect_equal(m$panels[[3]]$col, 1)
188
189 # Check mapping vars
190 expect_equal(m$panels[[1]]$mapping, list(x = "xvar", y = "yvar", panelvar1 = "g"))
191 expect_equal(m$panels[[1]]$mapping, m$panels[[2]]$mapping)
192 expect_equal(m$panels[[2]]$mapping, m$panels[[3]]$mapping)
193 # Check domain
194 expect_equal(
195 sortList(m$panels[[1]]$domain),
196 sortList(list(left=0, right=10, bottom=10, top=30))
197 )
198 expect_equal(sortList(m$panels[[1]]$domain), sortList(m$panels[[2]]$domain))
199 expect_equal(sortList(m$panels[[2]]$domain), sortList(m$panels[[3]]$domain))
200
201 # Check panel vars
202 factor_vals <- dat$g
203 expect_equal(m$panels[[1]]$panel_vars, list(panelvar1 = factor_vals[1]))
204 expect_equal(m$panels[[2]]$panel_vars, list(panelvar1 = factor_vals[2]))
205 expect_equal(m$panels[[3]]$panel_vars, list(panelvar1 = factor_vals[3]))
206 })
207
208
209 test_that("ggplot coordmap with 2D facet_grid", {
210 dat <- data.frame(xvar = c(0, 5, 10, 15), yvar = c(10, 20, 30, 40),
211 g = c("a", "b"), h = c("i", "j"))
212
213 tmpfile <- tempfile("test-shiny", fileext = ".png")
214 on.exit(unlink(tmpfile))
215
216 p <- ggplot(dat, aes(xvar, yvar)) + geom_point() +
217 scale_x_continuous(expand = c(0, 0)) +
218 scale_y_continuous(expand = c(0, 0))
219
220 p1 <- p + facet_grid(g ~ h)
221 png(tmpfile)
222 m <- getGgplotCoordmap(print(p1), 500, 400, 72)
223 dev.off()
224
225 # Should have 4 panels
226 expect_equal(length(m$panels), 4)
227 expect_equal(m$panels[[1]]$panel, 1)
228 expect_equal(m$panels[[1]]$row, 1)
229 expect_equal(m$panels[[1]]$col, 1)
230 expect_equal(m$panels[[2]]$panel, 2)
231 expect_equal(m$panels[[2]]$row, 1)
232 expect_equal(m$panels[[2]]$col, 2)
233 expect_equal(m$panels[[3]]$panel, 3)
234 expect_equal(m$panels[[3]]$row, 2)
235 expect_equal(m$panels[[3]]$col, 1)
236 expect_equal(m$panels[[4]]$panel, 4)
237 expect_equal(m$panels[[4]]$row, 2)
238 expect_equal(m$panels[[4]]$col, 2)
239
240 # Check mapping vars
241 expect_equal(m$panels[[1]]$mapping, list(x = "xvar", y = "yvar", panelvar1 = "h", panelvar2 = "g"))
242 expect_equal(m$panels[[1]]$mapping, m$panels[[2]]$mapping)
243 expect_equal(m$panels[[2]]$mapping, m$panels[[3]]$mapping)
244 expect_equal(m$panels[[4]]$mapping, m$panels[[4]]$mapping)
245 # Check domain
246 expect_equal(
247 sortList(m$panels[[1]]$domain),
248 sortList(list(left=0, right=15, bottom=10, top=40))
249 )
250 expect_equal(sortList(m$panels[[1]]$domain), sortList(m$panels[[2]]$domain))
251 expect_equal(sortList(m$panels[[2]]$domain), sortList(m$panels[[3]]$domain))
252 expect_equal(sortList(m$panels[[3]]$domain), sortList(m$panels[[4]]$domain))
253
254 # Check panel vars
255 expect_equal(m$panels[[1]]$panel_vars, list(panelvar1 = dat$h[1], panelvar2 = dat$g[1]))
256 expect_equal(m$panels[[2]]$panel_vars, list(panelvar1 = dat$h[2], panelvar2 = dat$g[1]))
257 expect_equal(m$panels[[3]]$panel_vars, list(panelvar1 = dat$h[1], panelvar2 = dat$g[2]))
258 expect_equal(m$panels[[4]]$panel_vars, list(panelvar1 = dat$h[2], panelvar2 = dat$g[2]))
259 })
260
261
262 test_that("ggplot coordmap with various data types", {
263 tmpfile <- tempfile("test-shiny", fileext = ".png")
264 on.exit(unlink(tmpfile))
265
266 # Factors
267 dat <- expand.grid(xvar = letters[1:3], yvar = LETTERS[1:4])
268 p <- ggplot(dat, aes(xvar, yvar)) + geom_point() +
269 scale_x_discrete(expand = c(0 ,0)) +
270 scale_y_discrete(expand = c(0, 0))
271 png(tmpfile)
272 m <- getGgplotCoordmap(print(p), 500, 400, 72)
273 dev.off()
274
275 # Check domain
276 expectation <- list(
277 left = 1,
278 right = 3,
279 bottom = 1,
280 top = 4,
281 discrete_limits = list(
282 x = letters[1:3],
283 y = LETTERS[1:4]
284 )
285 )
286
287 expect_equal(
288 sortList(m$panels[[1]]$domain),
289 sortList(expectation)
290 )
291
292 # Dates and date-times
293 dat <- data.frame(
294 xvar = as.Date("2016-09-27") + c(0, 10),
295 yvar = as.POSIXct("2016-09-27 09:00:00", origin = "1960-01-01", tz = "GMT") + c(3600, 0)
296 )
297 p <- ggplot(dat, aes(xvar, yvar)) + geom_point() +
298 scale_x_date(expand = c(0 ,0)) +
299 scale_y_datetime(expand = c(0, 0))
300 png(tmpfile)
301 m <- getGgplotCoordmap(print(p), 500, 400, 72)
302 dev.off()
303
304 # Check domain
305 expect_equal(
306 sortList(m$panels[[1]]$domain),
307 sortList(list(
308 left = as.numeric(dat$xvar[1]),
309 right = as.numeric(dat$xvar[2]),
310 bottom = as.numeric(dat$yvar[2]),
311 top = as.numeric(dat$yvar[1])
312 ))
313 )
314 })
315
316 test_that("ggplot coordmap with various scales and coords", {
317 tmpfile <- tempfile("test-shiny", fileext = ".png")
318 on.exit(unlink(tmpfile))
319
320 # Reversed scales
321 dat <- data.frame(xvar = c(0, 5), yvar = c(10, 20))
322 p <- ggplot(dat, aes(xvar, yvar)) + geom_point() +
323 scale_x_continuous(expand = c(0 ,0)) +
324 scale_y_reverse(expand = c(0, 0))
325 png(tmpfile)
326 m <- getGgplotCoordmap(print(p), 500, 400, 72)
327 dev.off()
328
329 # Check domain (y reversed)
330 expect_equal(
331 sortList(m$panels[[1]]$domain),
332 sortList(list(left=0, right=5, bottom=20, top=10))
333 )
334
335 # coord_flip
336 p <- ggplot(dat, aes(xvar, yvar)) + geom_point() +
337 scale_x_continuous(expand = c(0 ,0)) +
338 scale_y_continuous(expand = c(0 ,0)) +
339 coord_flip()
340 png(tmpfile)
341 m <- getGgplotCoordmap(print(p), 500, 400, 72)
342 dev.off()
343
344 # Check mapping vars
345 expect_equal(m$panels[[1]]$mapping, list(x = "yvar", y = "xvar"))
346 # Check domain (y reversed)
347 expect_equal(
348 sortList(m$panels[[1]]$domain),
349 sortList(list(left=10, right=20, bottom=0, top=5))
350 )
351
352 coord_transform_universal <- function(...) {
353 if (packageVersion("ggplot2") >= "4.0.0") {
354 coord_transform(...)
355 } else {
356 coord_trans(...)
357 }
358 }
359
360 # Log scales and log coord transformations
361 dat <- data.frame(xvar = c(10^-1, 10^3), yvar = c(2^-2, 2^4))
362 p <- ggplot(dat, aes(xvar, yvar)) + geom_point() +
363 scale_x_log10(expand = c(0 ,0)) +
364 scale_y_continuous(expand = c(0, 0)) +
365 coord_transform_universal(y = "log2")
366 png(tmpfile)
367 m <- getGgplotCoordmap(print(p), 500, 400, 72)
368 dev.off()
369
370 # Check log bases
371 expect_equal(
372 sortList(m$panels[[1]]$log),
373 sortList(list(x=10, y=2))
374 )
375 # Check domains
376 expect_equal(
377 sortList(m$panels[[1]]$domain),
378 sortList(list(left=-1, right=3, bottom=-2, top=4))
379 )
380 })
381
382
383 test_that("ggplot coordmap maintains discrete limits", {
384 tmpfile <- tempfile("test-shiny", fileext = ".png")
385 on.exit(unlink(tmpfile))
386
387 # check discrete limits are correct for free x scales
388 p <- ggplot(mpg) +
389 geom_point(aes(fl, cty), alpha = 0.2) +
390 facet_wrap(~drv, scales = "free_x")
391 png(tmpfile)
392 m <- getGgplotCoordmap(print(p), 500, 400, 72)
393 dev.off()
394
395 expect_length(m$panels, 3)
396 expect_equal(
397 m$panels[[1]]$domain$discrete_limits,
398 list(x = c("d", "e", "p", "r"))
399 )
400 expect_equal(
401 m$panels[[2]]$domain$discrete_limits,
402 list(x = c("c", "d", "e", "p", "r"))
403 )
404 expect_equal(
405 m$panels[[3]]$domain$discrete_limits,
406 list(x = c("e", "p", "r"))
407 )
408
409 # same for free y
410 p2 <- ggplot(mpg) +
411 geom_point(aes(cty, fl), alpha = 0.2) +
412 facet_wrap(~drv, scales = "free_y")
413 png(tmpfile)
414 m2 <- getGgplotCoordmap(print(p2), 500, 400, 72)
415 dev.off()
416
417 expect_length(m2$panels, 3)
418 expect_equal(
419 m2$panels[[1]]$domain$discrete_limits,
420 list(y = c("d", "e", "p", "r"))
421 )
422 expect_equal(
423 m2$panels[[2]]$domain$discrete_limits,
424 list(y = c("c", "d", "e", "p", "r"))
425 )
426 expect_equal(
427 m2$panels[[3]]$domain$discrete_limits,
428 list(y = c("e", "p", "r"))
429 )
430
431 # check that specifying x limits is captured
432 p3 <- ggplot(mpg) +
433 geom_point(aes(fl, cty), alpha = 0.2) +
434 scale_x_discrete(limits = c("c", "d", "e"))
435
436 png(tmpfile)
437 m3 <- getGgplotCoordmap(suppressWarnings(print(p3)), 500, 400, 72)
438 dev.off()
439
440 expect_length(m3$panels, 1)
441 expect_equal(
442 m3$panels[[1]]$domain$discrete_limits,
443 list(x = c("c", "d", "e"))
444 )
445
446 # same for y
447 p4 <- ggplot(mpg) +
448 geom_point(aes(cty, fl), alpha = 0.2) +
449 scale_y_discrete(limits = c("e", "f"))
450
451 png(tmpfile)
452 m4 <- getGgplotCoordmap(suppressWarnings(print(p4)), 500, 400, 72)
453 dev.off()
454
455 expect_length(m4$panels, 1)
456 expect_equal(
457 m4$panels[[1]]$domain$discrete_limits,
458 list(y = c("e", "f"))
459 )
460
461 # make sure that when labels are specified, where
462 # still relaying the input data
463 p5 <- ggplot(mpg) +
464 geom_point(aes(fl, cty), alpha = 0.2) +
465 scale_x_discrete(
466 limits = c("e", "f"),
467 labels = c("foo", "bar")
468 )
469
470 png(tmpfile)
471 m5 <- getGgplotCoordmap(suppressWarnings(print(p5)), 500, 400, 72)
472 dev.off()
473
474 expect_length(m5$panels, 1)
475 expect_equal(
476 m5$panels[[1]]$domain$discrete_limits,
477 list(x = c("e", "f"))
478 )
479
480 })