1. Linking two charts
In the previous chapter you learned how to animate your plotly charts to explore associations over time. This allowed you to discover structure that is hard to detect using only static charts. In this chapter, you will learn how to link two charts, allowing an interaction with one to change the other.
2. Exploring clusters
For example, we can explore the relationship between the frontal lip and rear width measurements on Australian rock crabs by linking it to a jittered scatterplot of sex against species.
By linking the two charts, we can explore the clustering seen on the plot of frontal lip against rear width.
Using linked highlighting, often called linked brushing, we see that sex appears to explain the two clusters more than species, but that species and sex considered together may result in four distinct clusters.
3. Exploring longitudinal data
The same plumbing behind linked brushing allows for grouped highlighting within a single chart.
For example, here we are exploring the relationship between wages and experience for high school dropouts. Brushing a single individual's data allows us to explore unusual features that are impossible to see from the original chart.
4. Crosstalk
In this course, we'll use the crosstalk package to link plotly charts and enable grouped brushing.
Crosstalk enhances the htmlwidgets package to allow you to link plots purely with JavaScript. When you create a plotly chart, you're creating an HTML widget in the background. Crosstalk enables HTML widgets to be linked, and produces static HTML files that you can host on any web server.
When paired with plotly this is a powerful combination that enables you to create linked views entirely within R.
5. 2014 world indicators
In this lesson, you'll learn how to link two scatterplots created from the world indicators data from 2014. Specifically, you'll link a scatterplot displaying CO2 emissions against adjusted per capita income to a scatterplot of CO2 emissions against military spending.
6. Separate views
To understand how to use crosstalk, let's first consider two separate views. Here we have two scatterplots: one displaying CO2 emissions against adjusted per capita income, and the other displaying CO2 emissions against military spending.
To create these scatterplots, we use familiar plotly code and combine the two charts using the subplot command.
Notice that in the resulting chart, selecting points on one chart has no impact on the other. That is, there is no link between the charts.
7. Linked views
Now, let's consider the code necessary to link the two charts, so that selecting a point on one chart is reflected by the selection of the same point on the other chart.
8. Linked views
To create linked views, we first need to create a SharedData object. Here, we define shared underscore data using the SharedData$new call and passing in the world2014 data frame.
Next, we create the two individual plotly charts as before, using shared underscore data instead of the original data frame.
To combine the plots we can use the subplot command without any alteration.
9. Linked brushing
In the previous example, only a single point was selected when you clicked on a point, which is very limiting. To enable linked brushing, where multiple points can be selected using the lasso or box select tools, we must use the highlight command from plotly.
To do this, we pipe our plotly chart into the highlight command, and specify on equals plotly underscore select. The resulting plotly chart now allows linked brushing.
10. Let's practice!
Now that you have seen how to link two plotly charts, it's time to practice. After you create each chart, be sure to explore what these coordinated and linked views allow you to uncover in your data.