plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

For a categorical colour aesthetic, there appears to be no documented way to use selectedpoints to select individual points.

Open krivit opened this issue 2 months ago • 0 comments

The following example highlights the first point from each colour group (the first "a" point and the first "b" point):

library(plotly)

d <- data.frame(x = 1:4, c = c("a", "a", "b", "b"))

plot_ly(d) |>
  add_trace(x=~x, y = ~x, color = ~c, type = "scatter", selectedpoints = list(0))

From the plot_ly() documentation, it appears that this code implicitly creates multiple traces, one for each colour, so this behaviour makes sense on this level (even if it's surprising). However, there appears to be no documented way to access individual traces. I've tried a number of permutations

In my opinion, if the code is going to transparently create multiple traces like that, it should also transparently map selectedpoints provided by the user (ideally as 1-based indexes or logical vectors, for consistency with R) to their positions within each trace.

krivit avatar Nov 03 '25 06:11 krivit