Introduction to animated graphics
1. Introduction to animation
In this chapter we'll explore how to create animations. To begin, let's consider a situation where animation helps us explore the data.2. CO2 emissions and income
Our goal in this lesson is to graphically explore how the relationship between carbon dioxide emissions and per capita gross domestic product has changed over time. To see why we want to utilize an animation, let's review our existing graphical toolkit.3. Subplots
For a small number of years creating subplots for each year would display these relationships in a readable format. For example, we could explore how the relationship has changed in ten year increments.4. Limitations of subplots
While subplots work well for a few comparisons, they quickly become hard to read as the number of subplots increases. For example, if we attempt to display scatterplots of CO2 emissions against income for a thirty-year span, it's nearly impossible to perceive changes.5. Animation
Here, we display the same 30-year span from the previous slide as an animation. By animating the graphic, we bring the temporal change into focus, allowing us to easily perceive the somewhat subtle increase in both CO2 emissions and income, as well as how the relationship evolves. Now that you have seen the benefits of animation, we need to understand how plotly builds animations.6. Keyframe animation
In plotly, animations are created using a process known as keyframe animation. That is, animations are created by stringing together a series of interactive graphics, much like static images are strung together on film to create a movie. In our example, each frame is a single interactive scatterplot. These frames are then combined using smooth transitions to create the animation. Let's return to our data set to understand how apply this idea.7. The frame aesthetic
For each frame of our animation we wish to create a scatterplot with income on the x-axis and co2 on the y-axis. To achieve this we add the frame aesthetic parameter to our trace, telling plotly what variable from the dataset controls the frames. The remaining pieces of the code are as expected: - We pipe our data into the plotly layer and define our variable mappings. - We add the markers trace with the additional frame aesthetic, and also specify `showlegend = FALSE` to remove an unnecessary legend. - Last, we apply a log transformation to the x-and y-axes.8. Object constancy
The code results in an animation similar to one we've already seen, but something odd happens between 1990 and 1994: the points appear to reshuffle. Digging into the dataset we find the culprit: a few countries have missing CO2 values in these years that previously had data values. When creating the animation, we didn't specify ids to uniquely define that the 17th glpyh should always represent Belgium, so if the order of the rows changes due to a missing value, the 17th glyph may suddenly represent a different country.9. The ids aesthetic
To ensure that the glyphs in our animation always represent the same country, we map country to the ids aesthetic in the markers trace. The result is a smooth transition between each year, allowing you to track a country as a single glyph over time.10. Let's practice!
Now that you understand how keyframe animations are implemented in plotly using the frame and ids aesthetics, it's time to practice these concepts.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.