Get startedGet started for free

Interactive and dynamic graphics

1. Introduction

Hi, I'm Adam Loy, and I'll be your instructor for this course on animated and linked graphics in R.

2. Motivation

Interacting with your graphics, or watching them change over time, allows you to gain insight that may be difficult to gain from static plots, and allows people to explore your analysis without pouring through many different views. For example, it's far easier to understand how the relationship between country-level CO2 emissions and income evolve over time using an animation than a large number of static plots. In this course you'll learn how to explore multivariate relationships using both animation and interactivity. Before building more complicated graphics, let's review a few basic ideas about plotly.

3. plotly

In this course we will use plotly, a visualization library for interactive and dynamic web-based graphics. While there are other visualization libraries available for R, plotly is still under heavy development, so it's a great time to learn how to harness its power.

4. Types of graphics

Before you start creating graphics, it's important to think carefully about what type of graphic best suits your purpose: static, interactive, or dynamic. To highlight features of each type of graphic, let's consider creating a graphic to explore the price of the iShares all countries world index, an ETF that tracks the global stock market. <!-- Might be useful to show a snippet of the data here as well to give a sense for the data that will be plotted. -->

5. Static graphics

A static plot remains permanently fixed after you create it. For example, you can create a time series plot of the closing price of the All Countries World Index over the course of 2017 for inclusion in a report.

6. Interactive graphics

In contrast, an interactive graphic can be updated based on actions performed by the user. In our example, an interactive graphic allows us to hover above a specific date to see the closing price, or to zoom in and focus only on the fourth quarter. Simple interactions vastly increase your ability to explore the story your data are telling. Soon you'll review how easy plotly makes it to add these simple, but powerful, interactions to your graphics.

7. Dynamic graphics

Unlike interactive graphics, dynamic graphics change automatically. One example is an animation showing how the All Countries World Index changed throughout the course of 2017. Similarly, we could design a dynamic graphic to update every five minutes over the course of a trading day to communicate the current price of the fund. The key here is that neither of these examples require user input once the code to generate the graphics is written.

8. plotly review

To review the basics of plotly, let's create an interactive time series of the closing prices of the All Country World Index in 2017. In order to create the time series plot we need to map Date to the x-axis and Close to the y-axis.

9. plotly review

After loading the plotly package, we pipe our data into the `plot_ly` function, which creates the HTML canvas on which our plot is rendered, much like the ggplot() base-layer in static graphics. Here, we map Date to the x-axis and Close to the y-axis. Notice that we use a tilde to specify that we are mapping a variable from the data set to an aesthetic parameter rather than specifying a specific value. Finally, we add a line trace using the `add_lines()` function, just like adding a layer in ggplot2, but using the pipe operator.

10. plotly review

As you can see, the resulting graphic uses a line to connect the ordered pairs of dates and closing prices. Additionally, the icons at the top of the graphic highlight ways that you can interact with your plot. During the exercises be sure to explore these interactions so that you see the power of plotly!

11. Let's practice!

Before diving deeper into plotly, let's practice the basics.