Using gganimate to export gif

Flo picture Flo · Jul 20, 2018 · Viewed 13.8k times · Source

The package gganimate creates gifs (MWE code from here):

    library(ggplot2)
    #devtools::install_github('thomasp85/gganimate')
    library(gganimate)

    p <- ggplot(mtcars, aes(factor(cyl), mpg)) + 
            geom_boxplot() + 
            # Here comes the gganimate code
            transition_states(
                    gear,
                    transition_length = 2,
                    state_length = 1
            ) +
            enter_fade() + 
            exit_shrink() +
            ease_aes('sine-in-out')

How can export this gif now? In the previous (now archived) version of gganimate this was simple:

    gganimate(p, "output.gif")

However, I could not find an equivalent function in the current gganimate package.


Note: This question seems like an exact duplicated of the question from which I took the code for the MWE. However, gganimate has been updated and in the new version, displaying an animation in the viewer pane vs. exporting it seem to be different issues.

Answer

St&#233;phane Laurent picture Stéphane Laurent · Jul 20, 2018

You can do like this:

anim <- animate(p)
magick::image_write(anim, path="myanimation.gif")

enter image description here