Recreating the Datasaurus Dozen Using tweenr and ggplot2

If you haven’t seen it yet, there’s a great example of why it’s always important to visualize your data making its way around the Twitter-verse.

Despite looking very different, all of these datasets have the same summary statistics to two decimal places. You can download the datasets, get details about the project, and read the whole paper by Justin Matejka and George Fitzmaurice here. In this post, I’ll show how we can recreate the GIF from the above tweet using tweenr and gganimate.

Creating the plots

The first step is to read in the data. The data has three variables: the dataset name, x, and y. I’ll define dataset as a factor so that the datasets will appear in the correct order in the animation.

library(tidyverse)
library(forcats)

datasaurus <- read_table2("datafiles/DatasaurusDozen.tsv",
  col_names = TRUE, col_types = "cnn") %>%
  mutate(dataset = as_factor(dataset))
datasaurus
#> # A tibble: 1,846 x 3
#>    dataset     x     y
#>    <fct>   <dbl> <dbl>
#>  1 dino     55.4  97.2
#>  2 dino     51.5  96.0
#>  3 dino     46.2  94.5
#>  4 dino     42.8  91.4
#>  5 dino     40.8  88.3
#>  6 dino     38.7  84.9
#>  7 dino     35.6  79.9
#>  8 dino     33.1  77.6
#>  9 dino     29.0  74.5
#> 10 dino     26.2  71.4
#> # ... with 1,836 more rows

We can view all of the datasets at once using facet_wrap in ggplot2.

ggplot(datasaurus, aes(x = x, y = y)) +
  facet_wrap(~ dataset, nrow = 3) +
  geom_point()

Hard to believe all of these datasets have the same summary statistics!

Animating the plots

For a first pass at animating these datasets, I’ll use the gganimate package. This works just like ggplot code above, just with an added frame aesthetic and no facet_wrap.

library(gganimate)

p <- ggplot(datasaurus, aes(x = x, y = y)) +
  geom_point(aes(frame = dataset))

animation::ani.options(interval = 1)
gganimate(p, title_frame = FALSE)

This is close, but not quite what I was looking for. This does indeed animate all of the datasets, but in order to duplicate the GIF above, I really want to see the points moving into their new positions for each dataset. To get this effect, I’ll use the tweenr package. tweenr takes in a list of dataframes, and then interpolates the transitions between the states.

First, I’ll create a list of the datasets.

n_datasaurus <- datasaurus %>%
  group_by(dataset) %>%
  nest() %>%
  add_row(dataset = "dino", data = list(.$data[[1]]))
n_datasaurus
#> # A tibble: 14 x 2
#>    dataset    data              
#>    <fct>      <list>            
#>  1 dino       <tibble [142 × 2]>
#>  2 away       <tibble [142 × 2]>
#>  3 h_lines    <tibble [142 × 2]>
#>  4 v_lines    <tibble [142 × 2]>
#>  5 x_shape    <tibble [142 × 2]>
#>  6 star       <tibble [142 × 2]>
#>  7 high_lines <tibble [142 × 2]>
#>  8 dots       <tibble [142 × 2]>
#>  9 circle     <tibble [142 × 2]>
#> 10 bullseye   <tibble [142 × 2]>
#> 11 slant_up   <tibble [142 × 2]>
#> 12 slant_down <tibble [142 × 2]>
#> 13 wide_lines <tibble [142 × 2]>
#> 14 dino       <tibble [142 × 2]>

I’ve also added the dino dataset again at the bottom so that the GIF with start and end with that dataset, making it seamless. I’ll then use tween_states, sending it the list of dataframes, and specifying the length of each state and transitions (I had to play around a bit with the numbers until I was happy with the final animation).

tween_datasaurus <- tween_states(n_datasaurus$data, tweenlength = 1,
  statelength = 0.5, ease = "sine-out", nframe = 200) %>%
  as.tibble()
tween_datasaurus
#> # A tibble: 28,400 x 3
#>          x       y .frame
#>      <dbl>   <dbl>  <int>
#>  1 55.3846 97.1795      1
#>  2 51.5385 96.0256      1
#>  3 46.1538 94.4872      1
#>  4 42.8205 91.4103      1
#>  5 40.7692 88.3333      1
#>  6 38.7179 84.8718      1
#>  7 35.6410 79.8718      1
#>  8 33.0769 77.5641      1
#>  9 28.9744 74.4872      1
#> 10 26.1538 71.4103      1
#> # ... with 28,390 more rows

This creates a new dataframe with the added .frame variable. I can then use the same gganimate code from above, just specifying .frame as the frame aesthetic instead of dataset.

p <- ggplot(tween_datasaurus, aes(x = x, y = y)) +
  geom_point(aes(frame = .frame))

animation::ani.options(interval = 1 / 15)
gganimate(p, title_frame = FALSE)

And there you have it! Now we can see all of the points moving between each dataset!

Session info

devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.5.0 (2018-04-23)
#>  os       macOS High Sierra 10.13.4   
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2018-05-04                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version     date       source                           
#>  animation   * 2.5         2017-03-30 CRAN (R 3.5.0)                   
#>  assertthat    0.2.0       2017-04-11 CRAN (R 3.5.0)                   
#>  backports     1.1.2       2017-12-13 CRAN (R 3.5.0)                   
#>  bindr         0.1.1       2018-03-13 CRAN (R 3.5.0)                   
#>  bindrcpp    * 0.2.2       2018-03-29 CRAN (R 3.5.0)                   
#>  blogdown      0.6         2018-04-18 CRAN (R 3.5.0)                   
#>  bookdown      0.7         2018-02-18 CRAN (R 3.5.0)                   
#>  broom         0.4.4       2018-03-29 CRAN (R 3.5.0)                   
#>  callr         2.0.3       2018-04-11 CRAN (R 3.5.0)                   
#>  cellranger    1.1.0       2016-07-27 CRAN (R 3.5.0)                   
#>  cli           1.0.0       2017-11-05 CRAN (R 3.5.0)                   
#>  clisymbols    1.2.0       2017-05-21 CRAN (R 3.5.0)                   
#>  colorspace    1.3-2       2016-12-14 CRAN (R 3.5.0)                   
#>  crayon        1.3.4       2017-09-16 CRAN (R 3.5.0)                   
#>  debugme       1.1.0       2017-10-22 CRAN (R 3.5.0)                   
#>  desc          1.2.0       2018-05-01 CRAN (R 3.5.0)                   
#>  devtools      1.13.5.9000 2018-05-03 Github (r-lib/devtools@13ee56b)  
#>  digest        0.6.15      2018-01-28 CRAN (R 3.5.0)                   
#>  dplyr       * 0.7.4       2017-09-28 CRAN (R 3.5.0)                   
#>  evaluate      0.10.1      2017-06-24 CRAN (R 3.5.0)                   
#>  forcats     * 0.3.0       2018-02-19 CRAN (R 3.5.0)                   
#>  foreign       0.8-70      2017-11-28 CRAN (R 3.5.0)                   
#>  gganimate   * 0.1.0.9000  2017-06-06 Github (dgrtwo/gganimate@bf82002)
#>  ggplot2     * 2.2.1       2016-12-30 CRAN (R 3.5.0)                   
#>  glue          1.2.0       2017-10-29 CRAN (R 3.5.0)                   
#>  gtable        0.2.0       2016-02-26 CRAN (R 3.5.0)                   
#>  haven         1.1.1       2018-01-18 CRAN (R 3.5.0)                   
#>  hms           0.4.2       2018-03-10 CRAN (R 3.5.0)                   
#>  htmltools     0.3.6       2017-04-28 CRAN (R 3.5.0)                   
#>  httr          1.3.1       2017-08-20 CRAN (R 3.5.0)                   
#>  jsonlite      1.5         2017-06-01 CRAN (R 3.5.0)                   
#>  knitr       * 1.20        2018-02-20 CRAN (R 3.5.0)                   
#>  labeling      0.3         2014-08-23 CRAN (R 3.5.0)                   
#>  lattice       0.20-35     2017-03-25 CRAN (R 3.5.0)                   
#>  lazyeval      0.2.1       2017-10-29 CRAN (R 3.5.0)                   
#>  lubridate     1.7.4       2018-04-11 CRAN (R 3.5.0)                   
#>  magrittr      1.5         2014-11-22 CRAN (R 3.5.0)                   
#>  memoise       1.1.0       2017-04-21 CRAN (R 3.5.0)                   
#>  mnormt        1.5-5       2016-10-15 CRAN (R 3.5.0)                   
#>  modelr        0.1.1       2017-07-24 CRAN (R 3.5.0)                   
#>  munsell       0.4.3       2016-02-13 CRAN (R 3.5.0)                   
#>  nlme          3.1-137     2018-04-07 CRAN (R 3.5.0)                   
#>  pillar        1.2.2       2018-04-26 CRAN (R 3.5.0)                   
#>  pkgbuild      1.0.0       2018-05-03 Github (r-lib/pkgbuild@4ee12af)  
#>  pkgconfig     2.0.1       2017-03-21 CRAN (R 3.5.0)                   
#>  pkgload       1.0.0       2018-05-03 Github (r-lib/pkgload@35efedd)   
#>  plyr          1.8.4       2016-06-08 CRAN (R 3.5.0)                   
#>  psych         1.8.3.3     2018-03-30 CRAN (R 3.5.0)                   
#>  purrr       * 0.2.4       2017-10-18 CRAN (R 3.5.0)                   
#>  R6            2.2.2       2017-06-17 CRAN (R 3.5.0)                   
#>  Rcpp          0.12.16     2018-03-13 CRAN (R 3.5.0)                   
#>  readr       * 1.1.1       2017-05-16 CRAN (R 3.5.0)                   
#>  readxl        1.1.0       2018-04-20 CRAN (R 3.5.0)                   
#>  reshape2      1.4.3       2017-12-11 CRAN (R 3.5.0)                   
#>  rlang         0.2.0       2018-02-20 CRAN (R 3.5.0)                   
#>  rmarkdown     1.9         2018-03-01 CRAN (R 3.5.0)                   
#>  rprojroot     1.3-2       2018-01-03 CRAN (R 3.5.0)                   
#>  rstudioapi    0.7         2017-09-07 CRAN (R 3.5.0)                   
#>  rvest         0.3.2       2016-06-17 CRAN (R 3.5.0)                   
#>  scales        0.5.0       2017-08-24 CRAN (R 3.5.0)                   
#>  sessioninfo   1.0.0       2017-06-21 CRAN (R 3.5.0)                   
#>  stringi       1.1.7       2018-03-12 CRAN (R 3.5.0)                   
#>  stringr     * 1.3.0       2018-02-19 CRAN (R 3.5.0)                   
#>  testthat      2.0.0       2017-12-13 CRAN (R 3.5.0)                   
#>  tibble      * 1.4.2       2018-01-22 CRAN (R 3.5.0)                   
#>  tidyr       * 0.8.0       2018-01-29 CRAN (R 3.5.0)                   
#>  tidyselect    0.2.4       2018-02-26 CRAN (R 3.5.0)                   
#>  tidyverse   * 1.2.1       2017-11-14 CRAN (R 3.5.0)                   
#>  tweenr      * 0.1.5       2016-10-10 CRAN (R 3.5.0)                   
#>  usethis       1.3.0       2018-02-24 CRAN (R 3.5.0)                   
#>  utf8          1.1.3       2018-01-03 CRAN (R 3.5.0)                   
#>  withr         2.1.2       2018-03-15 CRAN (R 3.5.0)                   
#>  xfun          0.1         2018-01-22 CRAN (R 3.5.0)                   
#>  xml2          1.2.0       2018-01-24 CRAN (R 3.5.0)                   
#>  yaml          2.1.19      2018-05-01 CRAN (R 3.5.0)
comments powered by Disqus