Skip to content

Instantly share code, notes, and snippets.

@ayman
Last active March 31, 2020 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayman/39d574303a7973080de47ca1acd49754 to your computer and use it in GitHub Desktop.
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.
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")
@ayman
Copy link
Author

ayman commented Mar 22, 2020

Downloads live data at runtime and makes a chart like this.
image

@ayman
Copy link
Author

ayman commented Mar 31, 2020

March 31st.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment