Get startedGet started for free

Selection strategies

1. Selection strategies

So far you've explored how to create linked views in plotly. Now, you'll explore different strategies for selecting cases on a chart.

2. Two selection strategies

The examples you've seen illustrated transient selection, where previously selected values are forgotten upon the selection of new values. Transient selection is useful when a single group needs to be drawn into focus. An alternative strategy is that of persistent selection, where the selected values are accumulated until a user event clears that selection. For example, if you're examining outliers across charts you may wish to highlight different types of outliers in different colors, but want selections to persist to facilitate exploration.

3. Example

Before we can implement persistent selection, we first need a plotly chart. In this example we'll start with linked scatterplots of country-level CO2 emissions in 2014 against the proportion of urbanization; and against a country's per capita GDP.

4. Generate the base chart

To create the linked scatterplots, we create a shared data object using the world2014 data set, and pipe this shared data object into familiar scatterplot code, saving each chart as an object. We then use subplot() to combine the plots and do a bit of polishing.

5. Persistent selection

One way to achieve persistent highlighting in plotly is to use the highlight command and set persistent equals TRUE. The resulting plots now allow you to identify multiple interesting points one at a time as you explore. A second way to achieve persistent highlighting is to press the shift key to add points to a selection.

6. Highlighting in color

It can also be useful to highlight multiple groups or types of observations using different colors. For example, we may wish to flag the two largest outliers in terms of CO2 emissions in one color, and a few other interesting points in another. This differentiation helps reveal structure, especially clusters, that could be missed otherwise.

7. Highlighting in color

To enable the colorpicking widget, we add dynamic equals TRUE to the highlight command.

8. Two manipulation types

Now that we've reviewed transient and persistent selection, let's consider how we ask users to interact with a chart. So far in the course we've used direct manipulations, such as clicking to select points, where we interact with the data displayed on the chart. An alternative approach is to allow users to use dropdown menus or input boxes to interact with the chart. While you may be familiar with indirect manipulation in Shiny apps, it's possible add this in plotly without Shiny.

9. Difficult selections

To see the power of indirect manipulation, let's return to the time series plots of per capita GDP by country from lesson 2. While highlighting an individual country enables useful comparisons, it can be quite difficult to select the exact country you want to examine due to overplotting. A dropdown selector resolves this difficulty.

10. Indirect manipulation

The time series plot on the last slide was created using this code chunk discussed in lesson 2. Remember that we need to create a sharedData object to enable grouped highlighting on the chart.

11. Indirect manipulation

To add a dropdown menu, we pipe the chart into highlight and specify selectize equals TRUE. Notice that the key in the ShareData object is the country, and that we added a title for the dropdown menu by adding group equals the string "Select a country".

12. Selectize = TRUE

The resulting chart allows you to select countries using either the textbox or dropdown menu, making it possible to select a country that you can't easily identify from the chart alone.

13. Let's practice!

Now that you understand the different selection strategies and have expanded your toolkit, 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.