Skip to content

Commit 7356fe3

Browse files
authored
Coord sf breaks fallback (#6232)
* add news bullet * fallback * avoid duplicated labels * add test * add news bullet
1 parent fa7e2c1 commit 7356fe3

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

NEWS.md

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

3+
* Axis labels are now preserved better when using `coord_sf(expand = TRUE)` and
4+
graticule lines are straight but do not meet the edge (@teunbrand, #2985).
5+
* Attempt to boost detail in `coord_polar()` and `coord_radial()` near the
6+
center (@teunbrand, #5023)
37
* Scale names, guide titles and aesthetic labels can now accept functions
48
(@teunbrand, #4313)
59
* Binned scales with zero-width data expand the default limits by 0.1

R/coord-sf.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,14 @@ view_scales_from_graticule <- function(graticule, scale, aesthetic,
721721
accept_start <- graticule[[orth_start]] < thres
722722
accept_end <- graticule[[orth_end]] < thres
723723
}
724+
if (!any(accept_start | accept_end)) {
725+
eps <- sqrt(.Machine$double.xmin)
726+
subtract <- switch(position, top = , bottom = 90, 0)
727+
straight <-
728+
abs(graticule$angle_start - subtract) < eps &
729+
abs(graticule$angle_end - subtract) < eps
730+
accept_start <- straight
731+
}
724732

725733
# Parsing the information of the `label_axes` argument:
726734
# should we label the meridians ("E") or parallels ("N")?

tests/testthat/test-coord_sf.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,15 @@ test_that("coord_sf() can render with empty graticules", {
425425
p <- suppressWarnings(layer_grob(ggplot(df) + geom_sf())[[1]])
426426
expect_length(p$x, 1)
427427
})
428+
429+
test_that("coord_sf() can calculate breaks when expansion is on", {
430+
skip_if_not_installed("sf")
431+
df <- sf::st_multipoint(cbind(c(-180, 180), c(-90, 90)))
432+
df <- sf::st_sfc(df, crs = 4326)
433+
b <- ggplot_build(ggplot(df) + geom_sf())
434+
435+
x <- get_guide_data(b, "x")
436+
y <- get_guide_data(b, "y")
437+
expect_equal(nrow(x), 5L)
438+
expect_equal(nrow(y), 3L)
439+
})

0 commit comments

Comments
 (0)