1. Cumulative Animations
The animations we've considered thus far tracked data over time or the level of a factor. In this lesson, we'll explore how to represent cumulative information.
2. Example
A cumulative times series is a common animation task found in financial dashboards. In this lesson we'll animate the time series of adjusted per capita income in Belgium.
3. Belgian income data
The data are drawn from the world_indicators dataset by filtering out the rows corresponding to Belgium. The resulting data frame has 59 rows, one for each year between 1960 and 2018.
4. What's the frame?
At first glance this problem seems very similar to the animations we have already seen. However, we encounter a problem when specifying the frame aesthetic using the original dataset. Keep in mind that each row represents a single year.
5. What's the frame?
Our goal is to create a cumulative time series. In order to do this, each frame must contain a path connecting all income values up to that time point.
If we specify `frame = ~year` as in the previous lessons, then each frame can only display the current income value, since each year is associated with only one row.
6. Accumulating data sets
Rather than working with the original data, we must create a new data frame reflecting the cumulative nature of each frame. The new dataset will have more rows than the original, since each frame contains information from the rows for previous time points in addition to the current time point.
Let's start by visualizing how this accumulation of data works. The first year in the dataset is 1960, which requires only one row of data to plot the frame.
7. Accumulating data sets
To create the plot for 1961 we need data from both 1960 and 1961, so these rows are appended to the previous data, and a unique frame id is added.
8. Accumulating data sets
To create the plot for 1962 we need three rows of data, corresponding to 1960, 1961, and 1962. Again we append these rows and add a unique frame id.
Now that you can visualize how the data accumulate in the dataset, let's return to R.
9. split()
We'll use tools from the dplyr and purrr packages for data wrangling to create cumulative datasets for each frame.
First, we split the dataset into a list of dataframes based on the year variable using the command split(.$year).
10. accumulate()
Next, we create a list of cumulative datasets by recursively binding the data frames in our list together using the accumulate() function. Here, we use bind_rows() to combine data frames. The arguments .x and .y specify that we will combine the first and second data frames.
Since accumulate works recursively, the result from combining two datasets becomes the new first data frame in the next step. Essentially, new rows are being added to the data frames as accumulate moves down the list.
Also, notice that the function is specified using formula notation, since it starts with a tilde.
11. name
Then, we use set_names() to create logical names for each frame. Here, we use the years 1960 to 2018 as frame titles. This isn't necessary if the list is already properly named.
12. combine
Finally, we combine the list of cumulative data frames using bind_rows().
13. The accumulated data set
Here are the first 10 rows of the accumulated dataset. Notice that for each frame, all of the years up to that point are included.
14. animate
To create the animation, we add our animation code to the accumulation pipeline.
The canvas is created by mapping year to x and income to y. Next, we add the lines trace specifying that the frame column should be mapped to the frame aesthetic, and hide the trace legend.
15. Let's practice!
Now, it's your turn to create cumulative animations to deepen your understanding and explore more economic data.