Skip to content

Enable manual setting of axis tick labels with coord_sf. Closes #2857 #2858

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 2 commits into from
Aug 27, 2018

Conversation

clauswilke
Copy link
Member

This PR closes #2857. It enables most ways in which axis tick labels can normally be set for coord_sf(). The main limitation is that it can only accept character vectors. Parsed expressions will not work. (However, strings that contain the degree symbol will automatically be parsed into expressions.)

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

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

# explicitly supply label values
ggplot() + 
  geom_sf(aes(fill = AREA), data=nc) + 
  coord_sf() +
  scale_x_continuous(labels = letters[1:5]) +
  scale_y_continuous(
    breaks = c(34, 34.5, 35.7),
    labels = c("A", "B", "C")
  )

# set to NULL to remove labels
ggplot() + 
  geom_sf(aes(fill = AREA), data=nc) + 
  coord_sf() +
  scale_x_continuous(labels = NULL) +
  scale_y_continuous(
    breaks = c(34, 34.5, 35.7),
    labels = NULL
  )

# format via function
ggplot() + 
  geom_sf(aes(fill = AREA), data=nc) + 
  coord_sf() +
  scale_x_continuous(
    breaks = c(-84, -80, -76),
    labels = function(x) paste0(x, "*degree*East")
  ) +
  scale_y_continuous(
    breaks = c(34, 34.5, 35.7),
    labels = function(x) paste0(x, "*degree*North")
  )

# error if length mismatch
ggplot() + 
  geom_sf(aes(fill = AREA), data=nc) + 
  coord_sf() +
  scale_x_continuous(labels = letters[1:4])
#> Error: Breaks and labels along x direction are different lengths

Created on 2018-08-25 by the reprex package (v0.2.0).

Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

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

Hmmm, I don’t love the autoparsing, but I’m not sure how to do better and everything else looks good.

@clauswilke clauswilke merged commit 519a55d into tidyverse:master Aug 27, 2018
@clauswilke
Copy link
Member Author

I have some thoughts on how autoparsing could be avoided / used only on labels generated by st_graticule(), but it is quite involved and I don't want to get into this right now. The fundamental problem is that in the most general case we can end up with character labels for one direction (e.g. N/S) and parsed expressions for the other (e.g. E/W). To handle this appropriately, we'd have to break the graticule into two components, one for each direction, and store them separately. This would have repercussions in several other places in the code.

@clauswilke clauswilke deleted the issue-2857-sf-labels branch August 27, 2018 19:34
@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 this pull request may close these issues.

coord_sf() ignores axis label settings
2 participants