Created
February 12, 2022 03:09
-
-
Save thoughtfulbloke/f29fdb8ec90376c3af1b0d99a2267a02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(readr) | |
library(dplyr) | |
library(lubridate) | |
library(RcppRoll) | |
library(ggplot2) | |
library(ggthemes) | |
library(sf) | |
library(rmapshaper) | |
library(tidyr) | |
library(patchwork) | |
colset <- colorblind_pal()(6) | |
south_island <- c("Canterbury", "Nelson Marlborough", "South Canterbury", | |
"Southern", "West Coast") | |
DHBpop <- read_csv("../misc_data/DHBpop.csv") %>% | |
arrange(DHB) | |
# daily full demographic csvs folder | |
cases_folder <- list.files("../daily_all_cases", pattern = "csv$", full.names = TRUE) | |
all_csvs <- file.info(cases_folder) | |
# find most recently created csv file | |
latest_csv <- row.names(all_csvs)[which.max(all_csvs$ctime)] | |
all_cases <- read_csv(latest_csv, col_types= cols( | |
`Report Date` = col_date(format = ""), | |
.default = col_character())) | |
nz <- all_cases %>% | |
filter(DHB != "Managed Isolation & Quarantine", | |
is.na(Historical)) %>% | |
count(DHB,Date = `Report Date`) %>% | |
spread(key=DHB, value=n, fill = 0) %>% | |
gather(key="DHB", value="n", Auckland:Whanganui) %>% | |
arrange(DHB, desc(Date)) %>% | |
group_by(DHB) %>% | |
slice(2:9) %>% | |
summarise(tnew = paste("from", Date[4], "to", Date[1], "inclusive"), | |
told = paste("previous 4 days", Date[8], "to", Date[5], "inclusive"), | |
nnew = sum(n[1:4]), | |
nold = sum(n[5:8])) %>% | |
arrange(DHB) %>% | |
mutate(name2 = DHBpop$DHB, | |
population = DHBpop$pop2021, | |
percent_new = 100 * nnew/population, | |
diff_to_old = percent_new - 100 * nold/population) | |
# DHB boundaries map from Koordinates | |
DHB_map <- st_read("../misc_data/nzdhb.kml", quiet=TRUE) | |
# simplify the graph to what is visually useful | |
# this is a figure cm tall, and doesn't need life sized detail | |
DHBborder <- ms_simplify(DHB_map, keep=0.05) %>% | |
arrange(Name) | |
nzset <- bind_cols(nz, DHBborder) | |
# graphs, 4 figures patchworked together | |
gnd <- nzset %>% | |
filter(!(DHB %in% south_island)) | |
g1g <- ggplot(gnd, aes(geometry=geometry, fill=percent_new)) + | |
geom_sf(size=0.1, colour="black") + | |
scale_fill_gradient(limits=c(0,max(nzset$percent_new)), | |
low="white", high="darkred", name = "Percent") + | |
theme_void() + | |
labs(title="North Island, percent of DHB population\ninfected in four days", | |
subtitle=nzset$tnew[1]) | |
g1g | |
gsd <- nzset %>% | |
filter(DHB %in% south_island) | |
g2g <- ggplot(gsd, aes(geometry=geometry, fill=percent_new)) + | |
geom_sf(size=0.1, colour="black") + | |
scale_fill_gradient(limits=c(0,max(nzset$percent_new)), | |
low="white", high="darkred", name = "Percent") + | |
theme_void() + | |
labs(title="South Island, percent of population\ninfected in four days", | |
subtitle=nzset$tnew[1]) | |
g2g | |
g3g <- ggplot(gnd, aes(geometry=geometry, fill=diff_to_old)) + | |
geom_sf(size=0.1, colour="black") + | |
scale_fill_gradient2(limits=c(min(nzset$diff_to_old),max(nzset$percent_new)), | |
low="darkblue", mid="white", high="darkred", name = "Percent") + | |
theme_void() + | |
labs(title="NI, change from previous four days\nas percent of DHB population", | |
subtitle=nzset$told[1]) | |
g3g | |
g4g <- ggplot(gsd, aes(geometry=geometry, fill=diff_to_old)) + | |
geom_sf(size=0.1, colour="black") + | |
scale_fill_gradient2(limits=c(min(nzset$diff_to_old),max(nzset$percent_new)), | |
low="darkblue", mid="white", high="darkred", name = "Percent") + | |
theme_void() + | |
labs(title="SI, change from previous four days\nas percent of DHB population", | |
subtitle=nzset$told[1]) | |
g4g | |
g5 <- (g1g + g2g) / (g3g + g4g) | |
ggsave(filename="~/Desktop/maps.png", plot=g5,dpi=72, units="in", | |
bg="white", height = 5.556 * 1.6, width=9.877* 1.6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment