Cheatography
https://cheatography.com
A cheatsheet on the gganimate library.
Core Conceptsgganimate 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
● transition_*() : What variable controls
change and how?
● view_*() : Should the axes change with the
data?
● enter/exit_*() : How does new data get
added the plot? How does old data leave?
● shadow_*() : Should previous data be
“remembered” 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 transition_*() or
view_*() to make an animation. The other
function groups enable you to add features or alter
gganimate’s default settings .
Starting Plotslibrary(tidyverse)
library(gganimate)
a <- ggplot(diamonds, aes(carat, price)) + geom_point()
b <- ggplot(txhousing, aes(month, sales)) + geom_col()
c <- ggplot(economics, aes(date, psavert)) + geom_line()
|
enter/exit_*()Note: enter/exit_*() functions can be combined so that you can have old data fade away and
shrink to nothing by adding exit_fade() and exit_shrink() to the plot
| | transition_*()Other transitions
● transition_manual() : Similar to transition_states(), but without intermediate states.
● transition_layers() : Add layers (geoms) one at time.
● transition_components() : Transition elements independently from each other.
● transition_events() : Each element’s duration can be controlled individually.
Baseline Animationanim_a <- a + transition_states(color, transition_length = 3, state_length = 1)
|
Saving animationsanimation_to_save <- anim_a + exit_shrink()
anim_save("first_saved_animation.gif", animation = animation_to_save)
Since the animation argument uses your last rendered animation by default, this also works:
anim_a + exit_shrink()`
anim_save("second_saved_animation.gif")
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_renderer() or ffmeg_renderer()) or
spritesheets (sprite_renderer()):
# requires you to have the av package installed
anim_save("third_saved_animation.mp4",renderer = av_renderer())
|
| | view_*()view_zoom()
view_zoom() works similarly to
view_step() , except it changes the view by
zooming and panning.
Note: both view_step() and view_zoom() have
view_*_manual() versions for setting views
directly instead of inferring it from frame data
view_*()view_zoom()
view_zoom() works similarly to
view_step() , except it changes the view by
zooming and panning.
Note: both view_step() and view_zoom() have
view_*_manual() versions for setting views
directly instead of inferring it from frame data
Label variablesgganimate’s transition_*() functions create label variables you can pass to (sub)titles and other labels
with the glue package. For example, transition_states() has next_state, which is the name of the
state the animation is transitioning towards. Label variables are different between transitions, and details
are included in the documentation of each.
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets