Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. library(openair)
  2. library(tidyverse)
  3. library(lubridate)
  4. library(gganimate)
  5.  
  6. jhn <- as_tibble(importSAQN(site = "REN02", year = 2017:2019, pollutant = "all")) %>%
  7. mutate(day = date(date))
  8. # saveRDS(jhn, "jhn.rds")
  9. # jhn <- readRDS("jhn.rds")
  10.  
  11. pm2.5_size = 1.5 # smallest dot size
  12.  
  13. p <- jhn %>%
  14. # filter(date >= dmy("01-01-2019"), date < dmy("01-02-2019")) %>%
  15. ggplot(aes(x = date)) +
  16. ylim(0, 100) +
  17. ylab("") +
  18. xlab("") +
  19. scale_x_datetime(date_breaks = "1 day", breaks = NULL) + # this and view_follow fix the range
  20. labs(title = "{format(frame_time, '%B %d %Y')}") +
  21. geom_smooth(colour="black", alpha = 1, aes(y = pm10)) +
  22. geom_smooth(colour="blue", alpha = 1, aes(y = pm2.5)) +
  23. geom_point(colour="black", alpha = .3, aes(y = pm10), size = 4*pm2.5_size) +
  24. geom_point(colour="blue", alpha = .3, aes(y = pm2.5), size = pm2.5_size) +
  25. theme_minimal() +
  26. theme(axis.text.x = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank()) +
  27. # gganimate
  28. transition_time(day) +
  29. enter_fade() +
  30. exit_fade() +
  31. ease_aes("linear") +
  32. view_follow(fixed_y = TRUE)
  33.  
  34. # output as animated gif
  35. scale = .5
  36. animate(p, width = 1920*scale, height = 1080*scale,
  37. fps = 24, duration = 60)
  38. anim_save("anim.gif")
  39.  
  40. # output as video
  41. scale = 1
  42. animate(p, width = 1920*scale, height = 1080*scale,
  43. fps = 24, duration = 60, renderer = ffmpeg_renderer())
  44. anim_save("anim.mp4")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement