Black Lives Matter. Please consider donating to Black Girls Code today.

Updatemenu button to show/hide ribbons

Hi,

I would like to add a button to show/hide the ribbons on the plot modifying the opacity parameter. The problem is that, when I click one “Hide ribbon”, everything disappears includinf the lines but I want to display them. How can I hide only the ribbons?

Thanks.

require(plotly)
require(dplyr)

x <- 1:40
y <- c(1:10, 10 + 1:10, 20 + 1:10, 30 + 1:10)
df <- data.frame(x = x,
                 y = y,
                 z = gl(4, 10),
                 lower = x - 2,
                 upper = y + 2)


fig <- plot_ly(data = df,
               x = ~x,
               y = ~y,
               color = ~z,
               mode = "markers+lines",
               type = "scatter",
               opacity = 0.8) %>%
       add_ribbons(ymin = ~lower,
                   ymax = ~upper,
                   showlegend = FALSE,
                   opacity = 0.3) %>%
       layout(
    title = "Drop down menus - Styling",
    updatemenus = list(
      list(
        y = 0.8,
        buttons = list(

          list(method = "restyle",
               args = list("opacity", 0),
               label = "Hide ribbon"),

          list(method = "restyle",
               args = list("opacity", 1),
               label = "Show ribbon")))

    )
  )

the solution is:


x <- 1:40
y <- c(1:10, 10 + 1:10, 20 + 1:10, 30 + 1:10)
df <- data.frame(x = x,
                 y = y,
                 z = gl(4, 10),
                 lower = x - 2,
                 upper = y + 2)


fig <- plot_ly(data = df,
               x = ~x,
               y = ~y,
               color = ~z,
               mode = "markers+lines",
               type = "scatter",
               opacity = 0.8) %>%
       add_ribbons(ymin = ~lower,
                   ymax = ~upper,
                   showlegend = FALSE,
                   opacity = 0.3) %>%
       layout(
    title = "Drop down menus - Styling",
    updatemenus = list(
      list(
        y = 0.8,
        buttons = list(

          list(method = "restyle",
               args = list("visible", list(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)),
               label = "Show ribbon"),

          list(method = "restyle",
               args = list("visible", list(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE)),
               label = "Hide ribbon")))

    )
  )