Skip to content

Instantly share code, notes, and snippets.

@turgeonmaxime
Last active August 8, 2020 19:23
Show Gist options
  • Save turgeonmaxime/ef975c801104c252fd4f676744048086 to your computer and use it in GitHub Desktop.
Save turgeonmaxime/ef975c801104c252fd4f676744048086 to your computer and use it in GitHub Desktop.
Visualization of COVID-19 in Canada
library(tidyverse)
library(lubridate)
library(readr)
data_hr <- read_csv("https://raw.githubusercontent.com/ishaberry/Covid19Canada/master/timeseries_hr/cases_timeseries_hr.csv")
# Coerce string to dates and factor provinces
data_cum <- data_hr %>%
filter(province %in% c("Alberta", "Saskatchewan")) %>%
mutate(date_report = lubridate::dmy(date_report)) %>%
group_by(province, date_report) %>%
summarise(cases = sum(cases, na.rm = TRUE)) %>%
mutate(cum_cases = cumsum(cases)) %>%
filter(cum_cases != 0)
data_cum %>%
mutate(cases = if_else(province == "Alberta",
cases/4.371, cases/1.174)) %>%
group_by(province) %>%
mutate(roll_avg = RcppRoll::roll_mean(cases, n = 5,
fill = NA,
align = "right")) %>%
ggplot(aes(date_report, roll_avg, colour = province)) +
geom_line() +
theme_bw() +
xlab("") + ylab("Daily Cases/1M (5 day average)") +
theme(legend.position = "top") +
labs(caption = paste("Data downloaded from",
"https://github.com/ishaberry/Covid19Canada"),
colour = "")
library(tidyverse)
library(lubridate)
library(readr)
library(ggbeeswarm)
# Download data on cases by health region
data_hr <- read_csv("https://raw.githubusercontent.com/ishaberry/Covid19Canada/master/timeseries_hr/cases_timeseries_hr.csv")
# Filter for province, coerce string to dates, and add variable
# to highlight certain regions
data_atl <- data_hr %>%
filter(province %in% c("New Brunswick", "Nova Scotia",
"PEI", "NL")) %>%
mutate(date_report = lubridate::dmy(date_report))
date_range <- data_atl %>%
filter(cases != 0) %>%
pull(date_report) %>%
range
plot_atl <- purrr::map(c("New Brunswick", "Nova Scotia",
"PEI", "NL"),
function(prov) {
data_prov <- filter(data_atl,
province == prov)
total_cases <- data_prov %>% pull(cases) %>% sum
num_hrs <- data_prov %>%
filter(health_region != "Not Reported") %>%
count(health_region) %>% nrow
data_prov %>%
filter(cases != 0,
health_region != "Not Reported") %>%
uncount(cases) %>%
ggplot(aes(x = date_report,
y = health_region,
colour = health_region)) +
geom_quasirandom(groupOnX = FALSE) +
expand_limits(x = today()) +
xlim(date_range) +
theme_bw() +
theme(legend.position = 'none',
panel.grid = element_blank(),
plot.subtitle = element_text(face = "italic")) +
geom_hline(yintercept = 0.5 + seq_len(num_hrs - 1),
linetype = 'dashed',
colour = 'grey60',
size = 0.1) +
xlab("") + ylab("") +
labs(title = paste0(prov, " (Total COVID cases = ",
scales::comma(total_cases), ")"))
})
cowplot::plot_grid(plot_atl[[1]], plot_atl[[2]],
plot_atl[[3]], plot_atl[[4]],
ncol = 2)
library(tidyverse)
library(lubridate)
library(readr)
library(ggbeeswarm)
# Change name of province as required
prov_name <- "Saskatchewan"
# Download data on cases by health region
data_hr <- read_csv("https://raw.githubusercontent.com/ishaberry/Covid19Canada/master/timeseries_hr/cases_timeseries_hr.csv")
# Filter for province, coerce string to dates, and add variable
# to highlight certain regions
data_prov <- data_hr %>%
filter(province == prov_name) %>%
mutate(date_report = lubridate::dmy(date_report))
total_cases <- data_prov %>% pull(cases) %>% sum
num_hrs <- data_prov %>% filter(health_region != "Not Reported") %>%
count(health_region) %>% nrow
min_date <- data_prov %>%
filter(cases > 0) %>%
pull(date_report) %>%
min
data_prov %>%
filter(cases != 0,
health_region != "Not Reported") %>%
uncount(cases) %>%
ggplot(aes(x = date_report,
y = health_region,
colour = health_region)) +
geom_quasirandom(groupOnX = FALSE) +
expand_limits(x = today()) +
theme_bw() +
theme(legend.position = 'none',
panel.grid = element_blank(),
plot.subtitle = element_text(face = "italic")) +
geom_hline(yintercept = 0.5 + seq_len(num_hrs - 1),
linetype = 'dashed',
colour = 'grey60',
size = 0.1) +
xlab("") + ylab("") +
labs(title = paste0("COVID-19 cases in ", prov_name,
" (Total = ", scales::comma(total_cases), ")"),
subtitle = paste("Effective", data_prov %>% pull(date_report) %>% max),
caption = paste("Data downloaded from",
"https://github.com/ishaberry/Covid19Canada"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment