Button R plot.ly


#1

Hi!
I’m trying to make a new plot with R ploty. I want to have different buttons that allow me to show/ hide different lines from s different datasets. I know that with the legend I can do something like this, but I need buttons to select the data set and hide/show the line. I can’t have a legend with 50 lines.
Someone knows how to make a button in R plot with two click options, one for showing the line and the other click for hide the line?
Thanks


#2

have you found a solution? I have the same problem.
thanks :wink:


#3

Hi @CLARC @afrc

You can toggle visibility, like:

library(plotly)

df <- data.frame(x = c(1,2,3,4,5), y1 = c(3,3,3,3,3), y2 = c(6,6,6,6,6))

p <- plot_ly(df, type = 'scatter', mode = 'markers') %>%
  add_trace(x = ~x, y = ~y1) %>%
  add_trace(x = ~x, y = ~y2)

p <- p %>% layout(
  title = "Button Restyle",
  xaxis = list(domain = c(0.1, 1)),
  yaxis = list(title = "y", range = c(0,10)),
  updatemenus = list(
    list(
      type = "buttons",
      y = 0.8,
      buttons = list(
        
        list(method = "restyle",
             args = list("visible", c(T,T)),
             label = "All"),
        
        list(method = "restyle",
             args = list("visible", c(F,T)),
             label = "Trace 0"),
        
        
        list(method = "restyle",
             args = list("visible", c(T,F)),
             label = "Trace 1")))
  ))