Skip to content

Instantly share code, notes, and snippets.

@cddesja
Last active December 4, 2020 18:49
Show Gist options
  • Save cddesja/5a03b4727be9131beb55621ff3ed4923 to your computer and use it in GitHub Desktop.
Save cddesja/5a03b4727be9131beb55621ff3ed4923 to your computer and use it in GitHub Desktop.
Dawson's Plot
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")
@cddesja
Copy link
Author

cddesja commented Dec 4, 2020

What's your OS? I get this

covid <- read.csv("https://data.cdc.gov/api/views/y5bj-9g5w/rows.csv?accessType=DOWNLOAD&bom=true&format=true%20target=")
names(covid)
> names(covid)
 [1] "Jurisdiction"       "Week.Ending.Date"   "State.Abbreviation" "Year"              
 [5] "Week"               "Age.Group"          "Number.of.Deaths"   "Time.Period"       
 [9] "Type"               "Suppress"           "Note"           

@cddesja
Copy link
Author

cddesja commented Dec 4, 2020

Could also add

names(covid)[1] <- "Jurisdiction"

But I'd be curious why it's adding those characters for you.

@bklingen
Copy link

bklingen commented Dec 4, 2020

Yeah, might be OS. I'm running Windows 10.

(And when I just download the file and open it in Excel, I do get "Jurisdiction".) Strange.

@cddesja
Copy link
Author

cddesja commented Dec 4, 2020

Thanks for letting me know! My fix should take care of it. Adding readr::read_csv() certainly would work, too.

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