Skip to content

Commit 6606390

Browse files
authored
Skip viewscales if graticule is empty (#6060)
* skip viewscales for empty graticules * add test * add news bullet
1 parent eb8bf83 commit 6606390

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
`labs()` and several guides (@teunbrand, #3196).
200200
* `stat_summary_bin()` no longer ignores `width` parameter (@teunbrand, #4647).
201201
* Added `keep.zeroes` argument to `stat_bin()` (@teunbrand, #3449)
202+
* `coord_sf()` no longer errors when dealing with empty graticules (@teunbrand, #6052)
202203

203204
# ggplot2 3.5.1
204205

R/coord-sf.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,9 @@ sf_breaks <- function(scale_x, scale_y, bbox, crs) {
657657
#' @keywords internal
658658
view_scales_from_graticule <- function(graticule, scale, aesthetic,
659659
label, label_graticule, bbox) {
660+
if (empty(graticule)) {
661+
return(ggproto(NULL, ViewScale))
662+
}
660663

661664
# Setup position specific parameters
662665
# Note that top/bottom doesn't necessarily mean to label the meridians and

tests/testthat/test-coord_sf.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,27 @@ test_that("coord_sf() throws error when limits are badly specified", {
371371
# throws error when limit's length is different than two
372372
expect_snapshot_error(ggplot() + coord_sf(ylim=1:3))
373373
})
374+
375+
test_that("coord_sf() can render with empty graticules", {
376+
377+
skip_if_not_installed("sf")
378+
# Skipping this test on CRAN as changes upstream in {sf} might affect
379+
# this test, i.e. when suddenly graticules *do* work
380+
skip_on_cran()
381+
382+
df <- sf::st_sf(
383+
g = sf::st_sfc(sf::st_point(
384+
# Out of bounds values for lon/lat
385+
c(-600, 1200)
386+
)),
387+
crs = 4326
388+
)
389+
390+
# Double-check graticule is empty, suppressing warnings about oob longlat values
391+
grat <- suppressWarnings(sf::st_graticule(df))
392+
expect_equal(nrow(grat), 0)
393+
394+
# Plot should render
395+
p <- suppressWarnings(layer_grob(ggplot(df) + geom_sf())[[1]])
396+
expect_length(p$x, 1)
397+
})

0 commit comments

Comments
 (0)