0

Here is the code for the example's data:

library(tidyverse)
library(plotly)

df <- data_frame(
  date = Sys.Date() - seq(0, 99, by = 0.2)[2:101],
  cut = rep(c("Ideal", "Premium", "Very Good", "Good", "Fair"), 20),
  y = sample_n(diamonds, size = 100)$depth,
  z = sample_n(diamonds, size = 100)$price
)

Here is the code for the plot:

plot_ly(df, x = ~date, y = ~y, color = ~cut, 
        type = "scatter", mode = "lines") %>%
layout(
  title = "Drop down menus - Change y",
  xaxis = list(title = "x"),
  yaxis = list(title = "y"),
  updatemenus = list(
    list(
      y = 0.7,
      buttons = list(
        list(method = "restyle",
             args = list("y", list(~y)),
             label = "Show Y"),
        list(method = "restyle",
             args = list("y", list(~z)),
             label = "Show Z")))
))

What happens is that I execute the code, the plot is rendered, and it looks the way I want it to: counts of things for categorical variables through time. The goal is to have the drop down swap out one y variable for another, in this case z.

Based on some of the toy examples in the documentation and a couple of answers here on SO I feel that this should work. Instead it looks like all the values collapse strangely into one line. There doesn't seem to be a way to return the plot to its original state without rerunning the code.

Anyone have an idea how to get this to work correctly?

Here's my plotly version:

packageVersion("plotly")
[1] ‘4.5.6.9000’

1 Answers1

2

You could get it to work by

  • first creating an "empty" plot: plot_ly(df, x = ~date, color = ~cut)
  • add_trace for each y value: add_trace(y = ~z, type = 'scatter', mode = 'lines', visible=F) but hiding the 2nd trace
  • apply restyle to hide/show each set: args = list("visible", append(rep(list(TRUE), 5), rep(list(FALSE), 5)))

Did work for me with the plotly 4.5.6 on R-Studio on Ubuntu 16 and Windows 10.

library('tidyverse')
library('plotly')

df <- data_frame(
  date = Sys.Date() - seq(0, 99, by = 0.2)[2:101],
  cut = rep(c("Ideal", "Premium", "Very Good", "Good", "Fair"), 20),
  y = sample_n(diamonds, size = 100)$depth,
  z = sample_n(diamonds, size = 100)$price
)
p <- plot_ly(df, x = ~date, color = ~cut) %>%
add_trace(y = ~y, type = 'scatter', mode = 'lines', visible=T) %>%
add_trace(y = ~z, type = 'scatter', mode = 'lines', visible=F)

p <- p %>% layout(
  updatemenus = list(
    list(
      buttons = list(
        list(method = "restyle",
             args = list("visible", append(rep(list(TRUE), 5), rep(list(FALSE), 5))),
             label = "Show Y"),
        list(method = "restyle",
             args = list("visible", append(rep(list(FALSE), 5), rep(list(TRUE), 5))),
             label = "Show Z")))
))
Maximilian Peters
  • 27,890
  • 12
  • 74
  • 90
  • Thanks @Maximilian-Peters for the helpful solution! I tried doing it this way as well, but I did not use `add_trace()` I used `add_lines()`. That may not matter though--it looks like the magic happens with the `append(rep(list(T), 5), rep(list(F), 5))`--how is this working? Why would the `args` variable need the value 5 times? Is it because `cut` has five unique categorical values? Again, thanks! – Robert Mitchell Feb 08 '17 at 02:05
  • @RobertMitchell: beats me, too. In `add_trace` giving the `visible` argument once is sufficient, but later it needs repetition. Probably because of the `cut` the trace gets split afterwards and inherits the `visible` attribute from the `add_trace` call. – Maximilian Peters Feb 08 '17 at 16:49