Skip to content

Instantly share code, notes, and snippets.

@shuckle16
Last active August 23, 2020 22:37
Show Gist options
  • Save shuckle16/484b12f35250f89783725e6799750d97 to your computer and use it in GitHub Desktop.
Save shuckle16/484b12f35250f89783725e6799750d97 to your computer and use it in GitHub Desktop.
# scrapes and plots some key covid metrics for florida
library(dplyr)
library(rvest)
library(stringr)
library(ggplot2)
library(tidyr)
STATE_OF_INTEREST <- "Florida"
STATE_OF_INTEREST <- str_to_lower(STATE_OF_INTEREST)
url <- glue::glue("https://covidtracking.com/data/state/{STATE_OF_INTEREST}#historical")
fla_html <- read_html(url)
fla_dat <- fla_html %>% html_table() %>% `[[`(2)
fla_dat <-
fla_dat %>%
rename_all(tolower) %>%
mutate(
date = as.Date(date, "%a %B %d %Y")
) %>%
mutate_at(
vars(-c(date, starts_with("screenshot"))), .funs = function(x) {as.numeric(str_replace_all(x, "[[:punct:]]", ""))}
) %>%
arrange(date) %>%
mutate(
new_positive = c(NA, diff(cases)),
new_hospitalized = c(NA, diff(hospitalized)),
new_deaths = c(NA, diff(deaths))
)
fla_dat %>%
mutate(
frac_positive = new_positive / `new tests`
) %>%
filter(`new tests` > 100) %>%
ggplot(aes(x = date, y = frac_positive)) + geom_point() + geom_line()
fla_dat %>% ggplot(aes(x = date, y = new_deaths)) + geom_point() + geom_line()
fla_dat %>% ggplot(aes(x = date, y = new_positive)) + geom_point() + geom_line()
fla_long <-
fla_dat %>%
dplyr::select(date, new_deaths, new_positive, `new tests`, new_hospitalized) %>%
pivot_longer(cols = c(new_deaths, new_positive, `new tests`, new_hospitalized), names_to = "metric") %>%
group_by(metric) %>%
mutate(
scaled_value = (value - mean(value, na.rm = T)) / sd(value, na.rm = T)
)
fla_long %>%
ggplot(aes(x = date, y = scaled_value, color = metric)) +
geom_point(alpha = .3) +
geom_line(alpha = .3) + geom_ma(n = 7, linetype = "solid", size = 1.3) +
ggtitle(glue::glue("{STATE_OF_INTEREST} covid stats over time"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment