Skip to content

Instantly share code, notes, and snippets.

@seabbs
Last active January 25, 2021 14:53
Show Gist options
  • Save seabbs/ae918dfd111149716fa71970bdb3dd2b to your computer and use it in GitHub Desktop.
Save seabbs/ae918dfd111149716fa71970bdb3dd2b to your computer and use it in GitHub Desktop.
R code using {covid19.nhs.data} to generate a gif of weekly Covid-19 hospital admissions by lower-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("ltla")
shapefile <- england_ltla_shape
# complete and filter from September
adm <- adm %>%
filter(date >= "2020-09-01") %>%
drop_na(date, geo_code, admissions) %>%
mutate(date = ceiling_date(date, unit = "week", week_start = 1)) %>%
group_by(geo_code, date) %>%
summarise(admissions = sum(admissions), .groups = "drop") %>%
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('Week ending: {closest_state}') +
transition_states(date)
animate(map, fps = 3, renderer = gifski_renderer())
# Save gif ----------------------------------------------------------------
anim_save("ltla_admissions.gif")
@seabbs
Copy link
Author

seabbs commented Jan 25, 2021

ltla_admissions

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