knitr::opts_chunk$set(echo = TRUE)
library(tidyverse) library(readxl) library(here) library(janitor) library(lubridate)
nswgov <- read_csv(here::here("data", "nswgov_daily.csv")) %>% clean_names()
nswgov$date <- dmy(nswgov$date) glimpse(nswgov)
nswgov_long <- nswgov %>% select(1:27) %>% pivot_longer(names_to = "site", values_to = "aqi", 2:27) %>% mutate(category = case_when(between(aqi, 0, 33) ~ "Very Good", between(aqi, 34, 66) ~ "Good", between(aqi, 67, 99) ~ "Fair", between(aqi, 100, 149) ~ "Poor", between(aqi, 150, 200) ~ "Very Poor", (aqi > 200 ~ "Hazardous"))) nswgov_long$category <- factor(nswgov_long$category, levels = c("Very Good","Good","Fair","Poor","Very Poor","Hazardous"))
nswgov_long %>% ggplot(aes(x = date, y = aqi, colour = site)) + geom_point() + theme(legend.position = "none")
this_site <- "sydney_central_east_raqi_24_hour_index" nswgov_long_this <- nswgov_long %>% # filter(date > "2018-12-31", site==this_site) %>% filter(site==this_site) %>% na.omit() %>% mutate(month=month(date))
nswgov_long_month <- nswgov_long_this %>% group_by(month) %>% summarize(mean = mean(aqi), q1 = quantile(aqi,0.05), q2 = quantile(aqi,0.50), q3 = quantile(aqi,0.95)) nswgov_long_merge <- merge(nswgov_long_this, nswgov_long_month)
g <- nswgov_long_merge %>% filter(date > "2018-12-31") %>% ggplot(aes(x = date, y = aqi)) + geom_point(col="gray", size=1.2) + # geom_line(aes(x = date, y = q1, col="red"), size=1) + # geom_line(aes(x = date, y = q2, col="red"), size=1) + geom_line(aes(x = date, y = q3), size=1, col="red") + theme(legend.position = "none") + theme_bw() + xlab("year") + ylab("Daily avarage") + scale_y_log10() g
library(tidyverse) library(gganimate) library(waffle) devtools::session_info()
#g <- ggplot(nswgov_long, aes(date, site, fill=log10(aqi))) + # geom_tile() + # theme_bw() + # transition_reveal(along=date) nswgov_long2 <- nswgov_long %>% filter(date > "2018-12-31") %>% filter(!(site%in%c("research_monitoring_raqi_24_hour_index","northern_tablelands_raqi_24_hour_index"))) %>% mutate(site=stringr::str_remove(site,"raqi_24_hour_index")) %>% mutate(site=stringr::str_replace_all(site,"_"," ")) nswgov_long2$site <- factor(nswgov_long2$site, levels = c("sydney central east ", "sydney north west ", "sydney south west ", "illawarra ", "lower hunter ", "central tablelands ", "south west slopes ", "north west slopes ", "upper hunter muswellbrook ", "upper hunter singleton ", "upper hunter maison dieu ", "upper hunter singleton nw ", "upper hunter camberwell ", "upper hunter bulga ", "upper hunter mt thorley ", "upper hunter muswellbrook nw ", "upper hunter wybong ", "upper hunter aberdeen ", "upper hunter singleton south ", "upper hunter jerrys plains ", "upper hunter warkworth ", "upper hunter merriwa ", "central coast ", "newcastle local ")) jet.colors <- colorRampPalette(c("#F0FFFF", "cyan", "#007FFF", "yellow", "#FFBF00", "orange", "red", "#7F0000"), bias = 2.25) p_anim <- ggplot(nswgov_long2, aes(x = date, y = site, fill = category)) + geom_tile(size = 0.35) + scale_fill_manual(values=c("#31add3","#99b964","#ffd236","#ec783a","#782d49","#d04730")) + # scale_x_continuous(expand = c(0,0)) + # scale_fill_gradientn(colors = jet.colors(16), na.value = 'white') + theme_minimal() + transition_time(date) + shadow_mark() # annotate(geom = "text", x = 1963.5, y = 50.5, label = "Vaccine introduced", size = 5, hjust = 0) + # ggtitle("Incidence of Meases (USA) \n1928-2011") + # geom_vline(xintercept = 1963, col = "black") + # labs(x = "Year", y = "State", fill = "Rate\n(Cases per 100,000)") anim_save(p_anim, width = 1000, height = 600, width = 100, filename = "aqi_nsw.gif", end_pause=50)
nswgov_long2 <- nswgov_long %>% filter(!(site%in%c("research_monitoring_raqi_24_hour_index","northern_tablelands_raqi_24_hour_index"))) %>% mutate(site=stringr::str_remove(site,"raqi_24_hour_index")) %>% mutate(site=stringr::str_replace_all(site,"_"," ")) nswgov_long2$site <- factor(nswgov_long2$site, levels = c("sydney central east ", "sydney north west ", "sydney south west ", "illawarra ", "lower hunter ", "central tablelands ", "south west slopes ", "north west slopes ", "upper hunter muswellbrook ", "upper hunter singleton ", "upper hunter maison dieu ", "upper hunter singleton nw ", "upper hunter camberwell ", "upper hunter bulga ", "upper hunter mt thorley ", "upper hunter muswellbrook nw ", "upper hunter wybong ", "upper hunter aberdeen ", "upper hunter singleton south ", "upper hunter jerrys plains ", "upper hunter warkworth ", "upper hunter merriwa ", "central coast ", "newcastle local ")) p_anim <- ggplot(nswgov_long2, aes(x = date, y = site, fill = category)) + geom_tile(size = 0.35) + geom_vline(xintercept = ymd("2014-01-01"), size=2, col="white") + geom_vline(xintercept = ymd("2015-01-01"), size=2, col="white") + geom_vline(xintercept = ymd("2016-01-01"), size=2, col="white") + geom_vline(xintercept = ymd("2017-01-01"), size=2, col="white") + geom_vline(xintercept = ymd("2018-01-01"), size=2, col="white") + geom_vline(xintercept = ymd("2019-01-01"), size=2, col="white") + scale_fill_manual(values=c("#31add3","#99b964","#ffd236","#ec783a","#782d49","#d04730")) + # scale_x_continuous(expand = c(0,0)) + # scale_fill_gradientn(colors = jet.colors(16), na.value = 'white') + theme_minimal() + transition_time(date) + shadow_mark() # annotate(geom = "text", x = 1963.5, y = 50.5, label = "Vaccine introduced", size = 5, hjust = 0) + # ggtitle("Incidence of Meases (USA) \n1928-2011") + # geom_vline(xintercept = 1963, col = "black") + # labs(x = "Year", y = "State", fill = "Rate\n(Cases per 100,000)") anim_save(p_anim, width = 1000, height = 600, width = 100, filename = "aqi_nsw_since2014.gif", end_pause=50) #library(beepr) #beepr::beep(8)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.