thomasp85/gganimate

Elements mixed between first and last frame

pengchenglai opened this issue · 0 comments

I want to create a GIF that plots the countries in Africa one by one. However, I noticed that the appearance of the border for the first country always appears in the last frame instead of the first. This issue becomes more noticeable when adding an end_pause.

Here is the example:

# load packages 
pack <- c('gganimate','rnaturalearth','sf','tidyverse')
lapply(pack,require,character.only=T)

# create the gif for African content
africa <- ne_countries(continent = "africa",returnclass ="sf")
# get 10 countries as an example
country10 <- st_as_sf(head(africa, 10))
# set the factor level for the apperance order of the countries
country10$order <- factor(country10$sovereignt,levels=country10$sovereignt)

#  merge geometry for africa
africa$un <- rep.int(1,length(africa$featurecla))
sf_use_s2(FALSE)
africa <- africa %>%
  group_by(un) %>%
  summarise(geometry=st_union(geometry))


a <- ggplot(data = africa) + 
  geom_sf() + 
  coord_sf()+
  theme(axis.text.y = element_blank(),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_blank(),
        panel.background = element_rect(fill = "lightgrey", colour = NA),
        plot.background = element_rect(fill = "lightgrey",colour = NA),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())+
  geom_sf(data = country10, col = "green3", fill = "transparent") +
  transition_states(order) + shadow_mark()

# output Africa gif
animate(a,fps=10, height = 4,
        width = 3, units = "cm", res = 600,end_pause = 30)
anim_save("africa.gif")

Thanks!

enter image description here