|
1 | 1 | context("geom-sf")
|
2 | 2 |
|
| 3 | +test_that("geom_sf() removes rows containing missing aes", { |
| 4 | + skip_if_not_installed("sf") |
| 5 | + if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3") |
| 6 | + |
| 7 | + grob_xy_length <- function(x) { |
| 8 | + g <- layer_grob(x)[[1]] |
| 9 | + c(length(g$x), length(g$y)) |
| 10 | + } |
| 11 | + |
| 12 | + pts <- sf::st_sf( |
| 13 | + geometry = sf::st_sfc(sf::st_point(0:1), sf::st_point(1:2)), |
| 14 | + size = c(1, NA), |
| 15 | + shape = c("a", NA), |
| 16 | + colour = c("red", NA) |
| 17 | + ) |
| 18 | + |
| 19 | + p <- ggplot(pts) + geom_sf() |
| 20 | + expect_warning( |
| 21 | + expect_identical(grob_xy_length(p + aes(size = size)), c(1L, 1L)), |
| 22 | + "Removed 1 rows containing missing values" |
| 23 | + ) |
| 24 | + expect_warning( |
| 25 | + expect_identical(grob_xy_length(p + aes(shape = shape)), c(1L, 1L)), |
| 26 | + "Removed 1 rows containing missing values" |
| 27 | + ) |
| 28 | + # default colour scale maps a colour even to a NA, so identity scale is needed to see if NA is removed |
| 29 | + expect_warning( |
| 30 | + expect_identical(grob_xy_length(p + aes(colour = colour) + scale_colour_identity()), |
| 31 | + c(1L, 1L)), |
| 32 | + "Removed 1 rows containing missing values" |
| 33 | + ) |
| 34 | +}) |
| 35 | + |
| 36 | +test_that("geom_sf() handles alpha properly", { |
| 37 | + skip_if_not_installed("sf") |
| 38 | + if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3") |
| 39 | + |
| 40 | + sfc <- sf::st_sfc( |
| 41 | + sf::st_point(0:1), |
| 42 | + sf::st_linestring(rbind(0:1, 1:2)), |
| 43 | + sf::st_polygon(list(rbind(0:1, 1:2, 2:1, 0:1))) |
| 44 | + ) |
| 45 | + red <- "#FF0000FF" |
| 46 | + p <- ggplot(sfc) + geom_sf(colour = red, fill = red, alpha = 0.5) |
| 47 | + g <- layer_grob(p)[[1]] |
| 48 | + |
| 49 | + # alpha affects the colour of points and lines |
| 50 | + expect_equal(g[[1]]$gp$col, alpha(red, 0.5)) |
| 51 | + expect_equal(g[[2]]$gp$col, alpha(red, 0.5)) |
| 52 | + # alpha doesn't affect the colour of polygons, but the fill |
| 53 | + expect_equal(g[[3]]$gp$col, alpha(red, 1.0)) |
| 54 | + expect_equal(g[[3]]$gp$fill, alpha(red, 0.5)) |
| 55 | +}) |
3 | 56 |
|
4 | 57 | # Visual tests ------------------------------------------------------------
|
5 | 58 |
|
@@ -64,36 +117,3 @@ test_that("geom_sf_text() and geom_sf_label() draws correctly", {
|
64 | 117 | ggplot() + geom_sf_label(data = nc_3857, aes(label = NAME))
|
65 | 118 | )
|
66 | 119 | })
|
67 |
| - |
68 |
| -test_that("geom_sf() removes rows containing missing aes", { |
69 |
| - skip_if_not_installed("sf") |
70 |
| - if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3") |
71 |
| - |
72 |
| - grob_xy_length <- function(x) { |
73 |
| - g <- layer_grob(x)[[1]] |
74 |
| - c(length(g$x), length(g$y)) |
75 |
| - } |
76 |
| - |
77 |
| - pts <- sf::st_sf( |
78 |
| - geometry = sf::st_sfc(sf::st_point(0:1), sf::st_point(1:2)), |
79 |
| - size = c(1, NA), |
80 |
| - shape = c("a", NA), |
81 |
| - colour = c("red", NA) |
82 |
| - ) |
83 |
| - |
84 |
| - p <- ggplot(pts) + geom_sf() |
85 |
| - expect_warning( |
86 |
| - expect_identical(grob_xy_length(p + aes(size = size)), c(1L, 1L)), |
87 |
| - "Removed 1 rows containing missing values" |
88 |
| - ) |
89 |
| - expect_warning( |
90 |
| - expect_identical(grob_xy_length(p + aes(shape = shape)), c(1L, 1L)), |
91 |
| - "Removed 1 rows containing missing values" |
92 |
| - ) |
93 |
| - # default colour scale maps a colour even to a NA, so identity scale is needed to see if NA is removed |
94 |
| - expect_warning( |
95 |
| - expect_identical(grob_xy_length(p + aes(colour = colour) + scale_colour_identity()), |
96 |
| - c(1L, 1L)), |
97 |
| - "Removed 1 rows containing missing values" |
98 |
| - ) |
99 |
| -}) |
|
0 commit comments