Last active
December 4, 2020 18:49
-
-
Save cddesja/5a03b4727be9131beb55621ff3ed4923 to your computer and use it in GitHub Desktop.
Dawson's Plot
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
covid <- read.csv("https://data.cdc.gov/api/views/y5bj-9g5w/rows.csv?accessType=DOWNLOAD&bom=true&format=true%20target=") | |
names(covid)[1] <- "Jurisdiction" | |
library(dplyr) | |
library(ggplot2) | |
ct <- covid %>% | |
group_by(Week, Year) %>% | |
filter(Type == "Unweighted", Jurisdiction != "United States") %>% | |
summarize(totl = sum(Number.of.Deaths)) | |
labels <- data.frame(Week = c(rep(54, 5), 45) , totl = c(53023, 58328, 61719, 56648, 58994, 57728), lab = 2015:2020) | |
options(scipen = 999) | |
# ggplot's default | |
ct %>% | |
ggplot() + | |
geom_line(aes(x = Week, y = totl, col = as.factor(Year), group = as.factor(Year))) + | |
theme_bw() + | |
theme(legend.position = "none", | |
panel.grid.minor = element_blank()) + | |
scale_color_manual("", values = c(rep("gray", 5), "red")) + | |
geom_text(labels, mapping = aes(x = Week, y = totl, label = lab)) + | |
ylab("All weekly deaths in the U.S. any cause") + | |
xlab("Week of the Year") | |
# with 0 | |
ct %>% | |
ggplot() + | |
geom_line(aes(x = Week, y = totl, col = as.factor(Year), group = as.factor(Year))) + | |
coord_cartesian(ylim = c(0, 80000)) + | |
theme_bw() + | |
theme(legend.position = "none", | |
panel.grid.minor = element_blank()) + | |
geom_text(labels, mapping = aes(x = Week, y = totl, label = lab)) + | |
scale_color_manual("", values = c(rep("gray", 5), "red")) + | |
ylab("All weekly deaths in the U.S. any cause") + | |
xlab("Week of the Year") | |
# deviation from 2015 - 2019 weekly average | |
ct.year <- ct %>% | |
filter(Year != "2020") %>% | |
group_by(Week) %>% | |
summarize(M = mean(totl)) | |
ct.diff <- left_join(ct.year, ct) | |
ct.diff$diff <- ct.diff$totl - ct.diff$M | |
labels <- data.frame(Week = c(rep(54, 5), 45) , totl = c(-4719, 586, 3977, -1094, 1252, 4717), lab = 2015:2020) | |
ct.diff %>% | |
ggplot() + | |
geom_line(aes(x = Week, y = diff, col = as.factor(Year), group = as.factor(Year))) + | |
theme_bw() + | |
theme(legend.position = "none", | |
panel.grid.minor = element_blank()) + | |
geom_text(labels, mapping = aes(x = Week, y = totl, label = lab)) + | |
scale_color_manual("", values = c(rep("gray", 5), "red")) + | |
ylab("Deviations of death from the weekly average of deaths in 2015 - 2019") + | |
xlab("Week of the Year") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for letting me know! My fix should take care of it. Adding
readr::read_csv()
certainly would work, too.