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() drops axis labels if graticules don't extend all the way to the plot boundary #2985

Open
clauswilke opened this issue Nov 9, 2018 · 5 comments · May be fixed by #6232
Open

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

clauswilke opened this issue Nov 9, 2018 · 5 comments · May be fixed by #6232
Labels
bug an unexpected problem or unintended behavior coord 🗺️

Comments

@clauswilke
Copy link
Member

clauswilke commented Nov 9, 2018

In ggplot2 3.1.0, coord_sf() drops axis labels if graticules don't extend all the way to the plot boundary. This is a regression from 3.0.0. A reprex follows.

library(ggplot2)

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

ggplot(world) + geom_sf() + theme_bw() + coord_sf(expand = FALSE)

Created on 2018-11-08 by the reprex package (v0.2.1)

This bug was introduced by me when I wrote code such as the following, to calculate intersection points between the graticules and the plot boundaries:

ggplot2/R/sf.R

Line 592 in 15ddc22

id1 <- c(id1, which(graticule$type == panel_params$label_axes$top & graticule$y_start > 0.999))

I did not consider that the plot range could extend beyond the physical limits of the earth. I'll have to ponder what the options are. One option might be to make the tolerance configurable. (I hardcoded 0.001.) Another option might be to base the tolerance on the expansion factors used for the x and y scales.

@hadley
Copy link
Member

hadley commented Nov 9, 2018

Alternatively, should we prohibit plot ranges that are outside of the limits of the earth?

@clauswilke
Copy link
Member Author

I don't think that's a feasible approach. We want to be able to make plots such as these:

library(ggplot2)

world <- sf::st_as_sf(rworldmap::getMap(resolution = "low"))
ggplot(world) + geom_sf() + theme_bw() +
  coord_sf(crs = "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000")

ggplot(world) + geom_sf() + theme_bw() + 
  coord_sf(crs = "+proj=robin")

Created on 2018-11-09 by the reprex package (v0.2.1)

On further reflection, it's not entirely clear to me what the right approach is. In the general case, if the graticule lines don't intersect with the plot boundary, we don't know where to place the ticks. It only works out in the longlat projection because the lines are perfectly horizontal/vertical. On the flip side, it would be nice to place labels at arbitrary positions along the axes, at least manually, and that isn't possible currently.

I noticed the old behavior here:
https://www.r-spatial.org/r/2018/10/25/ggplot2-sf.html
Notice how the longlat projection has labels but the Lambert Azimuthal Equal-Area projection does not. So the old behavior also had some inconsistencies.

@hadley
Copy link
Member

hadley commented Nov 9, 2018

Ah good point. I guess the ideal behaviour is to project out the graticule lines to where they would intersect with the plot, but I don't see how we'd have enough information to do that (or what that even really means when the graticules are not reasonable straight lines)

@thomasp85
Copy link
Member

projection could also result in some weird placements for highly non-linear graticules... and alternative could be to provide an annotation function for adding graticule labels for the cases where they are not drawn by the axes

@paleolimbot paleolimbot added bug an unexpected problem or unintended behavior coord 🗺️ labels May 23, 2019
@thomasp85 thomasp85 added this to the ggplot2 3.3.4 milestone Mar 25, 2021
@thomasp85 thomasp85 removed this from the ggplot2 3.3.4 milestone Apr 8, 2021
@teunbrand
Copy link
Collaborator

Hi @edzer, do you happen to know if there is a reliable way to get an 'outline' of sf geometries that encompasses the graticule? I think tick mark positions and labels can already be extracted from the graticule, but I'm unsure how to connect these ticks to form a sort of curved axis.

@teunbrand teunbrand linked a pull request Dec 11, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior coord 🗺️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants