Created
January 4, 2022 22:11
-
-
Save jonspring/7ad0f7fa8db830246f74ad0e6d5391e7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(httr); library(jsonlite) | |
library(tidyverse); library(lubridate) | |
library(gganimate); library(ggtext) | |
sf_hosp <- GET("https://data.sfgov.org/resource/nxjg-bhem.json?$limit=999999") | |
sf_hosp$content %>% | |
rawToChar %>% | |
fromJSON %>% | |
mutate(patientcount = as.numeric(patientcount), | |
reportdate = as.Date(reportdate)) %>% | |
filter(covidstatus == "COVID+") %>% | |
arrange(reportdate) %>% | |
mutate(dphcategory = forcats::fct_rev(dphcategory)) %>% | |
group_by(dphcategory) %>% | |
mutate(avg = slider::slide_index_dbl(patientcount, reportdate, | |
mean, .before = lubridate::days(6))) -> sf_hosp_clean | |
sf_hosp_clean %>% | |
select(-patientcount, -data_loaded_at) %>% | |
tidyr::pivot_wider(names_from = dphcategory, values_from = avg) %>% | |
mutate(ratio = ICU / `Med/Surg`) %>% | |
mutate(era = case_when( | |
reportdate <= ymd(20200621) ~ "First wave<br><i style='color:#BBBBBB'>Mar-Jun '20</i>", | |
reportdate <= ymd(20201021) ~ "Summer 2020<br><i style='color:#BBBBBB'>Jul-Oct '20", | |
reportdate <= ymd(20210401) ~ "Winter '20-21<br><i style='color:#BBBBBB'>Nov'20-Mar'21", | |
reportdate <= ymd(20210702) ~ "pre-Delta<br><i style='color:#BBBBBB'>Apr-Jul '21", | |
reportdate <= ymd(20211129) ~ "Delta<br><i style='color:#BBBBBB'>Aug-Nov '21", | |
TRUE ~ "Omicron<br><i style='color:#BBBBBB'>Dec '21 - ??" | |
) %>% forcats::fct_reorder(reportdate)) %>% | |
group_by(era) %>% | |
mutate(era_stage = row_number()) %>% | |
ungroup() -> sf_hosp_clean_anim | |
animate( | |
sf_hosp_clean_anim %>% | |
ggplot(aes( `Med/Surg`, ICU)) + | |
geom_path(data = . %>% select(-era, -era_stage), color = "gray80", alpha = 1) + | |
geom_path(aes(color = era), size = 1.2, show.legend = FALSE) + | |
geom_point(aes(color = era), size = 2, show.legend = FALSE) + | |
facet_wrap(~era) + | |
expand_limits(y = 0) + | |
scale_x_continuous(minor_breaks = NULL) + | |
scale_y_continuous(minor_breaks = NULL) + | |
labs(y = "# in Intensive Care", | |
x = "# in Acute Care", | |
title = "San Francisco COVID+ going to ICUs less than in prior waves", | |
caption = "Data through 2021-12-31\nSource: SFDPH, via https://sf.gov/data/covid-19-hospitalizations") + | |
theme_minimal(base_size = 24) + | |
ggthemes::scale_color_tableau(direction = -1) + | |
theme(strip.text = element_markdown(), | |
panel.grid = element_line(size = 0.5), | |
plot.title = element_text(size = 23), | |
plot.caption = element_text(color = "gray70", size = 16)) + | |
transition_reveal(era_stage) + | |
shadow_wake(wake_length = 1, alpha = 0.8, exclude_layer = 2, wrap = FALSE), | |
fps = 20, duration = 8, width = 800, height = 600, device = "ragg_png", | |
start_pause = 20, end_pause = 20, renderer = gifski_renderer(file = "SF_beds_20211231.gif")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment