Animation

Animation

https://medium.com/@FisherDanyel/effectiveness-of-animation-in-trend-visualization-ten-years-later-e2f52b433526

Hans Rosling: very compelling animation in 2006 TED talk

But are animations useful to help people understand things?

  • People like animations

    • Called “fun”, “exciting”, and even “emotionally touching.”
  • But animation didn’t help everyone understand better

    • Harder to answer questions about the data vs small multiples

    • Confusing: “the dots flew everywhere.”

Animation

https://medium.com/@urban_institute/4-observations-on-animating-your-data-visualizations-cf987b069c35

Different types of animation work differently (duh)

  • User-controlled versus not

  • Transitions between different variables

  • Layering more information as animation

  • Small multiples as animation

Animation

Questions to ask yourself:

  • Should you include animation?

  • How should that animation appear?

  • Should users be able to control?

    • step-wise or pause or scroll

 

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

plotly

plotly.js is a javascript library

  • plotly package in R implements the 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()

plotly::ggplotly() and ggplot()

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

plotly::ggplotly() and ggplot()

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)

plotly::plot_ly()

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)

plotly::plot_ly() options

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

gganimate

gganimate

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

gganimate

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

Summary: plotly versus gganimate

  • plotly pros:

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

  • plotly cons:

Advanced features require using plotly’s plotting

  • gganimate pros:

Use ggplot, just add a few extra functions

Many options for animation and transition

  • gganimate cons:

No control over animations – never-ending loop

Very slow to knit the file? (might just be me?)

Applications of animation

Advanced features of gganimate

  • Short talk with more advanced examples of gganimate

https://resources.rstudio.com/rstudio-conf-2019/gganimate-live-cookbook

  • tweenr to make smoother transitions

https://blog.revolutionanalytics.com/2017/05/tweenr.html

Animated map of Rladies membership

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

Animating regression

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

Animating die rolls

https://www.reddit.com/r/dataisbeautiful/comments/au25nq/simulating_6000_die_rolls_visualization_created/

  • Roll a simulated die 6000 times

  • Show the running total graphically

  • Vertical dashed line is expected value (1/6 per value)

Hypothetical outcomes

https://resources.rstudio.com/rstudio-conf-2019/visualizing-uncertainty-with-hypothetical-outcomes-plots

https://medium.com/hci-design-at-uw/hypothetical-outcomes-plots-experiencing-the-uncertain-b9ea60d7c740

  • Alternative way to show variability around an estimate

  • Strongly related to concepts like simulation, bootstrapping, as well as Bayesian interpretations