1

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:

enter image description here

I'm struggling a bit with two things:

  1. Getting country flags and tile labels placed correctly. Here's the example I'm trying to follow.
  2. 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")
Eric Green
  • 7,385
  • 11
  • 56
  • 102
  • [This post is somewhat related](https://stackoverflow.com/a/53163549/7941188). I think this is a perfect question for gganimate wizard @Jonspring :) – tjebo May 01 '20 at 08:59
  • and check out this thread https://community.rstudio.com/t/trying-to-create-animated-bar-plot-with-sliding-bars-that-overtake-each-other/46528/13 – tjebo May 01 '20 at 09:03
  • (would you maybe do us the favour and reduce the code to the minimum please? it makes it harder to read and focus on the problem with some of the truly irrelevant bits (theme and lab calls). Instead of providing the entire code of your data manipulation, you could also just create some fake data which reproduces the problem. cheers. – tjebo May 01 '20 at 09:12
  • 1
    thanks for the links @Tjebo. i moved the data prep to a gist. – Eric Green May 01 '20 at 11:36
  • removed downvote. If you remove all the plot bits from "making the plot nice" downwards, the question will be much better readable too without losing it's validity – tjebo May 01 '20 at 12:45

0 Answers0