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
tra­nsi­tio­n_*­(): What variable controls
change and how?
vie­w_*­(): Should the axes change with the
data?
ent­er/­exi­t_*­(): How does new data get
added the plot? How does old data leave?
sha­dow­_*(­): Should previous data be
“remem­bered” and shown with current data?
eas­e_a­es(­): How do you want to handle the
pace of change between transition values?
Note: you only need a trans­iti­on_*() or
view_*() to make an animation. The other
function groups enable you to add features or alter
gganim­ate’s default settings .

Starting Plots

libra­ry(­tid­yve­rse)
libra­ry(­gga­nim­ate)
a <- ggplot­(di­amonds, aes(carat, price)) + geom_p­oint()
b <- ggplot­(tx­hou­sing, aes(month, sales)) + geom_c­ol()
c <- ggplot­(ec­ono­mics, aes(date, psavert)) + geom_l­ine()

enter/­exi­t_*()

Note: enter­/ex­it_*() functions can be combined so that you can have old data fade away and
shrink to nothing by adding exit_­fade() and exit_­shr­ink() to the plot

shadow_*()

 

transi­tio­n_*()

Other transi­tions

tra­nsi­tio­n_m­anu­al(­): Similar to transi­tio­n_s­tat­es(), but without interm­ediate states.
tra­nsi­tio­n_l­aye­rs(­): Add layers (geoms) one at time.
tra­nsi­tio­n_c­omp­one­nts­(): Transition elements indepe­ndently from each other.
tra­nsi­tio­n_e­ven­ts(­): 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

anima­tio­n_t­o_save <- anim_a + exit_s­hri­nk()
anim_­sav­e("f­irs­t_s­ave­d_a­nim­ati­on.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_­sav­e("s­eco­nd_­sav­ed_­ani­mat­ion.gi­f")
anim_­save() 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_­sav­e("t­hir­d_s­ave­d_a­nim­ati­on.m­p4­"­,re­nderer = av_ren­der­er())
 

view_*()

vie­w_z­oom­()
view_­zoom() works similarly to
view_­ste­p(), except it changes the view by
zooming and panning.

Note: both view_­step() and view_­zoom() have
view_­*_m­anu­al() versions for setting views
directly instead of inferring it from frame data

view_*()

vie­w_z­oom­()
view_­zoom() works similarly to
view_­ste­p(), except it changes the view by
zooming and panning.

Note: both view_­step() and view_­zoom() have
view_­*_m­anu­al() 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.

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

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