<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/what-are-we-plotting-what-are-we-animating/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.data-imaginist.com/2018/what-are-we-plotting-what-are-we-animating/"> Data Imaginist</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img align="right" src="https://i0.wp.com/www.data-imaginist.com/assets/images/gganimate_logo_small.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>This is my first blog post about <code>gganimate</code> — a package I’ve been working on since mid-spring this year. I have many thoughts and lots to say about animation and <code>gganimate</code>, so much in fact that it has seemed too big a task to begin writing about. Further, I felt like I had to spend my time developing the thing in the first place.</p>
<p>So this is an alternative entrance into writing about <code>gganimate</code> — sort of a tech-note about a specific problem. There will still come a time for some more formal writing about the theory and use of <code>gganimate</code> but until then I’ll refer to my <a rel="nofollow" href="https://youtu.be/21ZWDrTukEs" target="_blank">useR keynote</a> for any words on my thoughts behind it all.</p>
<div>
<h2>The Problem</h2>
<p>When we animate data visualisations we often do it by calculating intermediary data points resulting in a smooth transition between the states represented by the raw data. In <code>gganimate</code> this is done by adding a <em>transition</em> which defines how data should be expanded across the animation frames. Underneath it all most transitions calculate intermediary data representations using <code>tweenr</code> and <code>transformr</code> — so far, so good.</p>
<p>What we have glanced over, and what is at the center of the problem, is what state of the data we decide to use as basis for our expansion. If you are not familiar with <code>ggplot2</code> and the grammar of graphics this might be a strange phrasing — data is data — but if you are, you’ll know that data can undergo several statistical transformations before it is encoded into a visual property and put on paper (or screen). Some of the states the data undergo are:</p>
<ol>
<li>Raw data as it is passed into the plotting function</li>
<li>Raw data with only the columns mapped to aesthetics present</li>
<li>Data transformed by a statistic</li>
<li>Data with aesthetics mapped to a scale</li>
<li>Data with default aesthetic values added</li>
<li>Data transformed by the geom</li>
</ol>
<p>If you prepare your data for animation beforehand (e.g. using <code>tweenr</code>), you’re only able to touch the data at the first state and thus limited in what you can do. If there is a one-to-one mapping between the raw data and the final visual encoding this might not be a problem, but it breaks down spectacularly when the statistic transformation impose a grouping of the data into a shared visual encoding, e.g. a box-plot. Consider the task of calculating intermediary data for a transition from one box-plot showing statistics for 10 points, to another box-plot showing statistics for 15 points. If you could only use the raw data your atomic observations would suddenly have to change from 10 to 15 values in a smooth manner. On the other hand, if you could calculate the statistics used to draw the two box-plots and then calculate intermediary statistics instead, this discrepancy in the underlying data would not pose any problem. Indeed, the latter approach is what is done in <code>gganimate</code> — all data expansion is performed after statistics have been calculated. In fact, all expansion is done when data has reached state 5. Why wait so long? A simple example to explain this is the case of colour (or fill) aesthetics. If they are mapped to a categorical variable there will be no way to create a smooth transition based on the raw data. On the other hand, if we wait until the raw data has been mapped to its final colour value, we may smoothly transition the colour itself, ignoring the fact that the intermediary colours does not correspond to any meaningful category in the raw data.</p>
</div>
<div>
<h2>The Curious Case of Tesselation</h2>
<p>So, “what is the problem?”, you may ask. Indeed, this approach is almost universally good, to the extend that you might just ignore the existence of other approaches… But the devils in the detail — let’s make a plot:</p>
<pre><code>library(ggplot2)
library(ggforce)

data &lt;- data.frame(
  x = runif(20),
  y = runif(20),
  state = rep(c('a', 'b'), 10)
)

ggplot(data, aes(x = x, y = y)) + 
  geom_voronoi_tile(fill = 'grey', colour = 'black', bound = c(0, 1, 0, 1)) + 
  geom_point() + 
  facet_wrap(~state)</code></pre>
<p><img src="https://i2.wp.com/www.data-imaginist.com/post/2018-09-22-what-are-we-plotting-what-are-we-animating_files/figure-html/unnamed-chunk-2-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>Now, think about what you would expect a transition between the two panels to look like – my guess is that it is nothing like below:</p>
<pre><code>library(gganimate)
ggplot(data, aes(x = x, y = y)) + 
  geom_voronoi_tile(fill = 'grey', colour = 'black', bound = c(0, 1, 0, 1)) + 
  geom_point() + 
  transition_states(state, transition_length = 3, state_length = 1) + 
  ease_aes('cubic-in-out')</code></pre>
<p><img src="https://i2.wp.com/www.data-imaginist.com/post/2018-09-22-what-are-we-plotting-what-are-we-animating_files/figure-html/unnamed-chunk-3-1.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Okay, what is going on? To be honest I had a different expectation about how this would fail when I started writing this. The reason why the voronoi tiles are static (and calculated based on all the points) is that the voronoi tessellation is calculated on the full panel data. At the time the voronoi tile statistic receives the data it all just belongs to the same panel since <code>gganimate</code> differentiate states using the group aesthetics. To show you how I expected this example to break down we’ll have to tell the voronoi stat to tessellate based on the groups instead:</p>
<pre><code>ggplot(data, aes(x = x, y = y)) + 
  geom_voronoi_tile(fill = 'grey', colour = 'black', bound = c(0, 1, 0, 1),
                    by.group = TRUE) + 
  geom_point() + 
  transition_states(state, transition_length = 3, state_length = 1) + 
  ease_aes('cubic-in-out')</code></pre>
<p><img src="https://i0.wp.com/www.data-imaginist.com/post/2018-09-22-what-are-we-plotting-what-are-we-animating_files/figure-html/unnamed-chunk-4-1.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Now, at least it is wrong in the way that I expected it to be. Why is this wrong? The tessellation stat outputs polygon data that is then drawn by a polygon geom, so <code>gganimate</code> does the best it can to transition these polygons smoothly between the states. In this example this is not what we expected though. We expect a tessellation to always be true, even during the transition so the tessellation should be calculated for each frame, based on intermediary point positions. In other words, here we want the expansion to happen on the raw data.</p>
<pre><code>library(tweenr)
library(magrittr)
data &lt;- split(data, data$state)

data &lt;- tween_state(data[[1]], data[[2]], 'cubic-in-out', 40) %>% 
  keep_state(10) %>% 
  tween_state(data[[1]],'cubic-in-out', 40) %>% 
  keep_state(10)

ggplot(data, aes(x = x, y = y)) + 
  geom_voronoi_tile(fill = 'grey', colour = 'black', bound = c(0, 1, 0, 1),
                    by.group = TRUE) + 
  geom_point() + 
  transition_manual(.frame)</code></pre>
<p><img src="https://i1.wp.com/www.data-imaginist.com/post/2018-09-22-what-are-we-plotting-what-are-we-animating_files/figure-html/unnamed-chunk-5-1.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Ah, we have finally arrived at the expected animation, but what a mess of a journey.</p>
</div>
<div>
<h2>Who Plots Tesselation Anyway?</h2>
<p>You may think the above example is laughably construed — this may even be the first time you’ve heard of voronoi tessellation. Hold my beer, because it is about to get even worse, even using a geom from <code>ggplot2</code> itself. We’ll start with a plot again:</p>
<pre><code>data &lt;- data.frame(
  x = c(rnorm(50, mean = 5, sd = 3), rnorm(40, mean = 2, sd = 1)),
  y = c(rnorm(50, mean = -2, sd = 7), rnorm(40, mean = 6, sd = 4)),
  state = rep(c('a', 'b'), c(50, 40))
)

ggplot(data, aes(x = x, y = y)) +
  geom_contour(stat = 'density_2d') + 
  facet_wrap(~state)</code></pre>
<p><img src="https://i0.wp.com/www.data-imaginist.com/post/2018-09-22-what-are-we-plotting-what-are-we-animating_files/figure-html/unnamed-chunk-6-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>And how might this look if we transition between <em>a</em> and <em>b</em>?</p>
<pre><code>ggplot(data, aes(x = x, y = y)) +
  geom_contour(stat = 'density_2d') + 
  transition_states(state, transition_length = 3, state_length = 1) + 
  ease_aes('cubic-in-out')</code></pre>
<p><img src="https://i0.wp.com/www.data-imaginist.com/post/2018-09-22-what-are-we-plotting-what-are-we-animating_files/figure-html/unnamed-chunk-7-1.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Oh my… The problem is more or less the same as with the tessellation – the stat creates a <em>primitive</em> data representation (here paths and not polygons) and <code>gganimate</code> does its best at transitioning those, but in doing this the intermediary frames does not resemble contour lines at all, but more a bowl of spaghetti.</p>
<p>So, could we fix it in the same way? Just prepare the data beforehand. Well, not really as we run into the first problem discussed, way up at the beginning of the blog. There is really no meaningful way of transitioning 50 points into 40. We could remove 10 and move the remaining 40, but in terms of the derived density this would look messy (but let’s try anyway):</p>
<pre><code>data2 &lt;- split(data, data$state)
data2 &lt;- tween_state(data2[[1]], data2[[2]], 'cubic-in-out', 40) %>% 
  keep_state(10) %>% 
  tween_state(data2[[1]], 'cubic-in-out', 40) %>% 
  keep_state(10)

ggplot(data2, aes(x = x, y = y)) +
  geom_contour(stat = 'density_2d') + 
  transition_manual(.frame)</code></pre>
<p><img src="https://i1.wp.com/www.data-imaginist.com/post/2018-09-22-what-are-we-plotting-what-are-we-animating_files/figure-html/unnamed-chunk-8-1.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>It sort of does the right thing, but there is a noticeable switch in the density as the 10 points disappears and reappears.</p>
<p>What we really want to do is to calculate intermediary states of the 2D densities that the contours are derived from. The densities remove the point discrepancy while presenting a statistic that can be truthfully transitioned. Unfortunately the density data is only present ephemerally inside the stat function and is not accessible to the outside world (where <code>gganimate</code> resides). We could rewrite the density_2d stat to wait with the contour transformation:</p>
<pre><code>StatDensityContour &lt;- ggproto('StatDensityContour', StatDensity2d,
  compute_group = function (data, scales, na.rm = FALSE, h = NULL, contour = TRUE, 
                            n = 100, bins = NULL, binwidth = NULL) {
    StatDensity2d$compute_group(data, scales, na.rm = na.rm, h = h, contour = FALSE, 
                                n = n, bins = bins, binwidth = binwidth)
  },
  finish_layer = function(self, data, params) {
    names(data)[names(data) == 'density'] &lt;- 'z'
    do.call(rbind, lapply(split(data, data$PANEL), function(d) {
      StatContour$compute_panel(d, scales = NULL, bins = params$bins, 
                                binwidth = params$binwidth)
    }))
  }
)

ggplot(data, aes(x = x, y = y)) +
  geom_contour(stat = 'density_contour') + 
  transition_states(state, transition_length = 3, state_length = 1) + 
  ease_aes('cubic-in-out')</code></pre>
<p><img src="https://i2.wp.com/www.data-imaginist.com/post/2018-09-22-what-are-we-plotting-what-are-we-animating_files/figure-html/unnamed-chunk-9-1.gif?w=456&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h2>What to make of this?</h2>
<p>You might feel like Alice who has stepped through the looking glass at this point. Should you always second guess whatever <code>gganimate</code> is doing? Of course not. The choice of interpolating the statistically transformed data is sound and will <em>just work</em> for most of what you want to do. I certainly want to allow gganimate to expand based on the raw data as well, though this has proven harder than expected as it is often only a subset of aesthetics you want to expand at that state (remember the problem with unmapped colour/fill).</p>
<p>Even if <em>early expansion</em> gets implemented it will only solve problems such as the voronoi example. The last contour example runs deeper and touches upon the theory of the grammar of graphics and how <code>ggplot2</code> implements it itself. Statistical transformations are often envisioned as a single operation, but can just as well be thought of as a chain of transformation (here density_2d -> contour). Alternatively one could think that it was the responsibility of the geom to calculate the contour lines. All-in-all the dichotomy of stat+geom is not so clear cut as it might appear, which has not been much of a problem when generating static plots. With the advent of <code>gganimate</code> this problem becomes more pertinent and I honestly don’t know the best way to address it. In a perfect world, all stats would return the data-state best fitted for expansion but this would require the <code>finish_layer()</code> hook to be more powerful, and would obviously require rewrites of a slew of geoms/stats. Then comes the question of whether it is even the responsibility of geom/stat developers to consider <code>gganimate</code> in the first place…</p>
<p>No matter the eventual solution to all this, I hope this post has made you a bit more aware of what happens to the data you plot as you passed it into <code>ggplot2</code>. Visualisations are after all first and foremost about data transformations…</p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.data-imaginist.com/2018/what-are-we-plotting-what-are-we-animating/"> Data Imaginist</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/vgqb48lL1RQ" height="1"></a></div>