How to return the same event_data information for selected points even after modifying the data?

I’m trying to do something seemingly simple: when the user clicks on a data point or select multiple points with lasso selection, I want to draw these points in a different colour. In order to do that, I look at what points are selected, and add a col variable to the dataframe, and I tell the ggplot to colour the points according to that column.

It does work for the first selection. But whenever there are already selected points, selecting the next set of points doesn’t work. I’ve added debug statements to see what data is returned from plotly, and it seems like it returns different pointNumber and curveNumber after the initial selection. I couldn’t find any documentation about how these variables work and I’m not sure how to fix this issue.

Here’s a GIF showing the issue

And here’s code to reproduce:

library(plotly)
library(shiny)

ui <- fluidPage(
  plotlyOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderPlotly({
    click_data <- event_data("plotly_click", source = "select")
    select_data <- event_data("plotly_selected", source = "select")
    data <- mtcars
    data$col <- "black"
    if (!is.null(select_data)) {
      cat(str(select_data))
      idx <- select_data$pointNumber + 1
      data[idx, "col"] <- "blue"
    }
    if (!is.null(click_data)) {
      cat(str(click_data))
      idx <- click_data$pointNumber + 1
      data[idx, "col"] <- "red"
    }
    p <- ggplot(data, aes(mpg, wt, col = I(col))) + geom_point()
    ggplotly(p, source = "select")
  })
}

shinyApp(ui, server)

I’ve also been told that perhaps what I need to do is create my own row identifier and pass it to the key aesthetic. I’m not sure what that means I tried defining key <- row.names(data) and then passing key=key to ggplot’s aes(), but that didn’t seem to change anything.

Carson Sievert answered my question on a gist

Here’s the answer:

I know it seems counter-intuitive, but pointNumber isn’t a reliable row identifier. Use a key variable like this:

library(plotly)
library(shiny)

mtcars$key <- row.names(mtcars)
mtcars$col <- "black"

ui <- fluidPage(
  plotlyOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderPlotly({
    click_data <- event_data("plotly_click")
    select_data <- event_data("plotly_selected")
    if (!is.null(select_data)) {
      mtcars[mtcars$key %in% select_data$key, "col"] <- "blue"
    }
    if (!is.null(click_data)) {
      mtcars[mtcars$key %in% click_data$key, "col"] <- "red"
    }
    p <- ggplot(mtcars, aes(mpg, wt, col = I(col), key = key)) + 
      geom_point()
    ggplotly(p) %>% layout(dragmode = "lasso")
  })
}

shinyApp(ui, server)

Hello Daattali,

I have some questions,

I am developing a shiny app for plotting some data, my output graphs are a scatterplots. I am plotting these graphs using plotly, ggplotly and ggplot2 etc.

  1. Can I select multiple points using click feature in Plotly. I dont want to use lasso selection, because the points I want to select are far from each other.

  2. I want user to select 5-10 points by clicking, these points should reflect below the plot in table form. And after that I want to draw a line through these selected points. Is it possible?

  3. Can I delete some points interactively by just selecting those on the plot and pressing a delete or remove button, maybe I can introduce a button which can do the delete operation.

Thank you
Kshitij