Get startedGet started for free

Brushing groups

1. Brushing groups

In the previous lesson, you learned how to create coordinated linked views between plotly charts; however, you brushed individual observations. In this lesson, you'll explore how to easily select groups of observations.

2. World indicators

In this lesson, you'll explore two aspects of the world indicators dataset. Specifically, you'll explore how country-level income has changed over time, and how the relationship between CO2 emissions and military spending as a percentage of GDP changes by region. Recall that the world indicators dataset contains measurements such as income, co2, and military spending for each year between 1960 and 2018.

3. Selecting individual time series

A natural example of when you want to select a group of observations is when plotting time series data. In this example we are plotting the adjusted per capita income of each country from 1960 to 2017. While this is clearly an interactive chart, it does not allow us to select and highlight the groups of observations for any specific country.

4. Selecting individual time series

To enable selection and highlighting, we first create a SharedData object where the country is mapped to the key variable. Then, we pipe this SharedData object into our plotting code. By adding the key argument we enable the selection of all the points in the group defined by the key.

5. Selecting groups on a scatterplot

The idea of grouped selection extends to other plot types. For example, to highlight all of the countries within the same region on a scatterplot of CO2 emissions against military spending, we again create a SharedData object and use the six_regions variable as the key. Now, the clicking on a single point highlights all points in the same region.

6. Linking a summary and detailed view

Grouped selection is useful on a single chart, but it is also a powerful element of your linked views toolkit. For example, grouped selection allows you to create a dotplot of the average military spending by region and link it to your scatterplot of CO2 emissions against military spending, enabling easy exploration of subgroups.

7. Linking a summary and detailed view

The key to creating these linked views is again the SharedData object. First, we filter out the rows for 2014 from the world indicators data frame, and create a SharedData object with a key defined by the six regions. Next, we create a dotplot of average military spending. Notice that we first pass the shared data into the plot_ly() function, and then use group_by and summarize to calculate the average military spending by region. SharedData objects cannot be passed directly into our usual data manipulation pipeline, but we can use the dplyr commands within the plotly pipeline. We can create the scatterplot as expected, by passing the shared data object into the plotly pipeline. To render the linked views we again create subplots. Here, we hide the legend since the dotplot will act as our legend for region.

8. Linking a summary and detailed view

The resulting chart enables us to easily brush all of the points within a region in the scatterplot by clicking on the dotplot. This allows us to zoom in on an entire group while still displaying the remainder of the data in the background, unlike when we deactivate traces in a plotly legend.

9. Let's practice!

Now that you have seen how SharedData objects facilitate group-wide brushing, it's time to practice.

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.