Show Menu
Cheatography

gganimate Cheat Sheet by

A cheatsheet on the gganimate library.

Core Concepts

gganimate builds on ggplot2’s grammar of
graphics to provide functions for animation. You
add them to plots created with ggplot() the
same way you add a geom

Main Function Groups
transi­tio­n_*()
: What variable controls
change and how?
view_*()
: Should the axes change with the
data?
enter/­exi­t_*()
: How does new data get
added the plot? How does old data leave?
shadow_*()
: Should previous data be
“remem­bered” and shown with current data?
ease_aes()
: How do you want to handle the
pace of change between transition values?
Note: you only need a
transi­tio­n_*()
or
view_*()
to make an animation. The other
function groups enable you to add features or alter
gganim­ate’s default settings .

Starting Plots

librar­y(t­idy­verse)

librar­y(g­gan­imate)

a <- ggplot­(di­amonds, aes(carat, price)) + geom_p­oint()

b <- ggplot­(tx­hou­sing, aes(month, sales)) + geom_col()

c <- ggplot­(ec­ono­mics, aes(date, psavert)) + geom_l­ine()

enter/­exi­t_*()

Note:
enter/­exi­t_*()
functions can be combined so that you can have old data fade away and
shrink to nothing by adding
exit_f­ade()
and
exit_s­hrink()
to the plot

shadow_*()

 

transi­tio­n_*()

Other transi­tions

transi­tio­n_m­anual()
: Similar to transi­tio­n_s­tat­es(), but without interm­ediate states.
transi­tio­n_l­ayers()
: Add layers (geoms) one at time.
transi­tio­n_c­omp­one­nts()
: Transition elements indepe­ndently from each other.
transi­tio­n_e­vents()
: Each element’s duration can be controlled indivi­dually.

Baseline Animation

anim_a <- a + transition_states(color, transition_length = 3, state_length = 1)

Saving animations

animat­ion­_to­_save <- anim_a + exit_s­hrink()

anim_s­ave­("fi­rst­_sa­ved­_an­ima­tio­n.g­if", animation = animat­ion­_to­_save)

Since the animation argument uses your last rendered animation by default, this also works:

anim_a + exit_s­hri­nk()`
anim_s­ave­("se­con­d_s­ave­d_a­nim­ati­on.g­if­")

anim_s­ave()
uses gifski to render the animation as a .gif file by default. You can use the renderer
argument for other output types including video files (av_re­nde­rer() or ffmeg_­ren­der­er()) or
sprite­sheets (sprit­e_r­end­ere­r()):
# requires you to have the av package installed
anim_s­ave­("th­ird­_sa­ved­_an­ima­tio­n.m­p4",­ren­derer = av_ren­der­er())
 

view_*()

view_z­oom()

view_z­oom()
works similarly to
view_s­tep()
, except it changes the view by
zooming and panning.

Note: both
view_s­tep()
and
view_z­oom()
have
view_*­_ma­nual()
versions for setting views
directly instead of inferring it from frame data

view_*()

view_z­oom()

view_z­oom()
works similarly to
view_s­tep()
, except it changes the view by
zooming and panning.

Note: both
view_s­tep()
and
view_z­oom()
have
view_*­_ma­nual()
versions for setting views
directly instead of inferring it from frame data

Label variables

gganim­ate’s transi­tio­n_*() functions create label variables you can pass to (sub)t­itles and other labels
with the glue package. For example, transi­tio­n_s­tates() has next_s­tate, which is the name of the
state the animation is transi­tioning towards. Label variables are different between transi­tions, and details
are included in the docume­ntation of each.
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Data Visualization in R through ggplot2 Cheat Sheet