Last active
March 31, 2020 23:24
-
-
Save ayman/39d574303a7973080de47ca1acd49754 to your computer and use it in GitHub Desktop.
A simple R ggplot of percent positive of covid cases by USA State.
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("ggplot2") | |
dataUrl <- "https://covidtracking.com/api/states/daily.csv" | |
c <- read.csv(dataUrl) | |
c$date <- as.Date(as.character(c$date), format = "%Y%m%d") | |
c_tmp <- c[order(c[c$date == max(c$date), ]$total, decreasing = TRUE), ] | |
states <- c_tmp[c_tmp$date == max(c_tmp$date), ]$state[1:10] | |
c[is.na(c$pending), ]$pending <- 0 | |
c$ratio <- (c$positive / (c$total - c$pending)) | |
c <- c[c$state %in% states, ] | |
g <- ggplot(c, aes(x = date, y = ratio)) | |
g <- g + labs(title = "Percent of COVID-19 Positive Cases Out of Total Tested", | |
subtitle = "(Top 10 states with the most tests conducted)", | |
caption = "Pending Tests Excluded.") | |
g <- g + xlab("Date") + ylab("Percent Tested Positive") | |
g <- g + geom_line(aes(color = state)) | |
g <- g + geom_label(aes(fill = factor(state), label = state), | |
position = position_jitter(width = .2, height = 0.01), | |
colour = "white", | |
size = 2) | |
g <- g + scale_y_continuous(labels = function(x) { | |
paste(round(x * 100), "%", sep = "") | |
}) | |
g <- g + theme(legend.position = "none") | |
(g) | |
## ggsave("covid.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Downloads live data at runtime and makes a chart like this.