Using Plotly Events for Drill Downs

Plots in your application can give a great overview of events and allow you to observe characteristics like trends or seasonalities. However, often people reading these graphs want to dig deeper after the first glance. In your app, you can use Plotly’s event data to trigger filtered plots or tables with one click. Let’s see how you can do this:

We start with a simple barplot that allows the user to check the correlation between two variables. In our case, we use the iris dataset and plot the correlation between sepal length and its width.

So far, so simple. We can observe that correlation between length and width varies between species. However, in the next step, a user might want to check the individual measurements. Perhaps he does not trust the calculated value we visualised and wants to see for himself. Or he wants to verify that the relationship is indeed linear and so on. Of course, there are various ways to accomplish this. You could add an additional scatterplot underneath with some species-dependent colouring. However, we want to make it even easier. A user should click on a bar and the respective plot should be shown.

Two things need to be done. First, we need to be able to register which bar a user clicks on. Secondly, the data set should be filtered depending on this click and a scatter plot should be displayed. The first step is documented in the code below. What might differ from your basic plotly object (If you’re not familiar with plotly, it might be a good idea to check it out) are the source parameter in the beginning and the function event_register. They allow you to get the click event for this specific plot as a reactive value. With the source parameter, we name the object so that in cases with multiple plots, a click can be distinguished. The event_register defines to which event we exactly want to listen to. There are quite some to choose from depending on your need.

 plot_ly(source = "plot_correlation") %>%
      add_trace(data = df_subset,
                x = ~Species,
                y = ~cor_sepal,
                type = "bar") %>%
      layout(title = "Correlation Sepal Length / Width",
               yaxis = list(title = "Correlation")) %>%
      event_register("plotly_click")

Once completed, we work on the part to handle the event and filter the dataset. In the first part of the code, we “listen” to the click event and save whatever is clicked to a reactive value. As you’ll see, you get a couple of different values. We are for now only interested in the value depending on X. In the second part of the code, we use that reactive value to filter the iris dataset and plot the subset as a scatterplot.

 observeEvent(event_data(event = "plotly_click",
                          source = "plot_correlation"), {

    clicked <- event_data(event = "plotly_click",
                          source = "plot_correlation")

    if (!is.null(clicked)) {
      values$selected_species <- clicked$x
    }
  })

  output$correlation_plot <- renderPlotly({
    validate(need(values$selected_species, message = "click on a bar"))

    df_susbet <- iris %>%
      filter(Species == values$selected_species)

    plot_ly() %>%
      add_trace(data = df_susbet,
                x = ~Sepal.Length,
                y = ~Sepal.Width,
                type = "scatter") %>%
      layout(title = paste0("Scatterplot ", values$selected_species))
  })
}

That’s it. We even reused the parameter of the clicked bar in the title of the scatterplot. The final result will look like this. You can click on a bar and underneath the new filtered scatterplot is shown. If you scroll down, you’ll find the code for the whole app.

This post first appeared on rshiny.blog.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: