Skip to content

Instantly share code, notes, and snippets.

@sivartravis
Forked from jeroen/dplyr-animation.R
Created February 25, 2016 21:13
Show Gist options
  • Save sivartravis/20eaaabddce10c820d4d to your computer and use it in GitHub Desktop.
Save sivartravis/20eaaabddce10c820d4d to your computer and use it in GitHub Desktop.
library(magrittr)
library(dplyr)
library(nycflights13)
library(evaluate)
library(ggplot2)
# Define the plot
makeplot <- function(mydata){
date <- as.Date(paste("2013", mydata[1,"month"], mydata[1,"day"], sep="-"))
ggplot(mydata, aes(distance, arr_delay)) +
geom_point() + geom_smooth(se = FALSE) +
xlim(0, 3000) + ylim(0, 500) +
xlab("Distance") + ylab("Delay") +
ggtitle(format(date, "%A, %B %d, %Y"))
}
# Render and store the plots
sub_plots <- flights %>%
filter(!is.na(distance), !is.na(arr_delay)) %>%
group_by(month, day) %>%
do(plot = evaluate("makeplot(.)", keep_warning=FALSE)[[3]])
# Play animation in rstudio
sub_plots %>% do(out = {print(.$plot); Sys.sleep(.10)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment