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’