Fixing abrupt changes/transitions in animated ggplot

I was wanting to make an animated ggplot of daily values for countries over the course of a year. I was able to do that thanks to some helpful questions/answers here.

While the code below successfully makes a gif, the changes/transitions when countries move up or down/overtake one another is very abrupt (i.e instantaneous). It is not smooth like the examples in the linked post.

I'm not entirely sure why this is happening and would really appreciate some help. Thanks in advance to anyone who is able to provide some insight. Code is below.

enter image description here

library(tidyverse)
library(ggplot2)
library(gganimate)
library(gifski)
library(png)

# Generate some fake data 
n=365  
df <- data.frame(country_name = rep(c("Country_1","Country_2","Country_3","Country_4","Country_5"), n, replace=FALSE),
                 date = rep(seq.Date(as.Date("2021-01-01"), as.Date("2021-12-31"), "day"),each=5), # each is number of countries 
                 incidents=sample(1:100, size = 25, replace=TRUE
                 ))

# Make cumulative number of events
df = df %>% 
  group_by(country_name) %>%
  arrange(date) %>%
  mutate(cumulative_incidents = cumsum(incidents)) %>%
  ungroup()

# create integer rankings (I thought the *1 would make things smoother)
df = df %>% 
  group_by(date) %>%
  mutate(rank = min_rank(-cumulative_incidents *1), 
         Value_rel = cumulative_incidents/cumulative_incidents[rank==1],
         Value_lbl = paste0(" ",round(cumulative_incidents/1e9))) %>%
  ungroup()
 
# make the static plot
my_plot = ggplot(df, aes(-rank,Value_rel, fill = country_name)) +
  geom_col(width = 0.8, position="identity") +
  coord_flip() + 
  geom_text(aes(-rank,y=0,label = country_name,hjust=0)) +       
  geom_text(aes(-rank,y=Value_rel,label = cumulative_incidents, hjust=0)) + 
  theme_minimal() +
  theme(legend.position = "none",axis.title = element_blank()) +
  # animate along Year
  transition_states(date,4,1)

# animate the plot
animate(my_plot, 100, fps = 25, duration = 20, width = 800, height = 600)

Solution 1:

The temporal resolution of your data is very high, you have 365 timepoints, but only 500 frames in your animation. These smooth switches therefore happen within 500 / 365 = ~1.4 frames and aren't visible.

Either make your animation much longer, reduce the time resolution of the data, or manually code in transitions. For example, here's what happens if we give data only once a month:

df2 <- filter(df, date %in% seq(min(date), max(date), 'week'))
my_plot <- ggplot(df2, aes(-rank,Value_rel, fill = country_name)) +
  geom_col(width = 0.8, position="identity") +
  coord_flip() + 
  geom_text(aes(-rank,y=0,label = country_name,hjust=0)) +       
  geom_text(aes(-rank,y=Value_rel,label = cumulative_incidents, hjust=0)) + 
  theme_minimal() +
  theme(legend.position = "none",axis.title = element_blank()) +
  # animate along Year
  transition_states(date, transition_length = 10, state_length = 1)
animate(my_plot, fps = 25, duration = 20, width = 800, height = 600)

enter image description here