Recap: Animation

Intermediate Interactive Data Visualization with plotly in R

Adam Loy

Statistician, Carleton College

Keyframe animation

Filmstrip of scatterplots.

world_indicators %>%
  plot_ly(x = ~income, y = ~co2) %>%
  add_markers(frame = ~year, ids = ~country, showlegend = FALSE)
Intermediate Interactive Data Visualization with plotly in R

Animated time series plot of Belgium's per capita income.

Intermediate Interactive Data Visualization with plotly in R

Cumulative animations

library(dplyr)
library(purrr)
belgium %>%
  split(.$year) %>%
  accumulate(~bind_rows(.x, .y)) %>%
  set_names(1960:2018) %>%
  bind_rows(.id = "frame")
Intermediate Interactive Data Visualization with plotly in R

Coping with staggered starting points

cran_logs_animation.gif

Intermediate Interactive Data Visualization with plotly in R

CRAN download data

glimpse(monthly_logs)
Rows: 171
Columns: 4
$ package   <chr> "ggvis", "ggvis", "ggvis", "ggvis", "ggvis", "ggvis",…
$ date      <date> 2014-06-30, 2014-07-31, 2014-08-31, 2014-09-30, 2014…
$ dec_date  <dbl> 2014.49, 2014.58, 2014.66, 2014.75, 2014.83, 2014.91,…
$ downloads <dbl> 1344, 2120, 2035, 1702, 3590, 2899, 2427, 2227, 2708,…
Intermediate Interactive Data Visualization with plotly in R

What if we ignore the baseline issue?

monthly_logs %>%
  split(f = .$dec_date) %>%
  accumulate(., ~bind_rows(.x, .y)) %>%
  bind_rows(.id = "frame") %>%
  plot_ly(x = ~dec_date, y = ~downloads) %>%
  add_lines(color = ~package, frame = ~frame, ids = ~package)
Warning message:
In p$x$data[firstFrame] <- p$x$frames[[1]]$data :
  number of items to replace is not a multiple of replacement length
Intermediate Interactive Data Visualization with plotly in R

Completing the data set

library(tidyr)
complete_logs <- monthly_logs %>% complete(package, dec_date, fill = list(downloads = 0))

arrange(complete_logs, dec_date)
## A tibble: 228 x 4
  package     dec_date date       downloads
  <chr>          <dbl> <date>         <dbl>
1 ggvis          2014. 2014-06-30      1344
2 highcharter    2014. NA                 0
3 plotly         2014. NA                 0
4 rbokeh         2014. NA                 0
5 ggvis          2015. 2014-07-31      2120
# … with 223 more rows
Intermediate Interactive Data Visualization with plotly in R

Animating the completed data

complete_logs %>%
  split(f = .$dec_date) %>%
  accumulate(., ~bind_rows(.x, .y)) %>%
  bind_rows(.id = "frame") %>%
  plot_ly(x = ~dec_date, y = ~downloads) %>%
  add_lines(color = ~package, frame = ~frame, ids = ~package)

cran_logs_animation_small.gif

Intermediate Interactive Data Visualization with plotly in R

Let's practice!

Intermediate Interactive Data Visualization with plotly in R

Preparing Video For Download...