Skip to content
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

Skip viewscales if graticule is empty #6060

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
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)
})
Loading