Description
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:
Lines 450 to 457 in 6500a30
For comparison,
coord_cartesian()
does this:Line 102 in 6500a30
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:
Lines 289 to 309 in 6500a30
Also potentially relevant: r-spatial/sf#829