Get startedGet started for free

Recap: Animation

1. Recap: Animation

So far, you've explored the launches data set using interactive graphics. In this this lesson, you'll tell the story of the space race using animation. Before doing this, let's quickly recap the key ideas behind animation in plotly.

2. Keyframe animation

Recall, that animation in plotly is achieved by adding the frame aesthetic. For example, in chapter two we animated a scatterplot of CO2 emissions against adjusted per capita GDP by mapping the year to the frame aesthetic. We also mapped country to ids to ensure object constancy. That is, to avoid reshuffling of points if the order of the rows changed from year to year.

3. Cumulative animations

In chapter 2 you were also introduced to cumulative animations. To create this animation, we had to create a new dataset that accumulated cases over time.

4. Cumulative animations

To create these accumulated datasets we appealed to tools from both the dplyr and purrr packages. For example, to accumulate the GDP for Belgium, we split the dataset by year, and then used accumulate() and bind_rows() to recursively combine the data frames for each year. We also used set_names() to meaningfully name each element of the list created by split prior to accumulating the data frames. If the list is already named, then this step is optional.

5. Coping with staggered starting points

For cumulative animations with multiple groups, plotly requires a common baseline to be present, even if it does not naturally exist. For example, here's an animation of the monthly downloads of four interactive data viz packages from CRAN. In 2014, ggvis was the only package on CRAN. plotly came along in late 2015, and rbokeh and highcharter then started getting downloads in 2016.

6. CRAN download data

The monthly logs dataset consists of the number of downloads of each R package at the end of each month. If the package wasn't downloaded that month, then it doesn't appear in the dataset; thus, we lack a common baseline for these packages.

7. What if we ignore the baseline issue?

If you don't realize that you don't have a common baseline for your groups, then you can still render an animation using the split and accumulate strategy, but not all groups will be plotted. Additionally, you'll see a warning message talking about an incorrect replacement length.

8. Completing the data set

In the monthly logs dataset, if a package wasn't downloaded, then there is no entry for that date. To resolve this issue, we can fill in 0 counts for these dates and create a completed dataset where all four packages have values for each date. To do this, we can use the complete function provided by the tidyr package. The complete function turns implicit missing values into explicit missing values by adding the missing combinations of the variables to a dataset. Here, we complete the monthly_logs dataset by adding all combinations of package and decimal date (dec_date). By default, complete fills in the missing values with NAs. A named list can be passed to the fill argument to provide alternate values. For example, we pass the list with element downloads equals 0 to fill in the missing values in the downloads column with zeros rather than NAs. The complete_logs dataset now has 228 rows, and packages that weren't on CRAN yet have zeros for their download volume.

9. Animating the completed data

Now that we have a completed dataset, we can use the same cumulative animation code as before to create the animation of CRAN downloads.

10. Let's practice!

Now, it's time to animate the space race using the tools you've learned.