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

Coord sf breaks fallback #6232

Merged
merged 6 commits into from
Jan 28, 2025
Merged

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #2985.

Briefly, if the graticule lines are horizontal or vertical, we could infer where the tickmark should be placed. This PR doesn't solve axis labels placement for all projections, but it does fix a common painpoint.

For WGS84, it solves the problem:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

world <- sf::st_as_sf(rworldmap::getMap(resolution = "low"))
p <- ggplot(world) + 
  geom_sf()
p

For the Robinson projection, it can place the y-axis labels and a single label at 0 degrees.

p + coord_sf(crs = "+proj=robin")

For this projection, no labels can be placed because all graticule lines are curved.

p + coord_sf(crs = "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000")

The IGH projection only gets y-axis labels because all x-graticule lines are not straight.

p + coord_sf(crs = "+proj=igh")

Created on 2024-12-11 with reprex v2.1.1

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, though the situation with axes and sf continues to bring me pain

@teunbrand
Copy link
Collaborator Author

the situation with axes and sf continues to bring me pain

Me too, I wish there was an sf-based solution that would just work for all types of projections

@teunbrand teunbrand merged commit 7356fe3 into tidyverse:main Jan 28, 2025
13 checks passed
@teunbrand teunbrand deleted the coord_sf_breaks_fallback branch January 28, 2025 10:35
@edzer
Copy link
Contributor

edzer commented Jan 28, 2025

Looks great, thanks - I'll try to dig into this and see how we can be more helpful from the side of sf in this respect (and other packages plotting maps with R).

@teunbrand
Copy link
Collaborator Author

That would be amazing to have @edzer! When I was trying to implement in-panel placement of axes, the problem I kept running into was not too much the ticks/label placement, but the axis line placement. We'd have to know the outline/extent of a map to gracefully place an axis. If that were easy to get from {sf} for most projections that'd really make a big dent in the larger issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

coord_sf() drops axis labels if graticules don't extend all the way to the plot boundary
3 participants