Skip to content

Coord sf breaks fallback #6232

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

Merged
merged 6 commits into from
Jan 28, 2025
Merged
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions R/coord-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")?
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-coord_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Loading