Last active
August 4, 2021 19:54
-
-
Save turgeonmaxime/f7c5e92d6ffb08dedca2d66721b783e5 to your computer and use it in GitHub Desktop.
Visualize vaccine uptake in Saskatchewan using the Government's official numbers
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(tidyverse) | |
library(RcppRoll) | |
library(rvest) | |
# Create temporary directory | |
tmp_dir <- tempdir() | |
file_path <- paste0(tmp_dir, "/vaccine_sk.csv") | |
# Download file | |
# Note: the filename changes everyday, so scrape the URL | |
base_url <- "https://dashboard.saskatchewan.ca" | |
file_url <- paste0(base_url, "/health-wellness/covid-19-vaccines/vaccines") |> | |
read_html() |> | |
html_nodes("div.indicator-export a") |> | |
html_attr("href") |> | |
str_subset("csv$") | |
download.file(paste0(base_url, file_url), | |
file_path) | |
# Read-in data | |
# Note: There are a lot of empty rows at the beginning, and therefore | |
# we need to read in a lot of rows to guess the data types | |
data <- read_csv(file_path, guess_max = 7500) | |
# Sum over all regions and compute rolling averages using RcppRoll | |
# Note: You may need to change the pipe operator if using older version of R | |
data <- data |> | |
group_by(Date) |> | |
summarise(first_dose = sum(`New First Vaccine Doses`, na.rm = TRUE), | |
full_dose = sum(`New Fully Vaccinated`, na.rm = TRUE), | |
total_dose = first_dose + full_dose) |> | |
mutate(first_dose = roll_mean(first_dose, n = 7, | |
fill = NA, | |
align = "right"), | |
full_dose = roll_mean(full_dose, n = 7, | |
fill = NA, | |
align = "right"), | |
total_dose = roll_mean(total_dose, n = 7, | |
fill = NA, | |
align = "right")) |> | |
filter(first_dose + full_dose > 0) # Remove empty rows at the top | |
# Create long dataset for plotting separate lines | |
data |> | |
pivot_longer(!Date, names_to = "type", | |
values_to = "count") |> | |
mutate(type = factor(type, | |
levels = c("first_dose", "full_dose", "total_dose"), | |
labels = c("First dose", "Fully vacc.", "Total"))) |> | |
ggplot(aes(x = Date, y = count, | |
colour = type)) + | |
geom_line() + | |
theme_bw() + | |
scale_colour_manual(name = "", values = c(colorspace::qualitative_hcl(2), "black")) + | |
theme(legend.position = "top") + | |
labs(caption = "Source: https://dashboard.saskatchewan.ca/health-wellness") + | |
xlab("") + ylab("Number of doses (7-day avg)") + | |
scale_y_continuous(labels = scales::comma) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output from the code above (ran on
2021-08-04 12:07:57 MDT
):