Skip to content

Instantly share code, notes, and snippets.

@gergness
Created September 23, 2020 01:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gergness/2453e446922fc588a17d49a2fa30c2b3 to your computer and use it in GitHub Desktop.
Save gergness/2453e446922fc588a17d49a2fa30c2b3 to your computer and use it in GitHub Desktop.
suppressPackageStartupMessages({
    library(covdata)
    library(dplyr)
    library(ggplot2)
    library(geofacet)
    library(glue)
})
pop_state <- uspop %>%
    filter(sex_id == "totsex" & hisp_id == "tothisp") %>%
    group_by(region_name, state_abbreviation = state_abbr) %>%
    summarize(pop = sum(pop), .groups = "drop")


deaths_state_week <- nchs_wdc %>% 
    group_by(state_abbreviation, year, week, week_ending_date) %>% 
    summarize(deaths = sum(number_of_deaths), .groups = "drop") %>%
    inner_join(pop_state, by = "state_abbreviation") %>%
    mutate(
        deaths_per_100k = deaths / pop * 100000
    )

max_2020data_week <- deaths_state_week %>%
    filter(year == 2020) %>%
    group_by(state_abbreviation) %>%
    filter(week == max(week)) %>%
    ungroup() %>%
    filter(week %in% range(week)) %>%
    arrange(week) %>% 
    pull(week_ending_date) %>%
    unique() %>%
    paste(collapse = " - ")
    
ggplot(
    deaths_state_week, 
    aes(x = week, y = deaths_per_100k, color = year == 2020, alpha = year == 2020, group = year)
) + geom_line() + 
    scale_color_manual(values = c("black", "red"), guide = NULL) +
    scale_alpha_manual(values = c(0.4, 1), guide = NULL) +
    scale_x_continuous(name = "", breaks = NULL, ) + 
    scale_y_continuous(name = "") + 
    facet_geo(~state_abbreviation) + 
    labs(
        title = "Deaths per 100k residents by state: 2020 compared to 2015-2019",
        subtitle = glue("2020 data through {max_2020data_week} (and are provisional so may be updated)"),
        caption = glue(
            "Data from National Center for Health Statistics via Kieran Healy's covdata package\n",
            "https://kjhealy.github.io/covdata/"
        )
    )

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