Skip to content

Instantly share code, notes, and snippets.

@marcosci
Created November 2, 2022 12:29
Show Gist options
  • Save marcosci/ce0d7739a11a77a930c7c2072d0c74ee to your computer and use it in GitHub Desktop.
Save marcosci/ce0d7739a11a77a930c7c2072d0c74ee to your computer and use it in GitHub Desktop.
Graticules for ggplot2 and sf
library(sf)
library(tidyverse)
projections <- c("+proj=laea",
"+proj=moll",
"+proj=adams_ws2",
"+proj=urm5 +n=0.9",
"+proj=wintri",
"+proj=bonne +lat_1=10",
"+proj=eck1",
"+proj=fouc",
"+proj=larr",
"+proj=nicol",
"+proj=murd1 +lat_1=30 +lat_2=50",
"+proj=rpoly")
world <- map_dfr(projections, function(projection){
world <- sf::st_graticule(lon=seq(-180,180, 15),
lat = seq(-90,90, 7.5),
ndiscr = 5000,
margin = 0.00000000001) %>%
st_transform(projection)
world$crs <- str_split(projection, " ")[[1]][1]
world
})
ggplot() +
geom_sf(data = world, size = 0.3, color = "#424242", alpha = 0.6) +
facet_wrap(vars(crs)) +
labs(title = "Coordinate Reference Systems - Graticules",
caption = "Data: naturalearth | Visualisation: Marco Sciaini") +
theme(panel.grid.major = element_blank(),
text=element_text(family="iA Writer Duospace"),
strip.background = element_rect(fill= alpha('#cd5c5c', 0.5), colour = NA),
strip.text=element_text(size = 9,
face = "bold"),
panel.background = element_rect(fill= alpha('#cd5c5c', 0.1), colour = NA),
plot.title = element_text(size = 26,
face = "bold",
colour = '#212832',
margin = margin(.3,0,1,0, "cm")),
plot.caption = element_text(size = 7,
colour = '#212832',
margin = margin(.3,0,.3,0, "cm"))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment