Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gganimate to export gif

Tags:

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.

like image 291
Flo Avatar asked Jul 20 '18 10:07

Flo


People also ask

How do I export an animated GIF?

Choose File > Export > Export image or File > Export > Export Animated GIF. Click a tab at the top of the Export image or Export Animated GIF dialog box to select a display option: Optimized or 2‑Up. (Optional) Select the slices you want to optimize and the file format you want to use.

What is another way you can save your Gganimate plot other than as a GIF input the file extension?

In order to save the animation to a specific location, you can use the anim_save() function which, like ggplot2::ggsave , defaults to taking the last rendered animation and writes it to a file.


2 Answers

gganimate 1.0.6 and gifski 0.8.6

Based on the suggestion by @Ronak Shah, I've added an updated answer using anim_save() from the gganimate package - as it uses gifski now to render the .gif output.

library(ggplot2)
library(gganimate)
# install.package("gifski") #if not already installed

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

anim_save("filenamehere.gif", p)

enter image description here

like image 100
EJJ Avatar answered Sep 20 '22 21:09

EJJ


You can do like this:

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

enter image description here

like image 22
Stéphane Laurent Avatar answered Sep 21 '22 21:09

Stéphane Laurent