Search code examples
rplotlylegendr-plotly

Disable the legend double click event


How do I disable the "Double click on legend to isolate one trace" interaction in plotly for R? I want a double click to just have the effect of two clicks.

Here is an example on how to do this with Javascript:

Plotly.newPlot('graph', [{
  y: [1, 2, 1]
}, {
  y: [3, 4, 2]
}])
.then(gd => {
  gd.on('plotly_legenddoubleclick', () => false)
})
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<body>
  <div id="graph"></div>
</body>

It uses gd.on('plotly_legenddoubleclick', () => false). I do not know how to translate this to R.

Example in R:

library(plotly)

plot_ly() %>%
  add_trace(y = c(1,2,1), x = c(1,2,3), mode= "graph")    %>%
  add_trace(y = c(3,4,2), x = c(1,2,3), mode= "graph")

Solution

  • I don't think javascript is necessary for this anymore, if it was in 2018. You can achieve this outcome directly in R by setting the legend's itemdoubleclick attribute via layout():

    library(plotly)
    
    plot_ly() %>%
      add_trace(y = c(1,2,1), x = c(1,2,3), mode= "graph") %>%
      add_trace(y = c(3,4,2), x = c(1,2,3), mode= "graph") %>%
      layout(legend = list(itemdoubleclick = FALSE))