Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
Created September 14, 2021 05:07
Show Gist options
  • Save thoughtfulbloke/6206ae59648851cf7ac12e7e8c6df10c to your computer and use it in GitHub Desktop.
Save thoughtfulbloke/6206ae59648851cf7ac12e7e8c6df10c to your computer and use it in GitHub Desktop.
R code for graph showing NZ lockdown vs. Cases
# MoH is the ministry of Health all cases demographics csv file
# 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)
# you need to provide csv name
file_name <- "covid_cases_2021-09-14.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-02-1"),
to=as.Date("2021-09-13"),
by="day"), n=0)
NZexAuck <- MoH %>%
filter(`Overseas travel` != "Yes",
!(DHB %in% c("Managed Isolation & Quarantine",
"Auckland","Counties Manukau",
"Waitemata")),
is.na(Historical)) %>%
count(case_date = `Report Date`) %>%
bind_rows(complete_sequence) %>%
arrange(desc(n)) %>%
group_by(case_date) %>%
slice(1) %>%
ungroup() %>%
mutate(n = ifelse(n=0,0,1), region="NZ excluding Auckland")
NZexAuck <- MoH %>%
filter(`Overseas travel` != "Yes",
!(DHB %in% c("Managed Isolation & Quarantine",
"Auckland","Counties Manukau",
"Waitemata")),
is.na(Historical)) %>%
count(case_date = `Report Date`) %>%
bind_rows(complete_sequence) %>%
arrange(desc(n)) %>%
group_by(case_date) %>%
slice(1) %>%
ungroup() %>%
mutate(Legend = ifelse(n==0,"No new cases","Some new cases"), region="NZ excluding\nAuckland") %>% select(-n)
Auckland <- MoH %>%
filter(`Overseas travel` != "Yes",
DHB %in% c("Auckland","Counties Manukau",
"Waitemata"),
is.na(Historical)) %>%
count(case_date = `Report Date`) %>%
bind_rows(complete_sequence) %>%
arrange(desc(n)) %>%
group_by(case_date) %>%
slice(1) %>%
ungroup() %>%
mutate(Legend = ifelse(n==0,"No new cases","Some new cases"), region="Auckland NZ") %>% select(-n)
bind_rows(Auckland, NZexAuck) %>%
mutate(Legend=factor(Legend), region=factor(region)) %>%
ggplot(aes(x=case_date, y=as.numeric(region), colour=Legend)) +
geom_segment(aes(yend=as.numeric(region)+.1, xend=case_date))
# for my purposes Lockdown is any day containing 2 or more minutes of level 3 or 4
lockdown1NZ <- data.frame(case_date = seq.Date(from=as.Date("2020-03-26"),
to=as.Date("2020-05-13"),
by="day"), Legend="Lockdown", stringsAsFactors = FALSE)
lockdownAU1 <- data.frame(case_date = seq.Date(from=as.Date("2020-03-26"),
to=as.Date("2020-05-13"),
by="day"), Legend="Lockdown", stringsAsFactors = FALSE)
lockdownAU2 <- data.frame(case_date = seq.Date(from=as.Date("2020-08-12"),
to=as.Date("2020-08-30"),
by="day"), Legend="Lockdown", stringsAsFactors = FALSE)
lockdownAU3 <- data.frame(case_date = seq.Date(from=as.Date("2021-02-28"),
to=as.Date("2021-03-07"),
by="day"), Legend="Lockdown", stringsAsFactors = FALSE)
lockdown2NZ <- data.frame(case_date = seq.Date(from=as.Date("2021-08-18"),
to=as.Date("2021-09-07"),
by="day"), Legend="Lockdown", stringsAsFactors = FALSE)
lockdownAU4 <- data.frame(case_date = seq.Date(from=as.Date("2021-08-18"),
to=as.Date("2021-09-13"),
by="day"), Legend="Lockdown", stringsAsFactors = FALSE)
Freedom <- data.frame(case_date = seq.Date(from=as.Date("2020-02-01"),
to=as.Date("2021-09-13"),
by="day"), Legend="No Lockdown", stringsAsFactors = FALSE)
lockauck <- bind_rows(lockdownAU1, lockdownAU2, lockdownAU3, lockdownAU4, Freedom) %>%
arrange(case_date, Legend) %>% group_by(case_date) %>% slice(1) %>% ungroup() %>%
mutate(region = "Auckland NZ Lockdown")
locknz <- bind_rows(lockdown1NZ, lockdown2NZ, Freedom) %>%
arrange(case_date, Legend) %>% group_by(case_date) %>% slice(1) %>% ungroup() %>%
mutate(region = "NZ excluding\nAuckland Lockdown")
grf <- bind_rows(Auckland, NZexAuck, lockauck, locknz) %>%
mutate(Legend=factor(Legend), region=factor(region)) %>%
ggplot(aes(x=case_date, y=as.numeric(region), colour=Legend)) +
geom_segment(aes(yend=as.numeric(region)+.5, xend=case_date), size=1.5) +
scale_colour_colorblind(name="Legend:") +
annotate("text", y=1.6, x=as.Date("2020-03-01"), label="Auckland Days with New Covid Cases", hjust=0) +
annotate("text", y=2.6, x=as.Date("2020-03-01"), label="Auckland Days with Lockdown (Level 4 or Level 3)", hjust=0) +
annotate("text", y=3.6, x=as.Date("2020-03-01"), label="NZ ex. Auckland Days with New Covid Cases", hjust=0) +
annotate("text", y=4.6, x=as.Date("2020-03-01"), label="NZ ex. Auckland Days with Lockdown (Level 4 or Level 3)", hjust=0) +
labs(x="Date", title="NZ lockdown days and new cases days, Feb 2020 to present",
caption ="data source: govt.nz") +
guides(colour = guide_legend(override.aes = list(size=3)))
davidise_graph(grf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment