Skip to content

Instantly share code, notes, and snippets.

@earino
Created June 21, 2015 01:23
Show Gist options
  • Save earino/3290dc444a4f4d4c3719 to your computer and use it in GitHub Desktop.
Save earino/3290dc444a4f4d4c3719 to your computer and use it in GitHub Desktop.
Animate Santa Monica City Data
library(dplyr)
library(magrittr)
library(ggmap)
library(readr)
library(animation)
library(lubridate)
parking <- read_csv("csvs/parking.csv")
parking %<>% mutate(wday=wday(Date.Time),
ymd=ymd(paste(year(Date.Time),
month(Date.Time),
day(Date.Time), sep="-")))
al1 = get_map(location = c(lon = -118.4969553, lat = 34.0098871),
zoom = 15,
maptype = 'roadmap')
p1 <- ggmap(al1)
weekdays <- c("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday")
parking %<>% arrange(Date.Time)
saveVideo({
for (day in unique(parking$ymd)) {
parking %>%
filter(ymd==day) %>%
group_by(Latitude, Longitude) %>%
summarise(mean_avail=mean(Available),
pct_filled=mean_avail/max(Available),
min_avail=min(Available),
max_avail=max(Available),
variance=var(Available)) -> mydf
p2 <- p1 + geom_point(aes(x=Longitude, y=Latitude, size=mean_avail, color=variance),
data=mydf, alpha=.5) +
geom_point(aes(x=Longitude, y=Latitude, size=min_avail, color=variance),
data=mydf, alpha=.25) +
geom_point(aes(x=Longitude, y=Latitude, size=max_avail, color=variance),
data=mydf, alpha=.1) +
scale_color_gradient(high="blue", low="green") +
scale_size(range = c(2, 50)) +
ggtitle(paste0("Average Parking Available on ",
strftime(as.POSIXct(day, origin="1970-01-01"), format= "%A, %B %d"))) +
theme(legend.position = "none",
plot.title = element_text(hjust = 1))
print(p2)
}
},
"/tmp/animation.mp4",
interval = 0.2,
ani.width = 1920,
ani.height = 1080
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment