Skip to content

Commit 091d92a

Browse files
authored
Drop lines in geom_sf() with missing linewidth (#5516)
* Use `non_missing_aes` field in GeomPath * Add test * add news bullet
1 parent 9f3eb1a commit 091d92a

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* Lines where `linewidth = NA` are now dropped in `geom_sf()` (#5204).
4+
35
* New `guide_axis_logticks()` can be used to draw logarithmic tick marks as
46
an axis. It supersedes the `annotation_logticks()` function
57
(@teunbrand, #5325).

R/geom-path.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,13 @@ GeomPath <- ggproto("GeomPath", Geom,
132132

133133
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, alpha = NA),
134134

135+
non_missing_aes = c("linewidth", "colour", "linetype"),
136+
135137
handle_na = function(self, data, params) {
136138
# Drop missing values at the start or end of a line - can't drop in the
137139
# middle since you expect those to be shown by a break in the line
138-
complete <- stats::complete.cases(data[names(data) %in% c("x", "y", "linewidth", "colour", "linetype")])
140+
aesthetics <- c(self$required_aes, self$non_missing_aes)
141+
complete <- stats::complete.cases(data[names(data) %in% aesthetics])
139142
kept <- stats::ave(complete, data$group, FUN = keep_mid_true)
140143
data <- data[kept, ]
141144

tests/testthat/test-geom-sf.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ test_that("errors are correctly triggered", {
149149
expect_snapshot_error(ggplotGrob(p))
150150
expect_snapshot_error(geom_sf_label(position = "jitter", nudge_x = 0.5))
151151
expect_snapshot_error(geom_sf_text(position = "jitter", nudge_x = 0.5))
152+
153+
# #5204: missing linewidth should be dropped
154+
pts <- sf::st_sf(
155+
geometry = sf::st_sfc(
156+
sf::st_linestring(matrix(c(0, 1, 0, 1), ncol = 2)),
157+
sf::st_linestring(matrix(c(0, 1, 1, 0), ncol = 2))
158+
),
159+
linewidth = c(1, NA)
160+
)
161+
expect_snapshot_warning(sf_grob(pts, na.rm = FALSE))
152162
})
153163

154164
# Visual tests ------------------------------------------------------------

0 commit comments

Comments
 (0)