st_graticule fails for nzmg
See https://github.com/r-spatial/sf/issues/1047
https://notpeerreviewed.netlify.com/2020/03/mapping-nz-with-an-orthographic-projection/
Hi edzer,
I've been looking through the functions that are used by each method my blog post mentioned above. The way I got it to work used the coord_map function within ggplot2, which requires a data.frame rather than an sf object. I haven't been able to figure out what part of the machinery 'under the hood' is responsible for the rendering of the graticules yet, but I am going to keep looking.
Ah, I see. A quick example (of geom_sf?) that did not work well would be useful here.
Here's a minimal working example that should demonstrate how coord_sf results in rendering artifacts in the graticules, while coord_map does not.
library(rworldmap)
library(dplyr)
library(ggplot2)
library(mapproj)
library(sf)
library(rnaturalearth)
## MWE for how sf objects create rendering artifacts in orthographic projection
## and how sf objects to not work within coord_map() in ggplot2
# download world data from natural earth
world <- ne_countries(scale = 'small', returnclass = 'sf')
class(world) # returns "sf" "data.frame"
# if we plot this with the standard plotting we get a world map
ggplot() +
geom_sf(data=world)
# if we now add an orthographic projection the horizontal lines become apparent
ortho <- "+proj=ortho +lat_0=-35 +lon_0=170 +x_0=0 +y_0=0 +a=6371000 +b=6371000 +units=m +no_defs"
ggplot() +
geom_sf(data=world) +
coord_sf(crs = ortho)
# now lets try to use the coord_map() function instead of coord_sf
# this results in an error because we need to use coord_sf with geom_sf
ggplot() +
geom_sf(data=world) +
coord_map("ortho", orientation=c(-35, 175, 0))
# if we want to use the world data with the coord_map() function
# we need to convert it to a dataframe. First we need to convert
# the sf object to a SpatialPolygonsDataframe so we can fortify it
world_fort <- world %>%
as("Spatial") %>%
fortify()
class(world_fort) # returns "data.frame"
ggplot() +
geom_polygon(data = world_fort, aes(x = long, y = lat, group = group), fill = "grey60") +
coord_map("ortho", orientation=c(-35, 175, 0))
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)
Matrix products: default
locale:
[1] LC_COLLATE=English_New Zealand.1252 LC_CTYPE=English_New Zealand.1252
[3] LC_MONETARY=English_New Zealand.1252 LC_NUMERIC=C
[5] LC_TIME=English_New Zealand.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rnaturalearth_0.1.0 sf_0.8-1 mapproj_1.2.7 maps_3.3.0 ggplot2_3.2.0
[6] dplyr_0.8.3 rworldmap_1.3-6 sp_1.3-1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.1 pillar_1.4.2 compiler_3.5.0 class_7.3-14 tools_3.5.0
[6] dotCall64_1.0-0 tibble_2.1.3 gtable_0.2.0 lattice_0.20-35 pkgconfig_2.0.1
[11] rlang_0.4.0 DBI_1.0.0 rstudioapi_0.7 rgdal_1.4-4 spam_2.5-1
[16] e1071_1.7-0 withr_2.1.2 rgeos_0.3-28 fields_10.3 classInt_0.4-2
[21] grid_3.5.0 tidyselect_0.2.5 glue_1.3.1 R6_2.2.2 foreign_0.8-70
[26] purrr_0.3.2 magrittr_1.5 units_0.6-3 scales_1.0.0 maptools_0.9-9
[31] assertthat_0.2.0 colorspace_1.3-2 labeling_0.3 KernSmooth_2.23-15 lazyeval_0.2.2
[36] munsell_0.5.0 crayon_1.3.4
>
https://github.com/r-spatial/sf/issues/1361