# Button R plot.ly

**URL:** <https://community.plotly.com/t/button-r-plot-ly/3700>\
**Category:** Plotly R\
**Created:** [March 22, 2017, 12:57pm UTC](https://community.plotly.com/t/button-r-plot-ly/3700 "2017-03-22T12:57:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CLARC](https://avatars.discourse-cdn.com/v4/letter/c/8491ac/32.png) [@CLARC](https://community.plotly.com/u/CLARC)\
**Post date:** [March 22, 2017, 12:57pm UTC](https://community.plotly.com/t/button-r-plot-ly/3700/1 "2017-03-22T12:57:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![afrc](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@afrc](https://community.plotly.com/u/afrc)\
**Post date:** [April 15, 2018, 8:23pm UTC](https://community.plotly.com/t/button-r-plot-ly/3700/2 "2018-04-15T20:23:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bcd](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@bcd](https://community.plotly.com/u/bcd)\
**Post date:** [April 16, 2018, 11:53am UTC](https://community.plotly.com/t/button-r-plot-ly/3700/3 "2018-04-16T11:53:04Z")

</div>

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")))
  ))
```
