Skip to content

Attribute plot12 in st_graticule() is not documented and not sufficiently general #828

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
clauswilke opened this issue Aug 23, 2018 · 7 comments

Comments

@clauswilke
Copy link

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).

@clauswilke
Copy link
Author

Actually, when writing code to place arbitrary labels on arbitrary sides of a plot, I realized that an added complexity arises because we don't necessarily know in which direction a graticule runs:
https://github.com/clauswilke/ggplot2/blob/ee2efae191c5d98499ce7b891b28d7048aebdd7e/R/sf.R#L385-L391
So in general, we would have to know not only which graticules intersect with which plot boundaries but also which end of each graticule intersects. This can't be easily encapsulated in TRUE/FALSE values but could be in a column that holds values such as "start", "end", or "none" to indicate intersection with a plot boundary at the start, at the end, or not at all, respectively.

@clauswilke
Copy link
Author

For reference, this is the ggplot2 pull request that uses this code, with example figures:
tidyverse/ggplot2#2849

@edzer
Copy link
Member

edzer commented Aug 24, 2018

The possibilities are: "start", "end", "none", and "both"!

@clauswilke
Copy link
Author

Ah, I hadn't thought about "both", but I can see how it could happen. Could you provide an example that generates such a case? It may be relevant to my discussion with @hadley on this issue:
tidyverse/ggplot2#2846

@edzer
Copy link
Member

edzer commented Aug 28, 2018

@clauswilke is your request still relevant? It's doable, but not like trivial.

@clauswilke
Copy link
Author

No, not that important. I have revised all my code to allow for this possibility in principle.

@edzer
Copy link
Member

edzer commented Aug 28, 2018

OK, then we can close here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants