Skip to content

Instantly share code, notes, and snippets.

@raybuhr
Last active May 13, 2020 23:04
Show Gist options
  • Save raybuhr/513c629d019850ccbd3f0620d3ea55ef to your computer and use it in GitHub Desktop.
Save raybuhr/513c629d019850ccbd3f0620d3ea55ef to your computer and use it in GitHub Desktop.
fetch and plot covid cases and tests to plot
## This is my variation on Chase's first pass
## https://gist.github.com/chasemc/1a6978d500aa47268ecaf9f99719af61
library(htmltab)
library(reshape2)
library(ggplot2)
data_url <- "https://web.archive.org/web/20200513204922/http://covidtracking.com/data/state/illinois"
a <- htmltab(doc = data_url, which = 4)
a$date <- as.Date(a$Date, "%a %b %d %Y")
a$new_tests <- as.integer(gsub(x = a$`New Tests`, pattern = ",", replacement = ""))
a$positive <- as.integer(gsub(x = a$Positive, pattern = ",", replacement = ""))
plot_data = melt(a[, c("date", "new_tests", "positive")], id.vars = "date")
p <- ggplot(plot_data, aes(x=date, y=value, color=variable)) +
geom_line() +
scale_color_brewer(palette = "Set1", direction = -1) +
scale_x_date(date_breaks = "2 weeks", name = "") +
theme_minimal() +
theme(legend.title = element_blank())
p1 <- p + scale_y_continuous(breaks = seq(0, 100000, 10000),
labels = scales::comma(seq(0, 100000, 10000)),
name = "")
p2 <- p + scale_y_log10(name="Log10 Scale", labels = scales::comma)
gridExtra::grid.arrange(p1, p2)
il_cases <- plot_data[plot_data$variable == "positive", c(1, 3)]
il_cases <- il_cases[order(il_cases$date), ]
row.names(il_cases) <- seq_along(il_cases$date)
colnames(il_cases) <- c("date", "positive")
il_cases$new_cases <- c(NA, diff(il_cases$positive, 1))
il_cases$pct_change <- il_cases$positive / (il_cases$positive - il_cases$new_cases) - 1
ggplot(il_cases[il_cases$date >= "2020-04-01",],
aes(x=date, y=pct_change)) +
geom_line() +
scale_x_date(date_breaks = "1 week") +
scale_y_continuous(labels = scales::percent) +
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment