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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
test_that("geom_sf() determines the legend type automatically", {
skip_if_not_installed("sf")
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
mp <- sf::st_sf(
geometry = sf::st_sfc(sf::st_multipoint(rbind(c(1,1), c(2,2), c(3,3)))),
v = "a")
s1 <- rbind(c(0,3),c(0,4),c(1,5),c(2,5))
s2 <- rbind(c(0.2,3), c(0.2,4), c(1,4.8), c(2,4.8))
s3 <- rbind(c(0,4.4), c(0.6,5))
mls <- sf::st_sf(
geometry = sf::st_sfc(sf::st_multilinestring(list(s1,s2,s3))),
v = "a")
p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
p2 <- rbind(c(1,1), c(1,2), c(2,2), c(1,1))
p3 <- rbind(c(3,0), c(4,0), c(4,1), c(3,1), c(3,0))
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,]
p5 <- rbind(c(3,3), c(4,2), c(4,3), c(3,3))
mpol <- sf::st_sf(
geometry = sf::st_sfc(sf::st_multipolygon(list(list(p1,p2), list(p3,p4), list(p5)))),
v = "a")
fun_geom_sf <- function(sf, show.legend) {
p <- ggplot() + geom_sf(aes(colour = v), data = sf, show.legend = show.legend)
ggplot_build(p)
}
# test the automatic choice
expect_identical(fun_geom_sf(mp, TRUE)$plot$layers[[1]]$show.legend, TRUE)
expect_identical(fun_geom_sf(mp, TRUE)$plot$layers[[1]]$computed_geom_params$legend, "point")
expect_identical(fun_geom_sf(mls, TRUE)$plot$layers[[1]]$show.legend, TRUE)
expect_identical(fun_geom_sf(mls, TRUE)$plot$layers[[1]]$computed_geom_params$legend, "line")
expect_identical(fun_geom_sf(mpol, TRUE)$plot$layers[[1]]$show.legend, TRUE)
expect_identical(fun_geom_sf(mpol, TRUE)$plot$layers[[1]]$computed_geom_params$legend, "polygon")
# test that automatic choice can be overridden manually
expect_identical(fun_geom_sf(mp, "point")$plot$layers[[1]]$show.legend, TRUE)
expect_identical(fun_geom_sf(mp, "point")$plot$layers[[1]]$computed_geom_params$legend, "point")
expect_identical(fun_geom_sf(mls, "point")$plot$layers[[1]]$show.legend, TRUE)
expect_identical(fun_geom_sf(mls, "point")$plot$layers[[1]]$computed_geom_params$legend, "point")
expect_identical(fun_geom_sf(mpol, "point")$plot$layers[[1]]$show.legend, TRUE)
expect_identical(fun_geom_sf(mpol, "point")$plot$layers[[1]]$computed_geom_params$legend, "point")
})
test_that("geom_sf() determines the legend type from mapped geometry column", {
skip_if_not_installed("sf")
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
p1 <- rbind(c(1,1), c(2,2), c(3,3))
s1 <- rbind(c(0,3), c(0,4), c(1,5), c(2,5))
s2 <- rbind(c(0.2,3), c(0.2,4), c(1,4.8), c(2,4.8))
s3 <- rbind(c(0,4.4), c(0.6,5))
d_sf <- sf::st_sf(
g_point = sf::st_sfc(sf::st_multipoint(p1)),
g_line = sf::st_sfc(sf::st_multilinestring(list(s1, s2, s3))),
v = "a"
)
p <- ggplot_build(
ggplot(d_sf) + geom_sf(aes(geometry = g_point, colour = "a"))
)
expect_identical(p$plot$layers[[1]]$computed_geom_params$legend, "point")
p <- ggplot_build(
ggplot(d_sf) + geom_sf(aes(geometry = g_line, colour = "a"))
)
expect_identical(p$plot$layers[[1]]$computed_geom_params$legend, "line")
# If `geometry` is not a symbol, `LayerSf$setup_layer()` gives up guessing
# the legend type, and falls back to "polygon"
p <- ggplot_build(
ggplot(d_sf) + geom_sf(aes(geometry = identity(g_point), colour = "a"))
)
expect_identical(p$plot$layers[[1]]$computed_geom_params$legend, "polygon")
})
test_that("geom_sf() removes rows containing missing aes", {
skip_if_not_installed("sf")
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
grob_xy_length <- function(x) {
g <- layer_grob(x)[[1]]
c(length(g$x), length(g$y))
}
pts <- sf::st_sf(
geometry = sf::st_sfc(sf::st_point(0:1), sf::st_point(1:2)),
size = c(1, NA),
shape = c("a", NA),
colour = c("red", NA)
)
p <- ggplot(pts)
expect_warning(
expect_identical(grob_xy_length(p + geom_sf(aes(size = size))), c(1L, 1L)),
"Removed 1 row containing missing values"
)
expect_warning(
expect_identical(grob_xy_length(p + geom_sf(aes(shape = shape))), c(1L, 1L)),
"Removed 1 row containing missing values"
)
# default colour scale maps a colour even to a NA, so identity scale is needed to see if NA is removed
expect_warning(
expect_identical(grob_xy_length(p + geom_sf(aes(colour = colour)) + scale_colour_identity()),
c(1L, 1L)),
"Removed 1 row containing missing values"
)
})
test_that("geom_sf() handles alpha properly", {
skip_if_not_installed("sf")
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
sfc <- sf::st_sfc(
sf::st_point(0:1),
sf::st_linestring(rbind(0:1, 1:2)),
sf::st_polygon(list(rbind(0:1, 1:2, 2:1, 0:1)))
)
red <- "#FF0000FF"
p <- ggplot(sfc) + geom_sf(colour = red, fill = red, alpha = 0.5)
g <- layer_grob(p)[[1]]
# alpha affects the colour of points and lines
expect_equal(g[[1]]$gp$col, alpha(red, 0.5))
expect_equal(g[[2]]$gp$col, alpha(red, 0.5))
# alpha doesn't affect the colour of polygons, but the fill
expect_equal(g[[3]]$gp$col, alpha(red, 1.0))
expect_equal(g[[3]]$gp$fill, alpha(red, 0.5))
})
test_that("errors are correctly triggered", {
skip_if_not_installed("sf")
pts <- sf::st_sf(
geometry = sf::st_sfc(sf::st_point(0:1), sf::st_point(1:2)),
size = c(1, NA),
shape = c("a", NA),
colour = c("red", NA)
)
p <- ggplot(pts) + geom_sf() + coord_cartesian()
expect_snapshot_error(ggplotGrob(p))
expect_snapshot_error(geom_sf_label(position = "jitter", nudge_x = 0.5))
expect_snapshot_error(geom_sf_text(position = "jitter", nudge_x = 0.5))
})
# Visual tests ------------------------------------------------------------
test_that("geom_sf draws correctly", {
skip_if_not_installed("sf")
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
nc_tiny_coords <- matrix(
c(-81.473, -81.741, -81.67, -81.345, -81.266, -81.24, -81.473,
36.234, 36.392, 36.59, 36.573, 36.437, 36.365, 36.234),
ncol = 2
)
nc <- sf::st_as_sf(
data_frame(
NAME = "ashe",
geometry = sf::st_sfc(sf::st_polygon(list(nc_tiny_coords)), crs = 4326)
)
)
# Perform minimal tests
pts <- sf::st_sf(a = 1:2, geometry = sf::st_sfc(sf::st_point(0:1), sf::st_point(1:2)))
plot <- ggplot() + geom_sf(data = pts)
expect_error(regexp = NA, ggplot_build(plot))
expect_doppelganger("North Carolina county boundaries",
ggplot() + geom_sf(data = nc) + coord_sf(datum = 4326)
)
pts <- sf::st_sf(a = 1:2, geometry = sf::st_sfc(sf::st_point(0:1), sf::st_point(1:2)))
expect_doppelganger("spatial points",
ggplot() + geom_sf(data = pts)
)
})
test_that("geom_sf_text() and geom_sf_label() draws correctly", {
skip_if_not_installed("sf")
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
nc_tiny_coords <- matrix(
c(-81.473, -81.741, -81.67, -81.345, -81.266, -81.24, -81.473,
36.234, 36.392, 36.59, 36.573, 36.437, 36.365, 36.234),
ncol = 2
)
nc <- sf::st_as_sf(
data_frame(
NAME = "ashe",
geometry = sf::st_sfc(sf::st_polygon(list(nc_tiny_coords)), crs = 4326)
)
)
# In order to avoid warning, transform to a projected coordinate system
nc_3857 <- sf::st_transform(nc, 3857)
expect_doppelganger("Texts for North Carolina",
ggplot() + geom_sf_text(data = nc_3857, aes(label = NAME))
)
expect_doppelganger("Labels for North Carolina",
ggplot() + geom_sf_label(data = nc_3857, aes(label = NAME))
)
})
test_that("geom_sf draws arrows correctly", {
skip_if_not_installed("sf")
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
nc_tiny_coords <- data_frame(
x = c(-81.473, -81.741, -81.67, -81.345, -81.266, -81.24, -81.473),
y = c(36.234, 36.392, 36.59, 36.573, 36.437, 36.365, 36.234)
)
nc <- sf::st_linestring(
sf::st_coordinates(sf::st_as_sf(nc_tiny_coords, coords = c("x", "y"), crs = 4326))
)
nc2 <- sf::st_cast(
sf::st_sfc(
sf::st_multilinestring(lapply(
1:(length(sf::st_coordinates(nc)[, 1]) - 1),
function(x) rbind(
as.numeric(sf::st_coordinates(nc)[x, 1:2]),
as.numeric(sf::st_coordinates(nc)[x + 1, 1:2])
)
)
)
), "LINESTRING"
)
expect_doppelganger("North Carolina county boundaries with arrow",
ggplot() + geom_sf(data = nc, arrow = arrow()) + coord_sf(datum = 4326)
)
expect_doppelganger("North Carolina county boundaries with more than one arrow",
ggplot() + geom_sf(data = nc2, arrow = arrow()) + coord_sf(datum = 4326)
)
})
|