Skip to content

Instantly share code, notes, and snippets.

@shuckle16
Created April 23, 2020 18:43
Show Gist options
  • Save shuckle16/af676e7b918547f58415a37905c780ed to your computer and use it in GitHub Desktop.
Save shuckle16/af676e7b918547f58415a37905c780ed to your computer and use it in GitHub Desktop.
scrapes covidtracking.com and plots tests, cases, and deaths over time
library(dplyr)
library(ggplot2)
library(tidyr)
library(stringr)
library(rvest)
usa_html <- read_html("https://covidtracking.com/data/us-daily")
usa_dat <- usa_html %>% html_table() %>% `[[`(1)
usa_dat <-
usa_dat %>%
rename_all(tolower) %>%
mutate(
date = as.Date(date, "%a %B %d %Y")
) %>%
mutate_at(
vars(-c(date)), .funs = function(x) {as.numeric(str_replace_all(x, "[[:punct:]]", ""))}
) %>%
arrange(date) %>%
mutate(
new_positive = c(NA, diff(positive)),
new_deaths = c(NA, diff(deaths))
)
usa_dat %>% ggplot(aes(x = date, y = new_deaths)) + geom_point() + geom_line()
usa_dat %>% ggplot(aes(x = date, y = `new tests`)) + geom_point() + geom_line()
usa_dat %>% ggplot(aes(x = date, y = new_positive / `new tests`)) + geom_point() + geom_line()
usa_dat %>%
dplyr::select(date, new_deaths, new_positive, `new tests`) %>%
pivot_longer(cols = c(new_deaths, new_positive, `new tests`), names_to = "metric") %>%
group_by(metric) %>%
mutate(
scaled_value = (value - mean(value, na.rm = T)) / sd(value, na.rm = T)
) %>%
ggplot(aes(x = date, y = scaled_value, color = metric)) + geom_point(alpha = .3) + geom_line(alpha = .3) + geom_smooth(se = F)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment