Skip to content

geom_sf(): add support for scale_x_continuous(n.breaks = ) #4622

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

Closed
pat-s opened this issue Sep 18, 2021 · 9 comments · Fixed by #5442
Closed

geom_sf(): add support for scale_x_continuous(n.breaks = ) #4622

pat-s opened this issue Sep 18, 2021 · 9 comments · Fixed by #5442
Labels
bug an unexpected problem or unintended behavior coord 🗺️

Comments

@pat-s
Copy link

pat-s commented Sep 18, 2021

Currently this argument does not have an effect when using geom_sf().

library("ggplot2")
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
ggplot(nc) +
  geom_sf(aes(fill = AREA)) +
  scale_x_continuous(n.breaks = 2)

Created on 2021-09-18 by the reprex package (v2.0.1)

@thomasp85
Copy link
Member

@clauswilke do you have a rough idea of why coord_sf() behaves like this?

@thomasp85 thomasp85 added bug an unexpected problem or unintended behavior coord 🗺️ labels Oct 28, 2021
@clauswilke
Copy link
Member

I'll take a look.

@thomasp85
Copy link
Member

Thanks... My guess is it comes down to sf, but you never know...

@clauswilke
Copy link
Member

I think it's these two lines:

ggplot2/R/coord-sf.R

Lines 226 to 227 in f5e01ba

lat = scale_y$breaks %|W|% NULL,
lon = scale_x$breaks %|W|% NULL,

We're reading out the value of the breaks element directly instead of calling get_breaks():

get_breaks = function(self, limits = self$get_limits()) {

But I don't fully understand how the other coords use this code. I believe they call guide_train.axis() which then calls get_breaks():

ggplot2/R/guides-axis.r

Lines 64 to 67 in f5e01ba

guide_train.axis <- function(guide, scale, aesthetic = NULL) {
aesthetic <- aesthetic %||% scale$aesthetics[1]
breaks <- scale$get_breaks()

but my problem is that I see how coord_cartesian() does this but not how coord_polar() does this. Yet things seem to work for coord_polar().

Maybe @paleolimbot can weigh in?

@paleolimbot
Copy link
Member

I'm far removed from this to have a quick answer other than that I recall that the intention was to use the guide axis framework for the other Coord subclasses once the bugs had been ironed out for CoordCartesian. Perhaps this is a good time to do that for CoordSf? I'm nearly at a place where I could take that on but it will be a few weeks.

@clauswilke
Copy link
Member

But do I understand correctly that the guide axis framework is not needed to get the axis breaks right? So we could just fix the two lines in coord_sf() and be done with it?

I would suspect that coord_sf() is a particularly challenging coord to move to the guide axis framework, as it does a lot of non-standard things such as turning projected coordinates into long/lat values for labels.

@paleolimbot
Copy link
Member

Yes, likely much easier! $get_breaks() is probably what should be used.

@hadley
Copy link
Member

hadley commented Apr 19, 2022

I think the concern with using get_breaks() is that the default break computation used by sf might be much better. I don't have any evidence for this, so it might be worth doing a little spelunking in the sf code to figure out exactly what it's doing so we can make sure our defaults match.

@teunbrand
Copy link
Collaborator

Some additional limitations:

  1. setting breaks = NULL does not remove gridlines or axis.
library(ggplot2)

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

ggplot(nc) +
  geom_sf() +
  scale_x_continuous(breaks = NULL)

  1. Providing a function to breaks does not work.
ggplot(nc) +
  geom_sf() +
  scale_x_continuous(breaks = scales::breaks_width(1))
#> Error in min(lon): invalid 'type' (closure) of argument

Created on 2023-09-26 with reprex v2.0.2

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.

6 participants