Skip to content
Merged
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@

* `stat_density2d()` can now take an `adjust` parameter to scale the default bandwidth. (#2860, @haleyjeppson)

* `geom_sf()` now removes rows that contain missing `shape`/`size`/`colour` (#3483, @yutannihilation)

# ggplot2 3.2.1

This is a patch release fixing a few regressions introduced in 3.2.0 as well as
Expand Down
2 changes: 2 additions & 0 deletions R/geom-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ GeomSf <- ggproto("GeomSf", Geom,
stroke = 0.5
),

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

draw_panel = function(data, panel_params, coord, legend = NULL,
lineend = "butt", linejoin = "round", linemitre = 10) {
if (!inherits(coord, "CoordSf")) {
Expand Down
33 changes: 33 additions & 0 deletions tests/testthat/test-geom-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,36 @@ test_that("geom_sf_text() and geom_sf_label() draws correctly", {
ggplot() + geom_sf_label(data = nc_3857, aes(label = NAME))
)
})

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) + geom_sf()
expect_warning(
expect_identical(grob_xy_length(p + 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)),
"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()),
c(1L, 1L)),
"Removed 1 rows containing missing values"
)
})