Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ggplot2 (development version)

* `coord_sf()` no longer errors when dealing with empty graticules (@teunbrand, #6052)
* Passing empty unmapped aesthetics to layers raises a warning instead of
throwing an error (@teunbrand, #6009).
* Moved {mgcv} from Imports to Suggests (@teunbrand, #5986)
Expand Down
3 changes: 3 additions & 0 deletions R/coord-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ sf_breaks <- function(scale_x, scale_y, bbox, crs) {
#' @keywords internal
view_scales_from_graticule <- function(graticule, scale, aesthetic,
label, label_graticule, bbox) {
if (empty(graticule)) {
return(ggproto(NULL, ViewScale))
}

# Setup position specific parameters
# Note that top/bottom doesn't necessarily mean to label the meridians and
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-coord_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,27 @@ test_that("coord_sf() throws error when limits are badly specified", {
# throws error when limit's length is different than two
expect_snapshot_error(ggplot() + coord_sf(ylim=1:3))
})

test_that("coord_sf() can render with empty graticules", {

skip_if_not_installed("sf")
# Skipping this test on CRAN as changes upstream in {sf} might affect
# this test, i.e. when suddenly graticules *do* work
skip_on_cran()

df <- sf::st_sf(
g = sf::st_sfc(sf::st_point(
# Out of bounds values for lon/lat
c(-600, 1200)
)),
crs = 4326
)

# Double-check graticule is empty, suppressing warnings about oob longlat values
grat <- suppressWarnings(sf::st_graticule(df))
expect_equal(nrow(grat), 0)

# Plot should render
p <- suppressWarnings(layer_grob(ggplot(df) + geom_sf())[[1]])
expect_length(p$x, 1)
})