Skip to content

Instantly share code, notes, and snippets.

@mikemahoney218
Created November 16, 2020 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikemahoney218/1b129d4425f3fd58232f94b925005d20 to your computer and use it in GitHub Desktop.
Save mikemahoney218/1b129d4425f3fd58232f94b925005d20 to your computer and use it in GitHub Desktop.
Code to create a map of MA COVID-19 cases-per-capita per day for the 30 Day Map Challenge
library(sf)
library(dplyr)
library(tidyr)
library(lubridate)
library(readr)
library(ggplot2)
library(tidycensus)
cases <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_US.csv") %>%
pivot_longer(cols = matches("^\\d.*"), names_to = "Date", values_to = "cumulative") %>%
mutate(Date = as_date(Date, format = "%m/%d/%y", tz = "America/New_York")) %>%
group_by(Province_State, Admin2) %>%
filter(max(cumulative) > 0) %>%
arrange(Province_State, Admin2, Date) %>%
mutate(daily = cumulative - lag(cumulative, 1L, 0))
county_2018 <- get_acs("county", "B01003_001", geometry = TRUE, state = "MA")
if (!dir.exists("25covid")) dir.create("25covid")
map_data <- cases %>%
filter(Province_State == "Massachusetts") %>%
group_by(Date, FIPS) %>%
summarise(daily = sum(daily)) %>%
ungroup() %>%
mutate(FIPS = as.character(FIPS)) %>%
inner_join(county_2018, by = c("FIPS" = "GEOID")) %>%
mutate(value = daily / estimate,
value = ifelse(value < 0, 0, value))
date_seq <- seq(as_date('2020-03-01'), max(map_data$Date), 1)
map_range <- range(map_data$value)
for (i in seq_len(length(date_seq))) {
map_data %>%
filter(Date == date_seq[[i]]) %>%
ggplot() +
geom_sf(aes(geometry = geometry, fill = value), color = "#ffffcc") +
scale_fill_distiller(palette = "YlOrRd",
direction = 1,
limits = map_range) +
coord_sf() +
labs(caption = date_seq[[i]]) +
theme_void() %+replace%
theme(legend.position = "none",
panel.background = element_rect(fill = "#202020"),
plot.caption = element_text(hjust = 0.05),
plot.caption.position = "plot") +
ggsave(paste0("25covid/", date_seq[[i]], ".png"), width = 11, height = 5)
}
# And an imagemagick command to make the movie...
paste0("convert -delay 50 -limit memory 3000MB ",
paste0("25covid/",
list.files("25covid"),
collapse = " "),
" 25covid/map.mp4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment