Skip to content

Implement GeomSf$handle_na() #3537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions R/geom-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,22 @@ GeomSf <- ggproto("GeomSf", Geom,
alpha = NA,
stroke = 0.5
),

non_missing_aes = c("size", "shape", "colour"),

handle_na = function(self, data, params) {
type <- sf_types[sf::st_geometry_type(data$geometry)]

if (all(type == "point")) {
non_missing_aes <- c("size", "shape", "colour")
} else {
non_missing_aes <- character()
}

remove_missing(data, params$na.rm,
c(self$required_aes, non_missing_aes),
snake_class(self)
)
},

draw_panel = function(data, panel_params, coord, legend = NULL,
lineend = "butt", linejoin = "round", linemitre = 10) {
if (!inherits(coord, "CoordSf")) {
Expand Down
25 changes: 21 additions & 4 deletions tests/testthat/test-geom-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,36 @@ test_that("geom_sf() removes rows containing missing aes", {
colour = c("red", NA)
)

p <- ggplot(pts) + geom_sf()
p1 <- ggplot(pts) + geom_sf()
expect_warning(
expect_identical(grob_xy_length(p + aes(size = size)), c(1L, 1L)),
expect_identical(grob_xy_length(p1 + aes(size = size)), c(1L, 1L)),
"Removed 1 rows containing missing values"
)
expect_warning(
expect_identical(grob_xy_length(p + aes(shape = shape)), c(1L, 1L)),
expect_identical(grob_xy_length(p1 + aes(shape = shape)), c(1L, 1L)),
"Removed 1 rows 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 + aes(colour = colour) + scale_colour_identity()),
expect_identical(grob_xy_length(p1 + aes(colour = colour) + scale_colour_identity()),
c(1L, 1L)),
"Removed 1 rows containing missing values"
)

plgns <- sf::st_sf(
geometry = sf::st_sfc(
sf::st_polygon(list(rbind(1:2, 2:3, 3:4, 1:2))),
sf::st_polygon(list(rbind(1:2, 2:3, 3:4, 1:2)))
),
colour = c(1, NA)
)

p2 <- ggplot(plgns) + geom_sf()
# colour is not a non_missing_aes for polygons
expect_warning(
expect_identical(grob_xy_length(p2 + aes(colour = colour) + scale_colour_identity()),
c(8L, 8L)),
NA
)

})