library("ggplot2")
library("gganimate")
## Warning: package 'gganimate' was built under R version 4.1.3
library("gifski")
## Warning: package 'gifski' was built under R version 4.1.3
library("tidyverse")
## Warning: package 'tidyverse' was built under R version 4.1.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble  3.1.5     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.0.2     v forcats 0.5.1
## v purrr   0.3.4
## Warning: package 'tidyr' was built under R version 4.1.2
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library("magick")
## Warning: package 'magick' was built under R version 4.1.3
## Linking to ImageMagick 6.9.12.3
## Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fontconfig, x11
pm.ca.17 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_ca17.csv",header=T,sep=",",as.is=T)
pm.ca.18 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_ca18.csv",header=T,sep=",",as.is=T)
pm.ca.19 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_ca19.csv",header=T,sep=",",as.is=T)
pm.ca.20 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_ca20.csv",header=T,sep=",",as.is=T)
pm.ca.21 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_ca21.csv",header=T,sep=",",as.is=T)
pm.or.17 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_or17.csv",header=T,sep=",",as.is=T)
pm.or.18 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_or18.csv",header=T,sep=",",as.is=T)
pm.or.19 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_or19.csv",header=T,sep=",",as.is=T)
pm.or.20 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_or20.csv",header=T,sep=",",as.is=T)
pm.or.21 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_or21.csv",header=T,sep=",",as.is=T)
pm.wa.17 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_wa17.csv",header=T,sep=",",as.is=T)
pm.wa.18 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_wa18.csv",header=T,sep=",",as.is=T)
pm.wa.19 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_wa19.csv",header=T,sep=",",as.is=T)
pm.wa.20 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_wa20.csv",header=T,sep=",",as.is=T)
pm.wa.21 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/pm2.5/pm2.5data_wa21.csv",header=T,sep=",",as.is=T)

pm.ca.17$year <- 2017
pm.ca.18$year <- 2018
pm.ca.19$year <- 2019
pm.ca.20$year <- 2020
pm.ca.21$year <- 2021

pm.or.17$year <- 2017
pm.or.18$year <- 2018
pm.or.19$year <- 2019
pm.or.20$year <- 2020
pm.or.21$year <- 2021

pm.wa.17$year <- 2017
pm.wa.18$year <- 2018
pm.wa.19$year <- 2019
pm.wa.20$year <- 2020
pm.wa.21$year <- 2021

pm.ca <- rbind(pm.ca.17,pm.ca.18,pm.ca.19,pm.ca.20,pm.ca.21)
pm.or <- rbind(pm.or.17,pm.or.18,pm.or.19,pm.or.20,pm.or.21)
pm.wa <- rbind(pm.wa.17,pm.wa.18,pm.wa.19,pm.wa.20,pm.wa.21)

pm2.5 <- rbind(pm.ca,pm.or,pm.wa)
so.ca.17 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_ca17.csv",header=T,sep=",",as.is=T)
so.ca.18 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_ca18.csv",header=T,sep=",",as.is=T)
so.ca.19 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_ca19.csv",header=T,sep=",",as.is=T)
so.ca.20 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_ca20.csv",header=T,sep=",",as.is=T)
so.ca.21 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_ca21.csv",header=T,sep=",",as.is=T)
so.or.17 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_or17.csv",header=T,sep=",",as.is=T)
so.or.18 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_or18.csv",header=T,sep=",",as.is=T)
so.or.19 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_or19.csv",header=T,sep=",",as.is=T)
so.or.20 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_or20.csv",header=T,sep=",",as.is=T)
so.or.21 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_or21.csv",header=T,sep=",",as.is=T)
so.wa.17 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_wa17.csv",header=T,sep=",",as.is=T)
so.wa.18 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_wa18.csv",header=T,sep=",",as.is=T)
so.wa.19 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_wa19.csv",header=T,sep=",",as.is=T)
so.wa.20 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_wa20.csv",header=T,sep=",",as.is=T)
so.wa.21 <- read.csv(file="C:/Users/Yu-Chun Cheng/Desktop/OSU/2-spring/ST537/sourcing data/so2/so2data_wa21.csv",header=T,sep=",",as.is=T)

so.ca.17$year <- 2017
so.ca.18$year <- 2018
so.ca.19$year <- 2019
so.ca.20$year <- 2020
so.ca.21$year <- 2021

so.or.17$year <- 2017
so.or.18$year <- 2018
so.or.19$year <- 2019
so.or.20$year <- 2020
so.or.21$year <- 2021

so.wa.17$year <- 2017
so.wa.18$year <- 2018
so.wa.19$year <- 2019
so.wa.20$year <- 2020
so.wa.21$year <- 2021

so.ca <- rbind(so.ca.17,so.ca.18,so.ca.19,so.ca.20,so.ca.21)
so.or <- rbind(so.or.17,so.or.18,so.or.19,so.or.20,so.or.21)
so.wa <- rbind(so.wa.17,so.wa.18,so.wa.19,so.wa.20,so.wa.21)

so2 <- rbind(so.ca,so.or,so.wa)
animation.1 <-ggplot(data=pm2.5,aes(x=STATE,y=Daily.Mean.PM2.5.Concentration))+
  geom_boxplot(aes(fill=STATE),outlier.shape = NA,na.rm=T,show.legend=FALSE)+
  theme_bw()+
  scale_y_continuous(limits=c(0,30))+
  scale_x_discrete( labels=c("California","Oregon","Washington"))+
  ggtitle("Daily mean PM2.5 concentration by state and year")+
  xlab("State")+
  ylab("Daily mean PM2.5 concentration")+
  scale_fill_manual(values = c("skyblue1", "tan1","orchid1"))+
  theme_bw()+
  transition_reveal(year)+
  ease_aes("sine-in-out")
animation.1

anim_save("animation.1.gif", animation = animation.1, path=NULL, renderer = gifski_renderer())
animation.2 <-ggplot(data=so2,aes(x=STATE,y=Daily.Max.1.hour.SO2.Concentration))+
  geom_boxplot(aes(fill=STATE),outlier.shape = NA,na.rm=T,show.legend=FALSE)+
  theme_bw()+
  scale_y_continuous(limits=c(0,5))+
  scale_x_discrete( labels=c("California","Oregon","Washington"))+
  ggtitle("Daily Max 1 hour SO2 Concentration by state and year")+
  xlab("State")+
  ylab("Daily Max 1 hour SO2 Concentration")+
  scale_fill_manual(values = c("skyblue3", "tan3","orchid3"))+
  theme_bw()+
  transition_reveal(year)+
  ease_aes("sine-in-out")+
  transition_states(year,transition_length=3,state_length=1)
animation.2

anim_save("animation.2.gif", animation = animation.2, path=NULL, renderer = gifski_renderer())
a_mgif <- image_read("animation.1.gif")
b_mgif <- image_read("animation.2.gif")

new_gif <- image_append(c(a_mgif[1], b_mgif[1]), stack = FALSE)
for(i in 2:65){
  combined <- image_append(c(a_mgif[i], b_mgif[i]), stack = FALSE)
  new_gif <- c(new_gif, combined)
}
new_gif

anim_save("new.gif", animation = new_gif, path=NULL, renderer = gifski_renderer())