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
|
test_that("geom_sf() basic polygons.", {
skip_if_not_installed("sf")
skip_if_not_installed("s2")
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
p <- ggplot(nc) + geom_sf()
l <- expect_doppelganger_built(p, "sf")
# one trace is for the graticule
expect_length(l$data, 2)
# can translate degree symbol
expect_true(
all(grepl("\\°", l$layout$xaxis$ticktext))
)
expect_true(
all(grepl("\\°", l$layout$yaxis$ticktext))
)
})
test_that("geom_sf() geometry collection.", {
skip_if_not_installed("sf")
# example from the sf vignette
a <- sf::st_polygon(list(cbind(c(0,0,7.5,7.5,0),c(0,-1,-1,0,0))))
b <- sf::st_polygon(list(cbind(c(0,1,2,3,4,5,6,7,7,0),c(1,0,.5,0,0,0.5,-0.5,-0.5,1,1))))
i <- sf::st_intersection(a, b)
cd <- sf::st_as_sf(data.frame(d = 1, geometry = sf::st_sfc(i)))
p <- ggplot(cd) + geom_sf()
l <- expect_doppelganger_built(p, "sf-geom-collection")
# graticule, point, line, polygon
expect_length(l$data, 4)
})
test_that("geom_sf() polygons with fill/text.", {
skip_if_not_installed("sf")
skip_if_not_installed("s2")
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
p <- ggplot(nc) + geom_sf(aes(fill = AREA, text = NAME))
l <- expect_doppelganger_built(p, "sf-fill-text")
# one trace for every fillcolor, one for graticule, one for colorbar
expect_length(l$data, length(unique(nc$AREA)) + 2)
expect_true(
all(unlist(lapply(l$data, "[[", "hoverinfo")) %in% c("skip", "none", "text"))
)
# graticule styling should inherit from panel.grid.major
expect_equivalent(
l$data[[1]]$line$color,
toRGB(ggplot2::calc_element("panel.grid.major", ggplot2::theme_gray())[["colour"]])
)
})
test_that("geom_sf() with basic polygons and points.", {
skip_if_not_installed("sf")
skip_if_not_installed("s2")
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
p <- ggplot(nc) +
geom_sf() +
annotate("point", x = -80, y = 35, colour = "red", size = 4) +
theme(panel.grid.major = element_line(colour = "red"))
l <- expect_doppelganger_built(p, "sf-points")
# one trace for graticule, one for point, and one polygons
expect_length(l$data, 3)
# graticule should be red
expect_equivalent(l$data[[1]]$line$color, "rgba(255,0,0,1)")
expect_equivalent(l$data[[2]]$mode, "lines")
expect_equivalent(l$data[[3]]$mode, "markers")
})
test_that("sf aspect ratio is correct", {
skip_if_not_installed("sf")
skip_if_not_installed("s2")
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
p <- ggplot(nc) + geom_sf()
l <- expect_doppelganger_built(p, "sf-aspect")
expect_equivalent(l$layout$xaxis$scaleanchor, "y")
expect_equal(l$layout$xaxis$scaleratio, 0.81678435872298)
})
test_that("works with a blank theme", {
skip_if_not_installed("sf")
skip_if_not_installed("s2")
skip_if_not_installed("ggthemes")
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
p <- ggplot(nc) + geom_sf() +
ggthemes::theme_map()
l <- expect_doppelganger_built(p, "sf-theme-map")
# TODO: perhaps the graticule shouldn't be included at all?
expect_length(l$data, 2)
expect_equivalent(l$data[[1]]$line$color, "transparent")
})
test_that("resolves overlapping axis ticks", {
skip_if_not_installed("sf")
skip_if_not_installed("maps")
world <- sf::st_as_sf(maps::map('world', plot = FALSE, fill = TRUE))
# filter the world sf object down to canada
canada <- dplyr::filter(world, ID == "Canada")
# coerce cities lat/long data to an official sf object
cities <- sf::st_as_sf(
maps::canada.cities,
coords = c("long", "lat"),
crs = 4326
)
# A PROJ4 projection designed for Canada
# http://spatialreference.org/ref/sr-org/7/
# http://spatialreference.org/ref/sr-org/7/proj4/
moll_proj <- "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84
+units=m +no_defs"
# perform the projections
canada <- sf::st_transform(canada, moll_proj)
cities <- sf::st_transform(cities, moll_proj)
# plot with geom_sf()
p <- ggplot() +
geom_sf(data = canada) +
geom_sf(data = cities, aes(size = pop), color = "red", alpha = 0.3)
expect_doppelganger_built(ggplotly(p), "sf-axis-ticks")
})
|