Code: https://github.com/theosanderson/adhoc_covid/tree/main/sgtf_analysis
library(tidyverse)
## -- Attaching packages --------------------------------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2 v purrr 0.3.4
## v tibble 3.0.3 v dplyr 1.0.2
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## -- Conflicts ------------------------------------------------------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(gganimate)
library(zoo)
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
geo <- read_sf("./Local_Authority_Districts__April_2019__UK_BUC_v2-shp/Local_Authority_Districts__April_2019__UK_BUC_v2.shp")
data <- read_csv("./phe_ltla_sgtf.csv")
## Parsed with column specification:
## cols(
## Region = col_character(),
## LTLA_code = col_character(),
## LTLA_name = col_character(),
## week = col_character(),
## `n_Other cases` = col_double(),
## n_Total = col_double(),
## `n_SGTF cases` = col_double(),
## `percent_Other cases` = col_double(),
## `percent_SGTF cases` = col_double()
## )
data$week = lubridate::dmy(data$week)
region_lookup = data %>% select(LTLA_name,Region) %>% distinct
blanks = expand_grid(week =unique(data$week),LTLA_name = unique(data$LTLA_name) ) %>% inner_join(region_lookup)
## Joining, by = "LTLA_name"
data <- full_join(data,blanks) %>% arrange(week)
## Joining, by = c("Region", "LTLA_name", "week")
problematic_ltlas = c("East Cambridgeshire","Boston")
data<-data %>% group_by(Region,week) %>% mutate(`percent_SGTF cases` = ifelse(LTLA_name %in% problematic_ltlas,mean(`percent_SGTF cases`,na.rm=TRUE),`percent_SGTF cases`))
data<-data %>% group_by(LTLA_name) %>% mutate(`percent_SGTF cases` = na.approx(`percent_SGTF cases`,na.rm=FALSE))
data<-data %>% group_by(LTLA_name)%>% arrange(week) %>% mutate(meanval = rollapply(`percent_SGTF cases`, 3, mean,na.rm=TRUE, partial = TRUE) ,`percent_SGTF cases` = ifelse(n_Total <20 | is.na(`percent_SGTF cases`),meanval,`percent_SGTF cases`))
both<- inner_join(geo,data,by=c("LAD19NM"="LTLA_name"))
a<-ggplot(both ,aes(fill=`percent_SGTF cases`/100,group=LAD19NM))+geom_sf()+scale_fill_viridis_c(option='magma',direction=-1,limits=c(0,1))+theme_minimal()+
theme(plot.title = element_text(size = 25,colour="black", vjust = 0))+labs(title="{frame_time}",fill="SGTF") +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank()) +
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())+transition_time(week)+ theme(legend.position="right")+coord_sf(expand=FALSE)
animate(a,nframes=100,height=700,width=700,fps=10,end_pause = 20,type="cairo")