Skip to content

Instantly share code, notes, and snippets.

@dakvid
Created November 27, 2022 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dakvid/7b2e9acd3110e94aff59d4cb49a92f66 to your computer and use it in GitHub Desktop.
Save dakvid/7b2e9acd3110e94aff59d4cb49a92f66 to your computer and use it in GitHub Desktop.
#30DayMapChallenge 2022 - Day 19 - Globe
# Animate an expanding buffer around land, with a "globe" view of the Pacific
library(glue)
library(stringr)
library(dplyr)
library(purrr)
library(sf)
library(rnaturalearth)
library(ggplot2)
library(av)
CRS_GLOBE <- "+proj=ortho +lat_0=-10 +lon_0=-160 +x_0=0 +y=0=0 +a=6371000 +b=6371000 +units=m +no_defs"
COLOUR_BG <- "grey10"
COLOUR_OCEAN <- "light blue"
COLOUR_LAND <- "dark green"
ocean <-
st_point(x = c(0, 0)) |>
st_buffer(dist = 6371000) |>
st_sfc(crs = CRS_GLOBE)
plot_the_frame <-
function(land_buffer) {
land_buffered <-
ne_countries(scale = "large", returnclass = "sf") |>
st_buffer(land_buffer) |>
st_intersection(ocean |> st_transform(crs = 4326L)) |>
st_transform(crs = CRS_GLOBE)
gg_frame <-
ggplot() +
geom_sf(data = ocean, fill = COLOUR_OCEAN, colour = NA) +
geom_sf(data = land_buffered, fill = COLOUR_LAND, colour = NA) +
theme_void() +
theme(plot.background = element_rect(fill = COLOUR_BG, colour = COLOUR_BG),
panel.background = element_rect(fill = COLOUR_BG, colour = COLOUR_BG))
ggsave(plot = gg_frame,
filename = glue("frame_{str_pad(land_buffer, 6, pad = '0')}.png"),
path = "Day_19_Globe/frame/",
height = 8, width = 8)
}
(0:400 * 1000) |>
walk(plot_the_frame)
av_encode_video(list.files(path = "Day_19_Globe/frame", pattern = "png", full.names = TRUE),
framerate = 30,
output = "Day_19_Globe/Day_19_globe.mp4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment