The purpose of this topic is to explore the benefits of animation and to learn how to produce animated data visualisations in R.
Adding animation to a data visualisation can have several benefits:
Movement is an example of preattentive pop out that we saw in the Perception Topic. We immediately and effortlessly attend to elements of an image that are in motion.
Movement is also involved in one of the “Gestalt Rules” from the perception topic: common fate. Elements of an image that move together are perceived as part of the same group.
Animation provides an extra aesthetic (time) for us to map data values to.
This means that we can represent more variables at once to obtain a multivariate visualisation of the data.
Animation of changes in values can reveal features that are less apparent from a collection of side-by-side static images. There is some experimental evidence that animation improves the perception of change.
A manufactured (and quite extreme) example is shown below: it is not obvious from the series of static panels in the facetted ‘ggplot2’ that the green point follows a circular path or that the red point changes from side to side, but those “trends” are immediately apparent in the animated version (which is effectively just drawing the panels one after the other).
We will take two approaches to generating animations:
A low-level approach where we draw every frame of the animation ourselves.
This will make use of the ‘magick’ package to collect the frames and turn them into a single animated image. The basic idea is shown below:
image_animate() to create the animation.drawFrame <- function(i) {
grid.newpage()
grid.rect(gp=gpar(col=NA, fill="white"))
grid.circle(x[i], 0.5, r=unit(4, "mm"), gp=gpar(fill="black"))
}The advantage of this approach is that we will have complete control over every aspect of the animation. The downside is that we will have to write more code.
The Reading on the ‘magick’ package provides a little more information about animating R graphics with ‘magick’.
An approach where we describe the animation at a high-level and the ‘gganimate’ package generates the animation frames for us.
A simple (artificial) example is shown below:
transition_states()Not only have we had to type less, we get more features automatically, like the smooth transitions between the states and pausing at each state.
The Reading on the ‘gganimate’ package provides some more examples, plus a “grammar” of animation based on concepts like transitions, tweening, and easing.
We will make use of our accumulating knowledge of ‘ggplot2’ and ‘grid’ to draw the frames of our animations.
The Drawing and Graphics section of the “Getting started” introduction to the ‘magick’ package.
The Getting Started vignette for ‘gganimate’.
NOTE: If the animated images do not show in this document, you may have to run the code yourself to see the example animations.
The ‘magick’ package by Jeroen Ooms.
A brief note on an alternative way to use ‘magick’ to create animations.
The ‘gganimate’ package by Thomas Lin Pedersen.
Help pages for ‘grid’ functions for R version 4.1.2.
“Information
Visualisation” by Colin Ware.
The University of Auckland has the
3rd
Edition in electronic version plus older editions in hard
copy.
“Animated Transitions in Statistical Data Graphics” by Jeffrey Heer and George G. Robertson.
This
work is licensed under a
Creative
Commons Attribution 4.0 International License.