Animations with gganimate

gganimate Logo

Today I’m going to show a brief walkthrough on using Thomas Lin Pedersen’s gganimate package, a great tool for greating GIFs of data for easy sharing. Check out his GIT repo for a full overview.

This example uses mock patient ART line data to reconstruct a live QRS complex. While some of this may sound unfamiliar, the image below is pretty familiar to anyone who has watched a hospital drama show:

gganimate Logo

ART lines (arterial line placements) are typically inserted into the radial artery of the wrist and are widely considered one of the most accurate ways of measuring blood pressure data. The ART line give a “Mean Arterial Pressure” or MAP value derived from the patient’s systolic and diastolic blood pressure readings:

$$MAP = \frac{Systolic BP}{3} + \frac{(2)(Diastolic BP)}{3}$$

In healthy adults, SBPs should range between 90 mmHg - 119 mmHg while DBPs are between 60 mmHg - 79 mm Hg, leading to desired MAPs between 70 mmHg - 100 mmHg. Tipping the MAP over or under this range can lead to “hypertension” and “hypotension” which come with different clinical manifestations.

For first time users of the gganimate package there may be some issues since it isn’t CRAN supported as of 12/31/2018. The following will be required to successfully run the package:

install.packages('devtools') # Package for GitHub connection
install.packages("gifski") # Selected for GIF rendering, others are available at ?renderers

df <- read.csv("ART_Testing.csv")

Without showing the actual raw data, conceptualize that the ART line data comes from a patient monitoring system in a formatted .csv file. Some light manipulation of the ART and time variables allow for much more realistic, and accurate, data for analysis.

To make the animation smooth, use of R’s approx command allows for interpolation between data points. Here an arbitrarily large value n=10000 has been selected.

dfART <- data.frame(approx(df$Time, df$AR1, n = 10000))

Finally, making use of the ggplot2 package applies graphic aesthetics and produces the animation. Some key things to note about incorporating the gganimate commands:

  • transition_time requires a time designation, in this case for us the x-variable in our dfART data frame.
  • shadow_wake give a nice trailing tail after the animation and some room for customization on what that looks like.
  • ease_aes allows for different easing function applications depending on what you’re showing. Here I’ve opted for ‘linear’, the default.
ggplot(dfART, aes(x = dfART$x, y = dfART$y, colour = "firebrick4")) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  theme(axis.line = element_line(size = 3, colour = "grey80")) +
  labs(title = 'Time: {frame_time}', x = 'Time in Seconds (Sample Rate: 240 Hz)', y = 'ART Value (mm Hg)') +
  theme(plot.title = element_text(hjust = 0.5, size = 18, color = "dodgerblue4")) +
  transition_time(dfART$x) +
  shadow_wake(wake_length = .2, size = TRUE, alpha = TRUE, colour = 'blue',
              fill = NULL, falloff = "cubic-in", wrap = TRUE,
              exclude_layer = NULL, exclude_phase = NULL) +

You should see a render status bar followed by a Frame completion percentage. Once the prompt reads “Finalizing encoding … done!” the animation will appear in the RStudio viewer. I recommend using the following command immediately after this prompt to output it to a shareable GIF:

anim_save("ART_Animation.gif", animation = last_animation())

Here is our final animation:

gganimate ART

I hope you found this helpful and enjoyable, GIFs are a great way to gain interest in your data and are fun to share. Thanks to Thomas Pedersen for making this package available, you can follow him through Twitter at @thomasp85.