Skip to content

Instantly share code, notes, and snippets.

@jakeybob
Last active April 13, 2019 16:51
Show Gist options
  • Save jakeybob/14508579d9a965bdc7f8587075d192bb to your computer and use it in GitHub Desktop.
Save jakeybob/14508579d9a965bdc7f8587075d192bb to your computer and use it in GitHub Desktop.
OpenAir Pollution Animation
library(openair)
library(tidyverse)
library(lubridate)
library(gganimate)
jhn <- as_tibble(importSAQN(site = "REN02", year = 2017:2019, pollutant = "all")) %>%
mutate(day = date(date))
# saveRDS(jhn, "jhn.rds")
# jhn <- readRDS("jhn.rds")
pm2.5_size = 1.5 # smallest dot size
p <- jhn %>%
# filter(date >= dmy("01-01-2019"), date < dmy("01-02-2019")) %>%
ggplot(aes(x = date)) +
ylim(0, 100) +
ylab("") +
xlab("") +
scale_x_datetime(date_breaks = "1 day", breaks = NULL) + # this and view_follow fix the range
labs(title = "{format(frame_time, '%B %d %Y')}") +
geom_smooth(colour="black", alpha = 1, aes(y = pm10)) +
geom_smooth(colour="blue", alpha = 1, aes(y = pm2.5)) +
geom_point(colour="black", alpha = .3, aes(y = pm10), size = 4*pm2.5_size) +
geom_point(colour="blue", alpha = .3, aes(y = pm2.5), size = pm2.5_size) +
theme_minimal() +
theme(axis.text.x = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank()) +
# gganimate
transition_time(day) +
enter_fade() +
exit_fade() +
ease_aes("linear") +
view_follow(fixed_y = TRUE)
# output as animated gif
scale = .5
animate(p, width = 1920*scale, height = 1080*scale,
fps = 24, duration = 60)
anim_save("anim.gif")
# output as video
scale = 1
animate(p, width = 1920*scale, height = 1080*scale,
fps = 24, duration = 60, renderer = ffmpeg_renderer())
anim_save("anim.mp4")
@jakeybob
Copy link
Author

jakeybob commented Apr 12, 2019

openair_pollution.R

A quick look at the openair R package (from @davidcarslaw), using it to download my nearest air pollution monitor, then using gganimate to plot the daily cycle of 2.5 and 10 micron particulates for the last couple of years. [video]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment