Skip to content

Instantly share code, notes, and snippets.

@gadenbuie
Last active April 10, 2020 16:18
Show Gist options
  • Save gadenbuie/1c9d26003004b698cdce7f98c766c027 to your computer and use it in GitHub Desktop.
Save gadenbuie/1c9d26003004b698cdce7f98c766c027 to your computer and use it in GitHub Desktop.
library(tidyverse)
fl_zip <- read_csv("https://raw.githubusercontent.com/gadenbuie/covid19-florida/master/data/covid-19-florida_arcgis_cases-by-zip.csv")

vec_proxy.sfc <- function(x) {
  class(x) <- setdiff(class(x), "sfc")
  vctrs::vec_proxy(x)
}

fl_counties <-
  USAboundaries::us_counties(states = "FL") %>% 
  as_tibble() %>% 
  mutate(
    name = toupper(name),
    centroid = map(geometry, ~unclass(sf::st_centroid(.))),
    c_long = map_dbl(centroid, `[`, 1),
    c_lat = map_dbl(centroid, `[`, 2)
  ) %>% 
  select(name, c_long, c_lat)

g <- 
  fl_zip %>%
  mutate_at("cases_1", as.numeric) %>% 
  filter(!is.na(cases_1), cases_1 >= 10, total_population >= 100) %>% 
  mutate(cases_per_cap = cases_1 / total_population) %>% 
  select(zip, name, countyname, total_population, starts_with("percent"), contains("income"), starts_with("cases_")) %>% 
  mutate(countyname = recode(countyname, DADE = "MIAMI-DADE")) %>% 
  pivot_longer(
    cols = c(total_population, starts_with("percent"), contains("income")),
    names_to = "stat",
    values_to = "value"
  ) %>% 
  left_join(fl_counties, by = c(countyname = "name")) %>% 
  mutate_at(vars(c_lat, c_long), round, 1) %>% 
  arrange(c_lat, c_long) %>% 
  mutate(
    countyname = forcats::fct_inorder(countyname) %>% fct_rev,
    stat = snakecase::to_title_case(stat),
    stat = recode(stat, "Percent Gen z" = "Percent Gen Z", "Percent Gen x" = "Percent Gen X")
  ) %>% 
  ggplot() +
  aes(cases_per_cap * 1e5, value, color = countyname) +
  geom_point() +
  facet_wrap(~ stat, scales = "free", nrow = 3) +
  grkmisc::theme_grk(panel_background_color = NULL) +
  # scale_x_continuous(trans = scales::log10_trans()) +
  # scale_x_continuous(labels = scales::percent_format()) +
  labs(y = NULL, x = "Confirmed Cases per 100K", color = "County") +
  ggtitle("Cumulative Confirmed COVID-19 Cases", "In Florida Zip Codes with more than 10 cases") +
  theme(
    panel.spacing.y = unit(1, "line"),
    legend.position = c(0.975, -0.05),
    legend.justification = c(1, 0),
    legend.text = element_text(size = 6, margin = margin()),
    legend.title = element_text(margin = margin(b = 0.5, unit = "line")),
    legend.spacing.y = unit(1, "pt"),
    legend.key.width = unit(10, "pt"),
    legend.key.height = unit(10, "pt")
  )
g

if (interactive()) {
  ggsave("~/Downloads/fl-covid-by-zip-per-cap.png", g, width = 6.4, height = 3.6, scale = 2.5)
}

Created on 2020-04-10 by the reprex package (v0.3.0)

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