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

Hi, I just found this while searching. Just noting that it doesn't seem to work.

In the dd section:

Error in CPL_transform(x, crs, aoi, pipeline, reverse) :
crs not found: is it missing?

Then others because that is missing.

@DavZim
Copy link
Author

DavZim commented Feb 8, 2021

Make sure you have the latest PROJ version installed, then it should work

@williamlai2
Copy link

I updated PROJ, but it still didn't work.

I got this message:

PROJ is currently non-operational since version 0.4.0

@DavZim
Copy link
Author

DavZim commented Feb 8, 2021

Sorry that I wasn't more clear. I meant the software PROJ and not the r package. See also https://proj.org/index.html

@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