Skip to content

Instantly share code, notes, and snippets.

@DavZim
Created January 20, 2021 10:37
Show Gist options
  • Save DavZim/fff4edb060a76b47ffd3385a1758862b to your computer and use it in GitHub Desktop.
Save DavZim/fff4edb060a76b47ffd3385a1758862b to your computer and use it in GitHub Desktop.
Creates a gif of the world in different map projections
library(tidyverse)
library(sf)
library(spData)
library(gganimate)
data(world)
# see also: https://proj.org/operations/projections/index.html
projs <- list(
"Mercator" = "+proj=merc",
"WGS 84" = "+proj=longlat",
"Robinson" = "+proj=robin",
"Compact Miller" = "+proj=comill",
"Eckert I" = "+proj=eck1",
"Eckert II" = "+proj=eck2",
"Eckert III" = "+proj=eck3",
"Eckert IV" = "+proj=eck4",
"Mollweide" = "+proj=moll",
"Azimuth Equidistant" = "+proj=aeqd",
"Lambert Equal Area" = "+proj=laea",
"Adams World in a Square 2" = "+proj=adams_ws2",
"Sinusoidal (Sanson-Flamsteed)" = "+proj=sinu",
"Interrupted Goode Homolosine" = "+proj=igh"
)
dd <- map_dfr(projs, function(p) {
d <- world %>%
select(iso_a2, name_long, continent, geom) %>%
mutate(projection = p, L1 = 1:n()) %>%
st_transform(p)
dd <- left_join(
st_coordinates(d) %>% as_tibble(),
d %>% as_tibble() %>% select(-geom),
by = "L1"
)
res <- dd %>%
select(x = X, y = Y, everything()) %>%
mutate(id = paste0(L1, L2, L3)) %>%
mutate(x_orig = x, y_orig = y,
x = (x - min(x)) / (max(x) - min(x)),
y = (y - min(y)) / (max(y) - min(y)))
res
}) %>%
mutate(projection = factor(projection, levels = unlist(projs)))
anim <- ggplot(dd, aes(x = x, y = y, group = id)) +
geom_polygon(fill = "black") +
transition_states(projection) +
ease_aes("cubic-in-out") +
labs(title = "The World in '{names(projs)[projs == closest_state]}' Projektion",
subtitle = "Projection: '{closest_state}'") +
theme_minimal() +
theme(axis.title=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
panel.grid=element_blank(),
plot.background=element_rect(fill = "white"))
anim_save("images/projections.gif", anim, res = 200, width = 1000, height = 1000)
@williamlai2
Copy link

Thanks. I'll give it a go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment