Search code examples
rgganimate

End Animation after Appearing


I made an animation from my data and managed to give it a good seize/resolution for a powerpoint presentation.

I would like the bars in the barchart to appear and then end the animation. I can't find the proper settings in gganimate to make the animation only run in one direction (appear-only).

    a <- data.frame(group = c("sehr schlecht",
                          "schlecht",
                          "weder noch",
                          "gut",
                          "sehr gut"),
                values = c(0, 0, 0, 0, 0),
                frame = rep("a", 5))

b <- data.frame(group = c("sehr schlecht",
                          "schlecht",
                          "weder noch",
                          "gut",
                          "sehr gut"),
                values = c(20,
                          40,
                          20,
                          5,
                          15),
                frame = rep("b", 5))

graphdata <- rbind(a,b)  

p <- ggplot(graphdata, aes(x=group, y=values, fill=group)) + 
  geom_bar(stat='identity') +
  ylim(0, 100) +
  theme_bw() +
  theme(legend.title=element_blank()) +
  scale_x_discrete(name ="Anteil (%)", 
                   limits=c("sehr schlecht",
                            "schlecht",
                            "weder noch",
                            "gut",
                            "sehr gut")) +
  # gganimate specific bits:
  transition_states(
    frame,
    transition_length = 2,
    state_length = 1
  ) +
  ease_aes('sine-in-out')

options(gganimate.dev_args = list(width = 800, height = 600, res = 150))
                           
animate(p + enter_appear(),
  renderer = av_renderer()
)

Solution

  • Okay, found it myself! Ending an animation after it ran can be toggled with the transitions states (wrap = FALSE):

      # gganimate specific bits:
      transition_states(
        frame,
        transition_length = 2,
        state_length = 1,
        wrap = FALSE
      ) +
      ease_aes('sine-in-out')