Skip to content

Instantly share code, notes, and snippets.

@gshotwell
Created July 25, 2020 20:21
Show Gist options
  • Save gshotwell/bd56f8965923d1011982bfdcc80d26c9 to your computer and use it in GitHub Desktop.
Save gshotwell/bd56f8965923d1011982bfdcc80d26c9 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(janitor)
library(RcppRoll)
deaths <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv")
deaths <- deaths %>%
rename(province_state = `Province/State`,
country_region = `Country/Region`) %>%
pivot_longer(-c("province_state", "country_region", "Lat", "Long"),
names_to = "date", values_to = "deaths") %>%
mutate(date = lubridate::mdy(date)) %>%
janitor::clean_names()
deaths <- deaths %>%
mutate(
lat_group = case_when(
lat > 25 ~ "North",
lat < 25 & lat > -25 ~ "Equator",
TRUE ~ "South"
),
month = lubridate::month(date),
quarter = lubridate::quarter(date),
is_summer = dplyr::case_when(
month %in% c(10, 11, 12, 1:4) ~ FALSE,
TRUE ~ TRUE
),
is_summer = dplyr::case_when(
lat_group == "South" ~ !is_summer,
lat_group == "Equator" ~ TRUE,
lat_group == "North" ~ is_summer
),
season = case_when(
!is_summer ~ "not_summer",
is_summer ~ "summer"
)
)
deaths <- deaths %>%
filter(lat_group %in% c("North", "South")) %>%
group_by(country_region, date,) %>%
summarize(deaths = sum(deaths),
season = season[1],
is_summer = is_summer[1],
lat_group = lat_group[1]) %>%
group_by(country_region) %>%
mutate(daily_deaths = deaths ) %>%
mutate(daily_deaths = deaths - lag(deaths)) %>%
arrange(date) %>%
mutate(rolling_deaths = roll_sum(daily_deaths, 7, align = "right", fill = NA))
deaths <- deaths %>%
mutate(mean_summer_deaths = mean(rolling_deaths[!is_summer], na.rm = TRUE)) %>%
mutate(deaths_norm = rolling_deaths / mean_summer_deaths) %>%
filter(mean_summer_deaths > 1)
deaths %>%
mutate(season = fct_rev(season)) %>%
ggplot(aes(x = date, y = rolling_deaths, color = season, group = country_region)) +
geom_point(alpha = 0.7, size = .5) +
facet_wrap(~lat_group, nrow = 2, scales = "free") +
theme_light() +
labs(title = "Covid Deaths",
y = "Seven day rolling average (free scales)",
x = "Date") +
scale_color_brewer(palette = "Set1") +
scale_color_manual(values = c("summer" = "maroon", "not_summer" = "dark blue"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment