I'm attempting to show my students how to replicate this animated COIVD-19 chart in {gganimate}
. I've found this tutorial post to be quite helpful.
Only showing a snippet here because I could not figure out how to keep the upload size small:
I'm struggling a bit with two things:
- Getting country flags and tile labels placed correctly. Here's the example I'm trying to follow.
- General look and feel (transitions are not as smooth, counter text is not positioned in body of chart, counter text styling is not as nice)
Here is my code:
# load packages
library(tidyverse)
library(gganimate) # install.packages(c("av", "gifski"))
library(viridis)
library(ggflags) # devtools::install_github("ellisp/ggflags")
library(countrycode) # install.packages("countrycode")
# get and prep the data
library("rio")
url <- "https://gist.githubusercontent.com/ericpgreen/123599c21fae4a2a653ae3b7795236d0/raw/0584ff1769adc9c1fb8062012699bdccffe1d170/animate.R"
df <- rio::import(url)
static <-
leaderboard %>%
# convert to factor
mutate(monthDay = factor(monthDay,
levels = monthDayLevels,
labels = monthDayLevels)) %>%
# plot
ggplot(., aes(x = rank, group = country,
fill = country, color = country
#,country = cc) # needed if embedding flags
)) +
geom_tile(aes(y = cases/2, height = cases,
width = 0.9), alpha = 0.8, color = NA) +
# if trying to embed flags
#geom_flag(aes(y = cases+50), size = 20) +
geom_text(aes(y = 0, label = paste(country, " ")),
vjust = 0.2, hjust = 1, size = 10) +
geom_text(aes(y=cases+50, label = label, hjust=0),
size = 10) +
coord_flip(clip = "off", expand = TRUE) +
scale_x_reverse()
# define animation
animate <-
static +
transition_states(monthDay,
transition_length = 4,
state_length = 1) +
ease_aes('cubic-in-out') +
view_follow(fixed_x = TRUE)
# render
animate(animate, fps = 10, duration = 50, width = 1000,
height = 600,
renderer=gifski_renderer("gganim.gif"))
Additional plotting code to reproduce my gif. Add this static
plot to reproduce my gif:
scale_fill_viridis_d(option = "plasma") +
scale_color_viridis_d(option = "plasma") +
theme_minimal() +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.grid.major.x = element_line(size=.4,
color="grey" ),
panel.grid.minor.x = element_line(size=.1,
color="grey" ),
plot.title.position = "plot",
plot.title=element_text(size=20,
face="bold",
colour="#313632"),
plot.subtitle=element_text(size=50,
color="#a3a5a8"),
plot.caption =element_text(size=15,
color="#313632"),
plot.background=element_blank(),
plot.margin = margin(1, 9, 1, 9, "cm")) +
labs(title = 'Cumulative number confirmed COVID-19 cases by date and country',
subtitle = '{closest_state}',
caption = "Data: Johns Hopkins CCSE")