Created
May 26, 2020 16:53
-
-
Save jshannon75/7facd45db0dc5263ec94773ccfbf1061 to your computer and use it in GitHub Desktop.
Making a gif of county level mobility data using the covdata package
This file contains 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(covdata) | |
library(tmap) | |
library(TTR) | |
ga_mobility<-apple_mobility %>% | |
filter(sub_region=="Georgia" & geo_type=="county" & | |
is.na(index)==FALSE) %>% | |
group_by(region) %>% | |
mutate(ra = runMean(index, 5)) %>% ###Create a five day moving average | |
filter(is.na(ra)==FALSE) | |
#Download county boundaries | |
ga_county<-tigris::counties(state="GA",class="sf") %>% | |
rename(region=NAMELSAD) | |
#Create a function that joins county data to boundaries and creates a map for a given date, saving as a png | |
county_map<-function(date_sel){ | |
ga_county1<-ga_county %>% | |
left_join(ga_mobility) %>% | |
filter(date==date_sel) | |
title<-paste("Apple mobility data for ",date_sel,sep="") | |
map<-tm_shape(ga_county) + | |
tm_polygons(col="#f2f2f2")+ | |
tm_shape(ga_county1)+ | |
tm_polygons("ra",breaks=c(0,60,80,100,125,150,200), | |
title="Index value (5-day avg)") + | |
tm_credits("Data: Apple mobility (via covdata package) | Map by @jerry_shannon", | |
position=c("left","bottom")) + | |
tm_layout(legend.outside=TRUE, | |
attr.outside=TRUE, | |
main.title=title) | |
filetitle<-paste("maps/map_",date_sel,".png",sep="") | |
tmap_save(map,filetitle) | |
} | |
#Select all dates from Feb 15 onward and apply the function | |
date_df<-ga_mobility %>% filter(date>"2020-02-15") | |
map(unique(date_df$date),county_map) | |
#Create a gif from al the png files using the gifski package | |
files<-paste("maps/",list.files(path="maps"),sep="") | |
gifski::gifski(files,gif_file="mobility_maps.gif", | |
width=600,height=700,delay=0.6,loop=TRUE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment