Skip to content

Instantly share code, notes, and snippets.

@mdlincoln
Last active December 18, 2015 16:33
Show Gist options
  • Save mdlincoln/102cb07100238649fde3 to your computer and use it in GitHub Desktop.
Save mdlincoln/102cb07100238649fde3 to your computer and use it in GitHub Desktop.
Animated GIF of simulation replications
library(ggplot2)
library(dplyr)
library(animation)
load(url("http://matthewlincoln.net/assets/docs/sim_data.rda"))
saveGIF({
# We want one frame per replication
for(i in 1:max(bm_raw_sims$r)) {
# Data from the first up to the current replciation
subdat <- bm_raw_sims %>% filter(between(r, 1, i))
# Data just for this current replication
thisdat <- subdat %>% filter(r == i)
# Finding the 10th and 90th percentile of all the replications up to this
# one, which we will use to draw the range ribbon
subag <- subdat %>%
group_by(year, model_type) %>%
summarize(min = quantile(centralization, 0.1), max = quantile(centralization, 0.9))
p <- ggplot(aes(x = year, color = model_type)) +
# Plot nice big points for the current replication
geom_point(data = thisdat, aes(y = centralization), size = 2) +
# Plot soft points for the previous replications
geom_point(data = subdat, aes(y = centralization), alpha = 0.05) +
# Plot a ribbon with the current upper- and lower-bounds of the distribution
geom_ribbon(data = subag, aes(ymin = min, ymax = max, fill = model_type), fill = "transparent") +
# Some chart chrome
xlim(1550, 1750) +
ylim(0, 0.2) +
ggtitle(paste("Replication", i)) +
theme_bw() +
theme(legend.position = "bottom")
plot(p)
}
}, movie.name = "range_simulation.gif", interval = 0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment