Skip to content

Instantly share code, notes, and snippets.

@geneorama
Created October 27, 2020 21:14
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 geneorama/d3a0832392e45ad8abbf842e19288d74 to your computer and use it in GitHub Desktop.
Save geneorama/d3a0832392e45ad8abbf842e19288d74 to your computer and use it in GitHub Desktop.
Yet another COVID 19 map - Chicago area cases, hospitalizations, and deaths (normalized for peak)
## Author Gene Leynes
## Date: Oct 27, 2020
## Best run with R (Free) in R Studio (Free)
## Download R and R Studio to reproduce.
## Available for windows, mac, linux
## If you don't have these packages you can install them with this code:
## install.packages("data.table")
## install.packages("magrittr")
## install.packages("plotly")
## install.packages("devtools")
## devtools::install_github("geneorama::geneorama")
##
## They are free, free, free, free, and free. They also install several
## dependencies... which are free.
library(data.table)
library(magrittr)
library(plotly, warn.conflicts = F, quietly = TRUE)
library(geneorama)
## Download from the data portal
## Link to human readable page: https://data.cityofchicago.org/resource/naz8-j4nc
dat <- fread("https://data.cityofchicago.org/api/views/naz8-j4nc/rows.csv?accessType=DOWNLOAD")
## Convert column names to be R friendly
colnames(dat) %>%
gsub(" - ","_", .) %>%
gsub(" ","_", .) %>%
gsub("-","_", .) %>%
gsub("/","_", .) %>%
tolower() %>%
setnames(dat, .) %>%
.[ , date := as.IDate(date, "%m/%d/%Y")]
## Define rolling average period
period <- 7
## Create plot data... order the data, remove na dates, calculate MA's
plotdata <- dat %>%
.[order(date)] %>%
.[ , list(date,
cases = ma(cases_total, p = period),
hospitalizations = ma(hospitalizations_total, p = period),
deaths = ma(deaths_total, p = period))] %>%
.[date>"2020-03-16"]
## The normalization part
plotdata[ , cases := cases / max(cases, na.rm = TRUE)]
plotdata[ , hospitalizations := hospitalizations / max(hospitalizations, na.rm = TRUE)]
plotdata[ , deaths := deaths / max(deaths, na.rm = TRUE)]
## Create and print the figure
fig <- plot_ly() %>%
add_lines(x = plotdata$date, y = plotdata$cases, name = "Cases") %>%
add_lines(x = plotdata$date, y = plotdata$hospitalizations, name = "Hospitalizations") %>%
add_lines(x = plotdata$date, y = plotdata$deaths, name = "Deaths") %>%
layout(title = "Chicago COVID Cases, Deaths, and Hospitalizations")
fig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment