I have been trying to generate a widget that allows me to switch between several times series, each of which has 3 distinct lines (black, blue, red), like so
I have been following this solution which lets me create all the time series I need. However, when I switch the series, it collapses the three lines into a single series. Below is a reproducible example:
library(plotly)
library(tidyr)
create_buttons <- function(df, y_axis_var_names) {
lapply(
y_axis_var_names,
FUN = function(var_name, df) {
button <- list(
method = 'restyle',
args = list('y', list(df[, var_name])),
label = sprintf('Show %s', var_name)
)
},
df
)
}
df1 <- data.frame(x = 1:200, y = runif(200), z = runif(200), j = runif(200), k = rep(0.7, 200), i = rnorm(200,0.6,0.05))
df2 <- data.frame(x = 1:200, y = runif(200), z = runif(200), j = runif(200), k = rep(0.7, 200), i = rnorm(200,0.6,0.05))
y_axis_var_names <- c('v', 'value')
df_lng1 <- gather(df1, key = 'key', value = 'value','y':'i')
df_lng2 <- gather(df2, key = 'key', value = 'value','y':'i')
df_lng <- cbind(df_lng1, df_lng2)
plt <- plot_ly() %>% add_lines(data = df_lng, x = ~x, y = ~v, color = ~key) %>%
layout(
title = "Drop down menus - Styling",
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
buttons = create_buttons(df_lng, y_axis_var_names)
)
))
plt
When using the dropdown menu of the resulting widget, the series collapse into the one for the yvalue.
How do I stop the collapse, while still being able to plot multiple lines in a single plot
Update
I found this thread, where the same problem occurs. It seems to be the definition of the args argument in the create_buttons function that needs to be altered to include logical vectors of the values that are to be shown. I haven't figured out how to do this yet.