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() ignores axis label settings #2857

Closed
clauswilke opened this issue Aug 25, 2018 · 2 comments · Fixed by #2858
Closed

coord_sf() ignores axis label settings #2857

clauswilke opened this issue Aug 25, 2018 · 2 comments · Fixed by #2858

Comments

@clauswilke
Copy link
Member

clauswilke commented Aug 25, 2018

As mentioned in #2846 (comment), axis labels cannot be set manually when using coord_sf().

library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
library(ggplot2) 

# graticule labeling via cardinal coordinates (N, S, E, W)
nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"))
#> Reading layer `nc.gpkg' from data source `/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sf/gpkg/nc.gpkg' using driver `GPKG'
#> Simple feature collection with 100 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs
ggplot() + 
  geom_sf(aes(fill = AREA), data=nc) + 
  coord_sf() +
  scale_y_continuous(
    breaks = c(34, 34.5, 35.7), # setting breaks manually works
    labels = c("A", "B", "C")   # but labels are ignored
  )

The reason is that the setup_panel_params() function of coord_sf() ignores the break info generated by the scales and instead reads and interprets the scales' breaks parameters directly:

ggplot2/R/sf.R

Lines 450 to 457 in 6500a30

graticule <- sf::st_graticule(
bbox,
crs = params$crs,
lat = scale_y$breaks %|W|% NULL,
lon = scale_x$breaks %|W|% NULL,
datum = self$datum,
ndiscr = self$ndiscr
)

For comparison, coord_cartesian() does this:
out <- scale$break_info(range)

coord_sf() is written the way it is because it needs to let sf::st_graticule() make appropriate decisions about where to put graticules and how to label them, with the knowledge of the CRS that is being used. The scales don't have that knowledge and would in general produce bad breaks and labels.

What this means is that if we want to be able to allow manual overriding of labels, we will have to add additional logic to coord_sf() / setup_panel_params() that essentially mimics the scales' get_labels() function:

ggplot2/R/scale-.r

Lines 289 to 309 in 6500a30

get_labels = function(self, breaks = self$get_breaks()) {
if (is.null(breaks)) return(NULL)
breaks <- self$trans$inverse(breaks)
if (is.null(self$labels)) {
return(NULL)
} else if (identical(self$labels, NA)) {
stop("Invalid labels specification. Use NULL, not NA", call. = FALSE)
} else if (is.waive(self$labels)) {
labels <- self$trans$format(breaks)
} else if (is.function(self$labels)) {
labels <- self$labels(breaks)
} else {
labels <- self$labels
}
if (length(labels) != length(breaks)) {
stop("Breaks and labels are different lengths")
}
labels
},

Also potentially relevant: r-spatial/sf#829

@clauswilke
Copy link
Member Author

It turns out this was much easier to fix than I thought. I've made a PR (#2858).

clauswilke added a commit that referenced this issue Aug 27, 2018
…#2858)

* enable manual formatting of tick labels for coord_sf(). closes #2857
@lock
Copy link

lock bot commented Feb 23, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Feb 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant