diff --git a/NEWS.md b/NEWS.md index 9340b73a2f..e2e2d0c607 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # ggplot2 (development version) +* Axis labels are now preserved better when using `coord_sf(expand = TRUE)` and + graticule lines are straight but do not meet the edge (@teunbrand, #2985). +* Attempt to boost detail in `coord_polar()` and `coord_radial()` near the + center (@teunbrand, #5023) * Scale names, guide titles and aesthetic labels can now accept functions (@teunbrand, #4313) * Binned scales with zero-width data expand the default limits by 0.1 diff --git a/R/coord-sf.R b/R/coord-sf.R index 3f96ff6aaf..d603d57de7 100644 --- a/R/coord-sf.R +++ b/R/coord-sf.R @@ -721,6 +721,14 @@ view_scales_from_graticule <- function(graticule, scale, aesthetic, accept_start <- graticule[[orth_start]] < thres accept_end <- graticule[[orth_end]] < thres } + if (!any(accept_start | accept_end)) { + eps <- sqrt(.Machine$double.xmin) + subtract <- switch(position, top = , bottom = 90, 0) + straight <- + abs(graticule$angle_start - subtract) < eps & + abs(graticule$angle_end - subtract) < eps + accept_start <- straight + } # Parsing the information of the `label_axes` argument: # should we label the meridians ("E") or parallels ("N")? diff --git a/tests/testthat/test-coord_sf.R b/tests/testthat/test-coord_sf.R index 516d2fa9ec..a684bea20b 100644 --- a/tests/testthat/test-coord_sf.R +++ b/tests/testthat/test-coord_sf.R @@ -425,3 +425,15 @@ test_that("coord_sf() can render with empty graticules", { p <- suppressWarnings(layer_grob(ggplot(df) + geom_sf())[[1]]) expect_length(p$x, 1) }) + +test_that("coord_sf() can calculate breaks when expansion is on", { + skip_if_not_installed("sf") + df <- sf::st_multipoint(cbind(c(-180, 180), c(-90, 90))) + df <- sf::st_sfc(df, crs = 4326) + b <- ggplot_build(ggplot(df) + geom_sf()) + + x <- get_guide_data(b, "x") + y <- get_guide_data(b, "y") + expect_equal(nrow(x), 5L) + expect_equal(nrow(y), 3L) +})