|
1 | 1 | context("geom-sf")
|
2 | 2 |
|
| 3 | +test_that("geom_sf() determines the legend type automatically", { |
| 4 | + skip_if_not_installed("sf") |
| 5 | + if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3") |
| 6 | + |
| 7 | + mp <- sf::st_sf( |
| 8 | + geometry = sf::st_sfc(sf::st_multipoint(rbind(c(1,1), c(2,2), c(3,3)))), |
| 9 | + v = "a") |
| 10 | + |
| 11 | + s1 <- rbind(c(0,3),c(0,4),c(1,5),c(2,5)) |
| 12 | + s2 <- rbind(c(0.2,3), c(0.2,4), c(1,4.8), c(2,4.8)) |
| 13 | + s3 <- rbind(c(0,4.4), c(0.6,5)) |
| 14 | + |
| 15 | + mls <- sf::st_sf( |
| 16 | + geometry = sf::st_sfc(sf::st_multilinestring(list(s1,s2,s3))), |
| 17 | + v = "a") |
| 18 | + |
| 19 | + p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0)) |
| 20 | + p2 <- rbind(c(1,1), c(1,2), c(2,2), c(1,1)) |
| 21 | + p3 <- rbind(c(3,0), c(4,0), c(4,1), c(3,1), c(3,0)) |
| 22 | + p4 <- rbind(c(3.3,0.3), c(3.8,0.3), c(3.8,0.8), c(3.3,0.8), c(3.3,0.3))[5:1,] |
| 23 | + p5 <- rbind(c(3,3), c(4,2), c(4,3), c(3,3)) |
| 24 | + |
| 25 | + mpol <- sf::st_sf( |
| 26 | + geometry = sf::st_sfc(sf::st_multipolygon(list(list(p1,p2), list(p3,p4), list(p5)))), |
| 27 | + v = "a") |
| 28 | + |
| 29 | + fun_geom_sf <- function(sf, show.legend) { |
| 30 | + p <- ggplot() + geom_sf(aes(colour = v), data = sf, show.legend = show.legend) |
| 31 | + ggplot_build(p) |
| 32 | + } |
| 33 | + |
| 34 | + # test the automatic choice |
| 35 | + expect_identical(fun_geom_sf(mp, TRUE)$plot$layers[[1]]$show.legend, TRUE) |
| 36 | + expect_identical(fun_geom_sf(mp, TRUE)$plot$layers[[1]]$geom_params$legend, "point") |
| 37 | + |
| 38 | + expect_identical(fun_geom_sf(mls, TRUE)$plot$layers[[1]]$show.legend, TRUE) |
| 39 | + expect_identical(fun_geom_sf(mls, TRUE)$plot$layers[[1]]$geom_params$legend, "line") |
| 40 | + |
| 41 | + expect_identical(fun_geom_sf(mpol, TRUE)$plot$layers[[1]]$show.legend, TRUE) |
| 42 | + expect_identical(fun_geom_sf(mpol, TRUE)$plot$layers[[1]]$geom_params$legend, "polygon") |
| 43 | + |
| 44 | + # test that automatic choice can be overridden manually |
| 45 | + expect_identical(fun_geom_sf(mp, "point")$plot$layers[[1]]$show.legend, TRUE) |
| 46 | + expect_identical(fun_geom_sf(mp, "point")$plot$layers[[1]]$geom_params$legend, "point") |
| 47 | + |
| 48 | + expect_identical(fun_geom_sf(mls, "point")$plot$layers[[1]]$show.legend, TRUE) |
| 49 | + expect_identical(fun_geom_sf(mls, "point")$plot$layers[[1]]$geom_params$legend, "point") |
| 50 | + |
| 51 | + expect_identical(fun_geom_sf(mpol, "point")$plot$layers[[1]]$show.legend, TRUE) |
| 52 | + expect_identical(fun_geom_sf(mpol, "point")$plot$layers[[1]]$geom_params$legend, "point") |
| 53 | +}) |
| 54 | + |
3 | 55 | test_that("geom_sf() removes rows containing missing aes", {
|
4 | 56 | skip_if_not_installed("sf")
|
5 | 57 | if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
|
|
0 commit comments