Skip to content

Instantly share code, notes, and snippets.

@jsta
Created September 16, 2021 01:24
Show Gist options
  • Save jsta/40c5ea20f2ceb475d4c9bd441125449e to your computer and use it in GitHub Desktop.
Save jsta/40c5ea20f2ceb475d4c9bd441125449e to your computer and use it in GitHub Desktop.
Automated zooming map with ggplot
library(ggplot2)
library(gganimate)
library(sf)
lg_extent <- c(xmin = -97.9036, ymin = 34.6176, xmax = -66.9989, ymax = 49.4194)
lg_extent <- sf::st_as_sfc(sf::st_bbox(lg_extent), crs = 4326)
earth <- sf::st_as_sf(
rnaturalearth::ne_states(country = "United States of America"))
views <- data.frame(rbind(
st_bbox(earth[earth$name_en == "Texas", ]),
st_bbox(earth[earth$name_en == "Florida", ]),
st_bbox(lg_extent),
st_bbox(earth)
))
p <- ggplot() +
geom_sf(data = earth, fill = "white") +
theme(panel.background = element_rect("lightblue")) +
view_zoom_manual(1, 1,
xmin = views$xmin, xmax = views$xmax,
ymin = views$ymin, ymax = views$ymax,
wrap = TRUE)
animate(p, 100, 10)
anim_save("test.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment