Skip to content

Instantly share code, notes, and snippets.

@MattSandy
Last active April 8, 2020 16:45
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 MattSandy/1c67d141e6b5a67b8a30be6994688c1f to your computer and use it in GitHub Desktop.
Save MattSandy/1c67d141e6b5a67b8a30be6994688c1f to your computer and use it in GitHub Desktop.
NYT Covid-19 Data Merged with Census
library(tidycensus)
library(tidyverse)
library(viridisLite)
library(gganimate)
library(patchwork)
us_county_population <- get_estimates(geography = "county",
product = "population",
shift_geo = TRUE, geometry = TRUE,
year = 2018) %>%
rename(fips = GEOID) %>% filter(variable == "POP")
counties <- read_csv("https://github.com/nytimes/covid-19-data/blob/master/us-counties.csv?raw=true")
fips_merge <- counties %>% left_join(us_county_population)
fips_merge$percapita <- fips_merge$cases*1000000 / (fips_merge$value)
offset <- 8
percapita_cap <- fips_merge %>% filter(date == Sys.Date() - offset) %>% .$percapita %>% max(na.rm = T)
fips_merge$percapita[which(fips_merge$percapita > percapita_cap)] <- percapita_cap
# plot --------------------------------------------------------------------
yesterday <- ggplot(fips_merge %>% filter(date == Sys.Date() - 1)) +
geom_sf(aes(fill = NA, geometry = geometry),
fill = "#000000",
color = alpha("#FFFFFF",0.1),
data = us_county_population) +
geom_sf(aes(fill = percapita, geometry = geometry), color = alpha("#FFFFFF",0.01)) +
coord_sf(datum = NA) +
theme_minimal() +
theme(legend.key.width = unit(1,"in"),
plot.background = element_rect(color = "#111111",
fill = "#111111"),
panel.background = element_rect(fill = "#111111", color = NA),
legend.background = element_rect(color = NA, fill = "#111111")) +
labs(title = " ",
subtitle = paste0("Date: ", Sys.Date() - 1),
caption = "@appupio") +
scale_fill_viridis_c()
last_week <- ggplot(fips_merge %>% filter(date == Sys.Date() - offset)) +
geom_sf(aes(fill = NA, geometry = geometry),
fill = "#000000",
color = alpha("#FFFFFF",0.1),
data = us_county_population) +
geom_sf(aes(fill = percapita, geometry = geometry), color = alpha("#FFFFFF",0.01)) +
coord_sf(datum = NA) +
theme_minimal() +
theme(legend.key.width = unit(1,"in"),
plot.background = element_rect(color = "#111111",
fill = "#111111"),
panel.background = element_rect(fill = "#111111", color = NA),
legend.background = element_rect(color = NA, fill = "#111111")) +
labs(title = "Covid-19 Cases Per Million. Data from //github.com/nytimes/covid-19-data/",
subtitle = paste0("Date: ", Sys.Date() - offset),
caption = " ") +
scale_fill_viridis_c()
plots <- last_week + yesterday
plots
ggsave(filename = "map.png",width = 24,height = 12, units = "in",dpi = 150)
system("convert map.png -trim -bordercolor white reframed.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment