🏥 🏭 Working on the COVID-19 response in Canada? Plotly & the Canadian government can help you and your organization. Learn more and get in touch.

Selecting/Deselecting traces from code

Hi everyone!

I wonder if there’s any way to select or deselect traces from inside the code just the way we tend to do with clicking on the legends?

I don’t want to subset the data, I just want to control the visibility of the plot.

Hi @Uriah,

I was also looking into this recently.
If you want to hide them, but keep them in the legend, you can use the following (assuming you are using Shiny):

myPlotProxy <- plotlyProxy("distPlot", session)

observeEvent(input$someInput, {
        plotlyProxyInvoke(myPlotProxy, "restyle", list(visible = FALSE), indicesToRemove)
        plotlyProxyInvoke(myPlotProxy, "restyle", list(visible = TRUE), indicesToKeep)
        plotlyProxyInvoke(myPlotProxy, "restyle", list(visible = "legendonly"), indicesToHide)
      })