Removing or restyling specific points in a trace (not an entire trace)

I want to be able to remove a few points from a trace, or alternatively modify their style to set their opacity to 0.

I’m not sure if the former is even technically possible, but I think the latter should be possible. I found an old thread on github (don’t have the link anymore) where someone said you can use eg marker.color[5] to change the colour of the 5th point in javascript. I couldn’t get it working with R. Does anyone know the R API to achieve that?

Hey @daattali

Here is an example with a dropdown:

p <- plot_ly(x = c(1,2,3,4,5), y = c(1,2,3,4,5),
             marker = list(color = c('blue','blue','blue','blue','blue'))) %>%
  layout(
    updatemenus = list(
      list(
        y = 0.8,
        buttons = list(
          
          list(method = "restyle",
               args = list("marker.color[2]", "rgba(31,119,180,1)"),
               label = "On"),
          
          list(method = "restyle",
               args = list("marker.color[2]", "rgba(31,119,180,0)"),
               label = "Off")))
      )
    ) 

Or if you just want to use plotly_build:

p <- plotly_build(p)
p$x$data[[1]]$marker$color[3] <- 'rgba(31,119,180,0)'

Thanks @bcd – do you know how to get this to work when using add_markers() - perhaps the layer number needs to be specified? Here’s a slight variation of your code:

plot_ly() %>%
    add_markers(x = c(1,2,3,4,5), y = c(1,2,3,4,5),
                color=I(c('blue','blue','blue','blue','blue'))) %>%
    layout(
        updatemenus = list(
            list(
                y = 0.8,
                buttons = list(
                    
                    list(method = "restyle",
                         args = list("marker.color[2]", "rgba(31,119,180,1)"),
                         label = "On"),
                    
                    list(method = "restyle",
                         args = list("marker.color[2]", "rgba(31,119,180,0)"),
                         label = "Off")))
        )
    ) 

For reference, the github issue with some info is at https://github.com/plotly/plotly.js/issues/144

For the above, you need to add color inside marker = list()

Alternatively, you can achieve this with the javascript via the onRender function. Below, click on any data point and it’ll restyle the 3rd data point in the 2nd data trace to opacity 0. Double click and it’ll restore opacity value to 1.

p <- plot_ly(x = c(1,2,3,4,5),) %>%
  add_markers( y = c(1,2,2.5,4,5), marker = list(color=c(rep('red',5)))) %>%
  add_markers(y = c(5,4,3.5,2,1), marker = list(color=c(rep('rgba(31,119,180,1)',5)))) %>%
  onRender("
           function(el, x) {
           el.on('plotly_click', function() {
           
           update = {'marker.color[2]': 'rgba(31,119,180,0)'};
           Plotly.restyle(el, update, [1]);
           
           });
           }
           ") %>%
  onRender("
           function(el, x) {
           el.on('plotly_doubleclick', function() {
           
           update = {'marker.color[2]': 'rgba(31,119,180,1)'};
           Plotly.restyle(el, update, [1]);
           
           });
           }
           ")

Thank you! That seems to do the trick, I would have never tried that, thanks.

Although now I’m running into new issues… alpha seems to be ignored now, and when I tried to simply add alpha = rep(alpha, nrow(data)) into that same marker list, it doesn’t seem to work. And another problem is that the colour legend disappears when I use the marker list as opposed to just color = .... You’ve already answered my original question, these are different problems, so no worries if you can’t answer these as well. Thanks regardless!

I know it’s a very old thread, but I think you can just replace the Y value of the point you want to get rid of with None. It worked for me, anyway.