Created
April 8, 2020 01:52
-
-
Save jshannon75/e91a7c5c5043e1fde73fb928c6639e73 to your computer and use it in GitHub Desktop.
Creating a county level map of county growth rate/doubling time using R & tmap
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(tidyverse) | |
library(ggrepel) | |
library(gghighlight) | |
library(tidycensus) | |
library(sf) | |
dailydata<-read_csv("https://github.com/nytimes/covid-19-data/raw/master/us-counties.csv") | |
#This next step requires a census API | |
cty_pop<-get_acs(geography="county",variables = "B01001_001",geometry=TRUE) %>% | |
rename(fips=GEOID) | |
dailydata_ga<-dailydata %>% | |
filter(state=="Georgia" & | |
date>as.Date("2020-03-16")& | |
county!="Unknown") | |
dailydata_ga_lag<-dailydata_ga %>% | |
group_by(county) %>% | |
arrange(desc(date)) %>% | |
mutate(value_3d=lag(x=cases,n=3,default=0,order_by=date), | |
change_3d=cases-value_3d, | |
double_time=if_else(value_3d==0,0,(70/(change_3d/value_3d*100)*3))) %>% | |
mutate(date_c=as.character(date)) | |
cty_pop_ga<-cty_pop %>% | |
filter(substr(fips,1,2)=="13") | |
double_date<-function(date_sel){ | |
cty_select<-dailydata_ga_lag %>% | |
filter(date_c==date_sel & cases>9) | |
map<-tm_shape(cty_pop_ga)+ | |
tm_polygons(col="#e0e0e0",border.col = "#95989c")+ | |
tm_shape(cty_select)+ | |
tm_polygons("double_time", | |
breaks=c(0,1,2,4,8,16,50), | |
palette=c('#016c59','#1c9099','#67a9cf','#a6bddb','#d0d1e6','#f6eff7'), | |
border.col = "#95989c", | |
title="Doubling time in days \n(darker=faster growth)")+ | |
tm_layout(legend.outside=TRUE, | |
attr.outside=TRUE, | |
main.title = paste("COVID cases-growth rate in Georgia: ",date_sel,sep=""), | |
main.title.size=1, | |
title.position =c("left","bottom"))+ | |
tm_credits(text="Counties with > 10 cases shown, Data: NY Times, Map by @jerry_shannon") | |
#map | |
tmap_save(map,paste("ga_maps/",date_sel,".png",sep="")) | |
} | |
dates<-unique(dailydata_ga_lag$date_c) | |
maps<-map(dates,double_date) | |
mapfiles<-paste("ga_maps/",list.files(path="ga_maps"),sep="") | |
gifski(mapfiles,"ga_doubling_2020_04_07.gif",width=800,height=500) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment