Skip to content

Instantly share code, notes, and snippets.

@seabbs
Last active January 25, 2021 14:52
Show Gist options
  • Save seabbs/1a2916e40ae7fac8f0eb7250dbfbef95 to your computer and use it in GitHub Desktop.
Save seabbs/1a2916e40ae7fac8f0eb7250dbfbef95 to your computer and use it in GitHub Desktop.
R code using {covid19.nhs.data} to generate a gif of Covid-19 hospital admissions byupper-tier local authority in England.
# Packages ----------------------------------------------------------------
library(covid19.nhs.data)
library(dplyr)
library(tidyr)
library(lubridate)
library(gganimate)
#devtools::install_github("thomasp85/transformr")
library(transformr)
library(gifski)
library(ggplot2)
# Get data ----------------------------------------------------------------
adm <- get_admissions("utla")
shapefile <- england_utla_shape
# complete and filter from September
adm <- adm %>%
filter(date >= "2020-10-01") %>%
drop_na(date, geo_code, admissions) %>%
complete(geo_code, date, fill = list(admissions = 0))
# Make map ----------------------------------------------------------------
# map latest
map_admissions(adm, shapefile) + theme(legend.position = "right")
# map all time (for gif)
map <- adm %>%
mutate(date = factor(date)) %>%
map_admissions(shapefile, date = NULL) +
theme(legend.position = "right")
# Turn into a gif ---------------------------------------------------------
map <- map +
ggtitle('Date: {closest_state}') +
transition_states(date)
animate(map, fps = 5, nframes = length(unique(adm$date)) * 2, renderer = gifski_renderer())
# Save gif ----------------------------------------------------------------
anim_save("utla_admissions.gif")
@seabbs
Copy link
Author

seabbs commented Jan 25, 2021

utla_admissions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment