Description
While trying to figure out how to address tidyverse/ggplot2#2846 (coord_sf()
can't draw axis tick labels on the right or top), I found this line in the ggplot2 code:
https://github.com/tidyverse/ggplot2/blob/5a37ae7dd590c045d8fe5d01ea66d4e733946713/R/sf.R#L336-L338
which uses the plot12
attribute from st_graticule()
:
sf::st_graticule()
#> Simple feature collection with 28 features and 10 fields
#> Attribute-geometry relationship: 3 constant, 0 aggregate, 0 identity, 7 NA's
#> geometry type: MULTILINESTRING
#> dimension: XY
#> bbox: xmin: -180 ymin: -89.91 xmax: 180 ymax: 89.91
#> epsg (SRID): 4326
#> proj4string: +proj=longlat +datum=WGS84 +no_defs
#> First 10 features:
#> degree type degree_label geometry x_start y_start
#> 1 -180 E 180*degree MULTILINESTRING ((-180 -89.... -180 -89.91
#> 2 -160 E 160*degree*W MULTILINESTRING ((-160 -89.... -160 -89.91
#> 3 -140 E 140*degree*W MULTILINESTRING ((-140 -89.... -140 -89.91
#> 4 -120 E 120*degree*W MULTILINESTRING ((-120 -89.... -120 -89.91
#> 5 -100 E 100*degree*W MULTILINESTRING ((-100 -89.... -100 -89.91
#> 6 -80 E 80*degree*W MULTILINESTRING ((-80 -89.9... -80 -89.91
#> 7 -60 E 60*degree*W MULTILINESTRING ((-60 -89.9... -60 -89.91
#> 8 -40 E 40*degree*W MULTILINESTRING ((-40 -89.9... -40 -89.91
#> 9 -20 E 20*degree*W MULTILINESTRING ((-20 -89.9... -20 -89.91
#> 10 0 E 0*degree MULTILINESTRING ((0 -89.91,... 0 -89.91
#> x_end y_end angle_start angle_end plot12
#> 1 -180 89.91 90 90 TRUE
#> 2 -160 89.91 90 90 TRUE
#> 3 -140 89.91 90 90 TRUE
#> 4 -120 89.91 90 90 TRUE
#> 5 -100 89.91 90 90 TRUE
#> 6 -80 89.91 90 90 TRUE
#> 7 -60 89.91 90 90 TRUE
#> 8 -40 89.91 90 90 TRUE
#> 9 -20 89.91 90 90 TRUE
#> 10 0 89.91 90 90 TRUE
As far as I understand, this attribute is true if an axis tick should be drawn on the left or bottom, and false otherwise. However, this is nowhere documented, as far as I can see. More importantly, this looks to me like a specialized hack. A more general solution would return appropriate attributes for all four sides of the plot. Actually, the most general case requires 8 pieces of information, because each graticule type (N or E) can potentially intersect with any edge of the bounding box. Looking at examples such as the one below (arguably weird) has made me wonder whether there should be a switch to allow labeling E graticules on the y axis and N graticules on the x axis, which would require this info.
Would it be possible to provide more general info about which graticules intersect with which boundaries? And also, would it be possible to document plot12
?
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
ggplot() +
geom_sf(aes(fill = AREA), data=nc) + coord_sf(crs = 3338)
Created on 2018-08-23 by the reprex package (v0.2.0).