Skip to content

Instantly share code, notes, and snippets.

@mdlincoln

mdlincoln/europop.R

Last active Dec 18, 2015
Embed
What would you like to do?
Create an animated GIF of European urban population 1500-1800
library(devtools)
install_github("mdlincoln/europop")
library(europop)
library(dplyr)
library(rgdal)
library(ggplot2)
library(animation)
# Get the coastline shapefile (derived from OSM)
eur_shp <- readOGR(system.file("extdata", "eur_coast", package = "europop"), layer = "coastlines_z4") %>% spTransform(CRS("+proj=longlat +datum=WGS84"))
eur_df <- fortify(eur_shp)
# Join city coordinates to the population metrics
pop_points <- europop %>% inner_join(city_coords, by = "city")
saveGIF({
for(i in seq(1500, 1800, 50)) {
# Get the points for this year
subpop <- pop_points %>% filter(year == i)
p <- ggplot() +
geom_map(data = eur_df, map = eur_df, aes(x = long, y = lat, group = group, map_id = id), fill = "gray", color = "gray") +
geom_point(data = subpop, aes(x = lon, y = lat, color = population, size = population)) +
coord_map() +
facet_wrap(~ year) +
theme_minimal() +
theme(legend.position = "none") +
ylim(min(pop_points$lat), max(pop_points$lat)) +
xlim(min(pop_points$lon), max(pop_points$lon))
plot(p)
}
}, movie.name = "europop.gif", interval = 0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment