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")
You could add similar JavaScript code to your R code by using htmlwidgets
.
Notes:
devtools::install_github("ropensci/plotly")
If it doesn't work in RStudio, you'll need to export the graph as HTML
library(plotly)
library(htmlwidgets)
p <- plot_ly() %>%
add_trace(y = c(1,2,1), x = c(1,2,3), mode= "graph", type='scatter') %>%
add_trace(y = c(3,4,2), x = c(1,2,3), mode= "graph", type='scatter')
javascript <- "var myPlot = document.getElementsByClassName('plotly')[0];
myPlot.on('plotly_legenddoubleclick', function(d, i) {return false});"
p <- htmlwidgets::prependContent(p, htmlwidgets::onStaticRenderComplete(javascript), data=list(''))
p
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With