Hans Rosling: very compelling animation in 2006 TED talk
But are animations useful to help people understand things?
People like animations
But animation didn’t help everyone understand better
Harder to answer questions about the data vs small multiples
Confusing: “the dots flew everywhere.”
Different types of animation work differently (duh)
User-controlled versus not
Transitions between different variables
Layering more information as animation
Small multiples as animation
Questions to ask yourself:
Should you include animation?
How should that animation appear?
Should users be able to control?
Yeah, yeah, but your scientists were so preoccupied with whether or not they could that they didn’t stop to think if they should. - Dr. Ian Malcolm, Jurassic Park
plotly.js is a javascript library
https://plotly-r.com/ - section 14 in particular
https://blog.methodsconsultants.com/posts/introduction-to-interactive-graphics-in-r-with-plotly/
Two main functions: ggplotly() and plot_ly()
Additional aesthetic (goes in the ggplot aes() function) for a “frame” variable
Each value of the frame variable is 1 frame in the animation
Numeric variable: numerical order (probably fine)
Character variable: alphabetical order (maybe fine)
Factor variable: factor order (maybe fine)
Default addition of a play/pause button and slider on graph
Only works for HTML output
data(gapminder)
plot1 <- ggplot(data = gapminder,
aes(x = gdpPercap, y = lifeExp,
color = continent, frame = year)) +
geom_point(aes(size = pop, frame = year,
ids = country)) +
scale_x_log10()
## Warning: Ignoring unknown aesthetics: frame, ids
ggplotly(plot1)
There are a variety of options, but they require you to move away from ggplot and into the plot_ly function, which has a somewhat different format
plot2 <- gapminder %>%
plot_ly(x = ~gdpPercap, y = ~lifeExp,
size = ~pop,
text = ~country, hoverinfo = "text") %>%
layout(xaxis = list(type = "log")) %>%
add_markers(color = ~continent,
frame = ~year, ids = ~country)
With animation_opts
, animation_button
, and
animation_slider
, you can change all these options
Speed of animation
Type of transition (smooth, jumpy, instant)
Where the “play” button shows up
A label indicating which frame is being viewed
Some examples in the lab
Set of additional functions to add to any ggplot
Also requires the gifski and png packages to create animations
Does not include play buttons – just loops the same animation over and over by default
VERY slow to knit the document on my computer – not sure exactly why
https://cran.r-project.org/web/packages/gganimate/vignettes/gganimate.html
transition_time
and transition_states
to
animate changes across continuous and categorical frame variables,
respectively
transition_states
also allow you to modify the time for
each frame and the time to switch, as well as to label the frames as
they change
ease_aes
to change transition style
More examples in the lab
Can easily add limited animation to an existing ggplot
Using the plotly function to plot isn’t too hard
Slick button and slider by default
Advanced features require using plotly’s plotting
Use ggplot, just add a few extra functions
Many options for animation and transition
No control over animations – never-ending loop
Very slow to knit the file? (might just be me?)
https://resources.rstudio.com/rstudio-conf-2019/gganimate-live-cookbook
https://d4tagirl.com/2017/05/how-to-plot-animated-maps-with-gganimate
Membership around the world
Circles represent size of the group in each location
Appear when the group was founded
https://towardsdatascience.com/animating-regression-models-in-r-using-broom-and-ggplot2-da798e6638be
Show change over time unfolding for 4 different groups
Uses gganimate and broom to extract regression coefficients, observed values, and predicted values
Uses the transition_reveal
option to make the line
appear gradually
Roll a simulated die 6000 times
Show the running total graphically
Vertical dashed line is expected value (1/6 per value)
https://datascienceplus.com/how-to-build-animated-bar-plots-using-r/
Alternative way to show variability around an estimate
Strongly related to concepts like simulation, bootstrapping, as well as Bayesian interpretations