Last active
May 7, 2020 03:24
-
-
Save jonocarroll/e92b06947c499fbeb729df7f8bcd10ca to your computer and use it in GitHub Desktop.
USA daily deaths with and without NY, highlighting weekends
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
library(COVID19) | |
library(dplyr) | |
library(zoo) # rollmean | |
library(ggplot2) | |
library(ggeasy) # easy_add_legend_title | |
#### deaths #### | |
## gather USA data at state level and identify weekends | |
us <- covid19("USA", level = 2) %>% | |
select(id, date, deaths) %>% | |
mutate(day = weekdays(date), | |
weekend = day %in% c("Saturday", "Sunday", "Monday")) | |
## sum deaths by date across all states | |
all <- us %>% | |
group_by(id) %>% | |
arrange(date) %>% | |
mutate(newdeaths = deaths - lag(deaths, default = 0)) %>% | |
group_by(date) %>% | |
summarise(totalnewdeaths = sum(newdeaths), day = unique(day), weekend = unique(weekend)) %>% | |
mutate(roll = c(rep(0, 6), rollmean(totalnewdeaths, 7, align = "right"))) %>% | |
mutate(id = "USA, total") | |
## sum deaths in NY | |
ny <- us %>% | |
filter(id == "USA, NY") %>% | |
mutate(totalnewdeaths = deaths - lag(deaths, default = 0)) %>% | |
mutate(roll = c(rep(0, 6), rollmean(totalnewdeaths, 7, align = "right"))) %>% | |
ungroup() %>% | |
mutate(id = "NY") | |
## sum deaths outside of NY | |
notny <- us %>% | |
filter(id != "USA, NY") %>% | |
group_by(id) %>% | |
mutate(newdeaths = deaths - lag(deaths, default = 0)) %>% | |
group_by(date) %>% | |
summarise(totalnewdeaths = sum(newdeaths), day = unique(day), weekend = unique(weekend)) %>% | |
mutate(roll = c(rep(0, 6), rollmean(totalnewdeaths, 7, align = "right"))) %>% | |
mutate(id = "USA - NY") | |
## combine into one data.frame for plotting | |
d <- bind_rows(all, notny, ny) | |
ggplot(d, aes(date, totalnewdeaths)) + | |
geom_col(aes(fill = weekend), alpha = 0.4) + | |
geom_line(aes(y = roll), lwd = 1) + | |
theme_light() + | |
facet_wrap(~id, nrow = 1) + | |
scale_x_date(limits = c(as.Date("2020-03-15"), Sys.Date()), | |
date_breaks = "2 weeks", | |
date_minor_breaks = "1 day") + | |
labs(x = "Date", | |
y = "Daily Deaths (linear)", | |
title = "USA with and without NY", | |
subtitle = "Solid line indicates 7-day rolling mean. Weekends: Sat/Sun/Mon", | |
caption = "Data source: {COVID19} (https://cran.r-project.org/package=COVID19)") + | |
easy_add_legend_title("Weekend?") | |
#### cases #### | |
## gather USA data at state level and identify weekends | |
us <- covid19("USA", level = 2) %>% | |
select(id, date, cases = confirmed) %>% | |
mutate(day = weekdays(date), | |
weekend = day %in% c("Saturday", "Sunday", "Monday")) | |
## sum deaths by date across all states | |
all <- us %>% | |
group_by(id) %>% | |
arrange(date) %>% | |
mutate(newcases = cases - lag(cases, default = 0)) %>% | |
group_by(date) %>% | |
summarise(totalnewcases = sum(newcases), day = unique(day), weekend = unique(weekend)) %>% | |
mutate(roll = c(rep(0, 6), rollmean(totalnewcases, 7, align = "right"))) %>% | |
mutate(id = "USA, total") | |
## sum cases in NY | |
ny <- us %>% | |
filter(id == "USA, NY") %>% | |
mutate(totalnewcases = cases - lag(cases, default = 0)) %>% | |
mutate(roll = c(rep(0, 6), rollmean(totalnewcases, 7, align = "right"))) %>% | |
ungroup() %>% | |
mutate(id = "NY") | |
## sum cases outside of NY | |
notny <- us %>% | |
filter(id != "USA, NY") %>% | |
group_by(id) %>% | |
mutate(newcases = cases - lag(cases, default = 0)) %>% | |
group_by(date) %>% | |
summarise(totalnewcases = sum(newcases), day = unique(day), weekend = unique(weekend)) %>% | |
mutate(roll = c(rep(0, 6), rollmean(totalnewcases, 7, align = "right"))) %>% | |
mutate(id = "USA - NY") | |
## combine into one data.frame for plotting | |
d <- bind_rows(all, notny, ny) | |
ggplot(d, aes(date, totalnewcases)) + | |
geom_col(aes(fill = weekend), alpha = 0.4) + | |
geom_line(aes(y = roll), lwd = 1) + | |
theme_light() + | |
facet_wrap(~id, nrow = 1) + | |
scale_x_date(limits = c(as.Date("2020-03-15"), Sys.Date()), | |
date_breaks = "2 weeks", | |
date_minor_breaks = "1 day") + | |
labs(x = "Date", | |
y = "Daily cases (linear)", | |
title = "USA with and without NY", | |
subtitle = "Solid line indicates 7-day rolling mean. Weekends: Sat/Sun/Mon", | |
caption = "Data source: {COVID19} (https://cran.r-project.org/package=COVID19)") + | |
easy_add_legend_title("Weekend?") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment