1. Polishing animations
Now that you know how to create animations in plotly, let's explore what customizations are available for animations.
2. Customizations
When working with animated graphics there are two ways to polish the resulting graphic: we can polish aspects of the animation, such as how we transition between frames or how the slider appears; and we can polish the graphical elements, such as glyphs, axes, and tooltips.
When you are designing animated graphics in plotly, be sure to think about each of these aspects before you publish your graphics.
3. Animation options
Let's revisit the animation from lesson 1, where we used the default animation options. Here, this is stored in the `ani` object.
To modify the transitions between frames, we use the `animation_opts()` function. Let's begin by adding this layer into our code to understand the defaults.
4. Animation options
There are four components of the animation_opts() function:
The **frame** argument specifies that there are 500 milliseconds between frames, including the time used to transition between frames.
The transition argument specifies the duration of the smooth transition between frames in milliseconds. By default, this is set equal to the frame, resulting in a smooth transition between frames with no focus on a static graphic. If transition is set to a value less than the frame, then each frame will be displayed as a static interactive graphic for the difference.
The easing argument specifies the type of transition used between frames. By default a linear transition is used, resulting in very smooth transitions.
The redraw argument specifies whether the entire graphic should be redrawn. In some cases specifying redraw = FALSE will result in far smoother transitions.
Now that you understand the defaults, let's polish this animation.
5. Speeding up
To speed up the animation, set the frame argument to a value less than 500. Here, we specify that there should be 300 milliseconds between frames rather than 500.
6. Pausing between frames
If you want to give the viewer time to process specific locations or relationships, then it might be useful to pause the animation between transitions. Here, we specify there should be 700 milliseconds between frames with a 350 millisecond transition, resulting in a 350 millisecond pause on each frame.
7. Bouncing points
To change the type of transition so that the points bounce between each location we specify easing = "bounce".
There are many different easing options available, including quadratic, exponential, and elastic transitions. Be sure to explore these options during the exercises.
Next, let's explore how we can customize the slider.
8. Slider options
By default, plotly includes a slider on your animation tracking the current value of the variable mapped to the frame aesthetic, printing the name of the variable and the value.
9. Removing the slider
To polish the slider, we use the animation_slider() function. For example, to remove the slider, we set hide equals TRUE.
10. Editing slider text
Using the animation_slider() function we can also change the slider text. In this example it's rather obvious that we are keeping track of the years, so we can remove the prefix "year: " from the plot to reduce nondata ink. Setting the prefix of the current value to NULL makes this edit. Note that we always pass a list of arguments to the `currentvalue`.
11. Editing slider text
We can also call attention to the slider annotation by changing its color or font size. To do this we add a font element to the currentvalue list. Here, we set the font color to black and the font size to 40.
12. Let's practice!
In this lesson, you learned how to polish elements of an animation using the animation_opts() and animation_slider() functions. Now, it's time to practice these tools with the state economic index data.