Options to create animated plots in R

I just researched ways to make animated .gif files in R. In particular, I wanted to animate a map over a number of years to visualize the change of a variable over these years. There are a lot of different options out there and I am trying to summarize their different functionalities here. Many people have already provided great in depth examples for these options, and I am linking to those examples instead of providing my own here.

This site was very helpful to get an overview of ImageMagick, gganimate, and tweenr. It also provides great examples including code snippets. Additionally, I found animationanimation_tmap, and plotly to be good options.

ImageMagick

Simplest approach by combining several .png files that were made in whatever way before.

system("convert -delay 40 *.png animatedgraph.gif")
file.remove(list.files(pattern=".png"))

ImageMagick is a free software that can do a lot of stuff, not just in R.  Installing ImageMagick is a little bit of a pain on a Mac. It requires to first install MacPorts and Xcode Command Line Tools. Here is a step-by-step guide.

animation

Can create animations in several different formats (GIF, HTML, LaTeX, SWF, and mp4). animation is an R package with a lot of functionality. Most of the animation types require the installation of additional R packages or other software (such as ImageMagick). This post provides an example of the saveGIF() function. Another example is here.

gganimate

Allows to set an “animation variable” via frame.

library(gganimate)
plot <- ggplot(data, aes(x = xvar, y = yvar, frame = animationvar)) +
geom_point()
gganimate(plot, "animatedgraph.gif")

gganimate is an R package. gganimate wraps the animation package to create animated ggplot2 plots. gganimate requires ImageMagick to be installed on your system. You apparently need to run RStudio as an administrator to not get an error message. A good in-depth gganimate example is this post.

tweenr

Allows to visualize smooth transitions between different states. Pretty neat! tweenr is an R package and works well with gganimate. Here is a good comparison of tweenr and gganmiate.

animation_tmap()

animation_tmap() creates animations from tmap and other series of R plots.

animation_tmap(tm, filename = "animation.gif", width = NA, height = NA,
delay = 40)

animation_tmap() is a function of the R package tmap. animation_tmap() requires ImageMagick to be installed on your system.

plotly

Plotly offers a number of amazing data visualization tools to create interactive and animated plots. It comes as an R package, but is also compatible with a number of other languages/tools (Python, MATLAB, Perl, Julia, and Arduino) and it comes with a quite useful cheatsheet.

ggplotly()  allows to make a ggplot2 graph interactive or animated.

There is a Datacamp tutorial for plotly. It is not amazing, but nice to get some quick hands-on experience with plotly. Here is a great example for using the plotly functions ggplotly()  and plot_geo(). The former apparently tends to create large files, that might not be ideal for displaying on websites. A great tutorial/book on plotly can be found here.

My conclusion

Plotly would have been amazing and also (kind of) worked, but the final result (using ggplotly()) had a filesize of about 100MB and that was just too big. I really liked the interactive navigation bar as well as the hover information. However, running the actual animation produced some weird behavior when it switched between the years (like modern art kind of weird). I am not sure why. I tried to figure out a way to use plot_geo() or plot_ly() instead of ggplotly(), but I couldn’t find a way to make a choropleth map that does not consist of the US or the countries of the world. If there is a solution to this, it has to do with setting the locationmode correctly.

tmap is meant for spatial objects and I don’t know if there is a way to have a spatial object with a time variable. I preferred having a data.frame with year as a variable that I could use as the frame variable. That didn’t seem possible with tmap, so I ended up using gganimate (I used fortify() and left_join() to get the data in the right format, kind of like the example on pages 18-19 here.).


Update 1/29/2018:

I figured out how to solve the weird “modern art behavior” of ggplotly(). The problem was that the smooth transition doesn’t make sense when polygons are plotted and they just change color, but not position. I needed to add transition = 0 in the animation options. The file size is still huge though.

ggplotly(g, tooltip ="text") %>%
animation_opts(1000, transition = 0)


Please excuse the probably numerous typos and grammatical errors in this text. This is mostly written for my own reference.

One thought on “Options to create animated plots in R

Leave a reply to Roger Bohn Cancel reply