Last active
August 22, 2021 02:42
-
-
Save thoughtfulbloke/edf7e77f32561bdb233aa3e2e58ceb09 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
# Data | |
# https://www.health.govt.nz/our-work/diseases-and-conditions/covid-19-novel-coronavirus/covid-19-data-and-statistics/covid-19-case-demographics#case-details | |
library(readr) | |
library(dplyr) | |
library(ggplot2) | |
library(ggthemes) | |
library(lubridate) | |
library(patchwork) | |
# you need to provide csv name | |
file_name <- "covid_cases_2021-08-22.csv" | |
update_date = as.Date(gsub("[^1234567890-]","",file_name)) | |
# update date needed because any current day info is partial | |
# rather than complete | |
MoH <- read_csv(file_name, | |
col_types = cols( | |
`Report Date` = col_date(format = ""), | |
`Case Status` = col_character(), | |
Sex = col_character(), | |
`Age group` = col_character(), | |
DHB = col_character(), | |
`Overseas travel` = col_character(), | |
Historical = col_character())) | |
complete_sequence <- data.frame(case_date = seq.Date(from=as.Date("2020-08-11"), | |
to=as.Date("2020-09-30"), | |
by="day"), n=0) | |
Aug_Ref <- MoH %>% | |
filter(`Overseas travel` != "Yes", | |
DHB != "Managed Isolation & Quarantine", | |
`Report Date` >= as.Date("2020-08-01"), | |
`Report Date` < as.Date("2020-10-01"), | |
is.na(Historical)) %>% | |
count(case_date = `Report Date`) %>% | |
bind_rows(complete_sequence) %>% | |
arrange(desc(n)) %>% | |
group_by(case_date) %>% | |
slice(1) %>% | |
ungroup() %>% | |
mutate(outbreak_day = as.numeric(difftime(case_date, | |
as.Date("2020-08-10"), | |
units="day")), | |
outbreak = "aug 2020") %>% | |
select(outbreak_day, n, outbreak) | |
current_sequence <- data.frame(case_date = seq.Date(from=as.Date("2021-08-17"), | |
to=Sys.Date() - days(1), | |
by="day"), n=0) | |
graphdaily <- MoH %>% | |
filter(`Overseas travel` != "Yes", | |
DHB != "Managed Isolation & Quarantine", | |
`Report Date` >= as.Date("2021-08-10"), | |
is.na(Historical)) %>% | |
count(case_date = `Report Date`) %>% | |
bind_rows(current_sequence) %>% | |
arrange(desc(n)) %>% | |
group_by(case_date) %>% | |
slice(1) %>% | |
ungroup() %>% | |
mutate(outbreak_day = as.numeric(difftime(case_date, | |
as.Date("2021-08-16"), | |
units="day")), | |
outbreak = "current", | |
Day = ifelse(case_date ==update_date, "Partial", "Complete")) %>% | |
select(outbreak_day, n, outbreak, Day) | |
top <- ggplot(graphdaily,aes(x=outbreak_day, y=n)) + | |
geom_point(size=1) + theme_minimal() + | |
geom_segment(aes(xend=outbreak_day, yend=0, linetype=Day)) + | |
geom_line(data=Aug_Ref, colour="orange", alpha=0.7) + | |
geom_point(data=Aug_Ref, colour="orange", size=0.8, alpha=0.7) + | |
ylab("Daily new\ncases") + xlab("") + | |
ggtitle("Current NZ outbreak (black) vs Aug 2020 non-Delta outbreak (orange). | |
Note:this doesn't show who was in isolation while infectious") + | |
theme(legend.position = "bottom", axis.title.x = element_blank()) | |
top | |
place0 <- data.frame(outbreak_day = c(0,0), | |
n = c(0,0), | |
outbreak = c("aug 2020", "current"), | |
Day = c("Complete", "Complete")) | |
graphcumul <- graphdaily %>% | |
bind_rows(Aug_Ref, place0) %>% | |
arrange(outbreak, outbreak_day) %>% | |
group_by(outbreak) %>% | |
mutate(cummulative_cases = cumsum(n), | |
prev_cases=lag(cummulative_cases), | |
prev_outbreak_day = lag(outbreak_day), | |
Day = ifelse(is.na(Day), "Complete", Day)) %>% | |
ungroup() %>% | |
filter(!is.na(prev_cases)) | |
bottom <- ggplot(graphcumul,aes(x=outbreak_day, y=cummulative_cases, | |
colour=outbreak)) + | |
geom_point(size=0.3) + theme_minimal() + | |
geom_segment(aes(xend=prev_outbreak_day, yend=prev_cases, linetype=Day)) + | |
scale_colour_manual(values=c("orange","black")) + | |
ylab("Cummulative\nCases") + xlab("Day of outbreak")+ | |
theme(legend.position = "none") | |
bottom | |
grf <- top / bottom | |
ggsave(filename="~/Desktop/ouput.png", plot=grf,dpi=72, units="in", | |
height = 5.556, width=9.877) | |
grf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment