22

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.

Flo
  • 1,503
  • 1
  • 18
  • 35
  • Have you tried using `image_write() `? – Miha Jul 20 '18 at 10:35
  • There is an `animate` function. – Axeman Jul 20 '18 at 10:36
  • @Miha. Not before, but when I tried I noticed that `p` is not a magick image object and I am not sure how to convert it into one. @Axeman, I did, but could not figure out how to specify sth along the lines of `file="p.gif"` – Flo Jul 20 '18 at 10:44
  • I assume you don't have the package: `install.packages("magick")`. – Miha Jul 20 '18 at 10:46
  • 1
    @Miha, I do, hence the error from the package: `Error: The 'image' argument is not a magick image object.`. Perhaps I am doing something wrong though? How would you export p to a gif with `magick`? – Flo Jul 20 '18 at 10:48
  • Try the `?animate` function ("This function takes a gganim object and renders it into an animation."). – Stéphane Laurent Jul 20 '18 at 10:59
  • @StéphaneLaurent `animate(p)` indeed renders an animation, but how can I export the animation as a file (e.g., `animation.gif`)? Note that I am only assigning the animation to `p` because I thought I can then export `p` somehow. When not assigning the animation to `p` it will be rendered automatically. – Flo Jul 20 '18 at 11:19
  • 1
    `image_graph` and `dev.off()` should work. Or another option: using `image_animate` with `image_write`. So try `save.gif <- image_animate(p, fps = 2); image_write(save.gif, "output.gif")` – Miha Jul 20 '18 at 11:39
  • Ah, thanks! I did not realize that `animate` and `image_write` should be part of one pipeline. – Flo Jul 20 '18 at 12:56

2 Answers2

18

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

EJJ
  • 1,474
  • 10
  • 17
10

You can do like this:

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

enter image description here

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • 24
    In GitHub, it's mentioned that `gganimate` doesn't use ImageMagick see [here](https://github.com/thomasp85/gganimate/issues/93#issuecomment-415798696). The appropriate method would be to use `anim_save()` such as `anim_save("filenamehere.gif", anim)` – EJJ Nov 21 '18 at 20:16
  • 1
    Using this method it reports: Error: The animation object does not specify a save_animation method. – Vojtěch Kania Nov 20 '19 at 09:43
  • @VojtěchKania The method in my answer or the one in EJJ's comment ? – Stéphane Laurent Nov 20 '19 at 09:49
  • Your method reports: Error: The 'image' argument is not a magick image object. – Vojtěch Kania Nov 20 '19 at 09:51
  • @VojtěchKania Ok. Something has changed. Anyway, EJJ's method looks better. It should be the answer. – Stéphane Laurent Nov 20 '19 at 09:56