Skip to content

Instantly share code, notes, and snippets.

@mkiang

mkiang/c19_vs_topten.R

Last active Nov 23, 2020
Embed
What would you like to do?
Comparing COVID-19 daily deaths to 2018 top ten causes of death
library(tidyverse)
library(ggrepel)
library(zoo)
source("https://raw.githubusercontent.com/mkiang/opioid_geographic/master/code/mk_nytimes.R")
## 2018 top causes of death:
## https://www.cdc.gov/nchs/fastats/deaths.htm
nchs_df <- tribble(~ cause, ~ deaths_2018,
"1.Heart disease", 655381,
"2. Cancer", 599274,
"3. Accidents", 167127,
"4. Chronic lower respiratory diseases", 159486,
"5. Stroke", 147810,
"6. Alzheimer’s disease", 122019,
"7. Diabetes", 84946,
"8. Influenza and Pneumonia", 59120,
"9. Nephritis, nephrotic syndrome and nephrosis", 51386,
"10. Suicide", 48344
) %>%
mutate(deaths_per_day = deaths_2018 / 365)
nchs_df$x_val <- as.Date(c(
"2020-03-15",
"2020-03-15",
"2020-02-01",
"2020-02-14",
"2020-03-01",
"2020-03-15",
"2020-04-01",
"2020-06-15",
"2020-07-01",
"2020-05-15"
))
covid_df <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv") %>%
mutate(daily_deaths = deaths - lag(deaths, default = 0)) %>%
mutate(rolling_avg = zoo::rollmean(daily_deaths, k = 7, fill = 0, align = "right"))
p1 <- ggplot(data = covid_df,
aes(x = date, y = daily_deaths)) +
geom_col(alpha = .3) +
geom_line(aes(y = rolling_avg), color = "red", size = 1, alpha = .9) +
geom_hline(data = nchs_df,
aes(yintercept = deaths_per_day),
alpha = .5) +
geom_label_repel(data = nchs_df,
aes(x = x_val,
y = deaths_per_day,
label = cause),
force = 5) +
scale_y_continuous("Deaths per day", expand = c(0, .05)) +
scale_x_date(NULL,
date_breaks = "1 month",
date_labels = "%b",
expand = c(0, 0)) +
mk_nytimes() +
labs(title = "COVID-19 daily deaths vs 2018 Top Ten Causes of Death",
subtitle = "Daily COVID-19 deaths (grey bars) and 7-day rolling average (red line). Reference lines are total 2018 deaths divided by 365.",
caption = "COVID data: NYTimes. 2018 mortality data: NCHS. @mathewkiang")
ggsave(
"./c19_vs_top10.jpg",
p1,
width = 15,
height = 5,
scale = 1,
dpi = 300
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment