I am trying to generate a plot which has independent dropdown menus for both Y and Y2 axes. I am able to implement a dropdown for either the Y1 or Y2 axis when the other is static. I have also generated a semi-solution making use of visible
repeatedly and manually defining all combinations of Y-Y2, but this solution is messy and not exactly what I am looking for (see below). The ideal output is a 2 Y axis plot that has one dropdown menu for each axis where we can select any combination of a set list of variables. I’m assuming that my lack of understanding related to args
is leaving me with no answers on this one.
The code that sort of works via visible
and toggling Y and Y2 manually defined combinations of variables.
library(plotly)
dat <- data.frame(x = runif(200), y = runif(200), z = runif(200), j = runif(200), k = rep(0.7, 200))
p <- plot_ly(dat, x = ~x) %>%
add_trace(y = ~y,
type = "scatter",
visible = TRUE,
name = "Y1-Y") %>%
add_trace(y = ~z,
visible = FALSE,
type = "scatter",
name = "Y1-Z") %>%
add_trace(y = ~ j,
visible = FALSE,
type = "scatter",
name = "Y1-J") %>%
# Y2 variables; removes Vibration
add_trace(y = ~k,
visible = TRUE,
type = "scatter",
mode = "markers",
yaxis = "y2",
name = "Y2-K") %>%
add_trace(y = ~z,
visible = FALSE,
type = "scatter",
yaxis = "y2",
name = "Y2-Z") %>%
add_trace(y = ~j,
visible = FALSE,
type = "scatter",
yaxis = "y2",
name = "Y2-J") %>%
layout(
xaxis = list(title = "X"),
yaxis = list(title = "Y"),
yaxis2 = list(overlaying = "y", side = "right", title = "Y2"),
updatemenus =
list(list(y = 0.7,
buttons = list(
list(
method = "restyle",
args = list(list("visible", list(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE))),
label = "Y-K"),
list(
method = "restyle",
args = list("visible", list(TRUE, FALSE, FALSE, FALSE, TRUE, FALSE)),
label = "Y-Z"),
list(
method = "restyle",
args = list("visible", list(TRUE, FALSE, FALSE, FALSE, FALSE, TRUE)),
label = "Y-J"),
list(
method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, TRUE, FALSE, FALSE)),
label = "J-K"),
list(
method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, FALSE, TRUE, FALSE)),
label = "J-Z"),
list(
method = "restyle",
args = list("visible", list(FALSE, TRUE, FALSE, TRUE, FALSE, FALSE)),
label = "Z-K")))
)
)
p
The below code is the closest I have gotten to 2 working buttons. This is obviously a flawed approach using visible
. Is there a way to directly call Y2 into args
, essentially “filtering” out Y1 variables?
p2 <- plot_ly(dat, x = ~x) %>%
add_trace(y = ~y,
type = "scatter",
visible = TRUE,
name = "Y1-Y") %>%
add_trace(y = ~z,
visible = FALSE,
type = "scatter",
name = "Y1-Z") %>%
add_trace(y = ~ j,
visible = FALSE,
type = "scatter",
name = "Y1-J") %>%
add_trace(y = ~k,
visible = TRUE,
type = "scatter",
mode = "markers",
name = "Y1-K") %>%
add_trace(y = ~y,
visible = TRUE,
type = "scatter",
mode = "markers",
yaxis = "y2",
name = "Y2-Y") %>%
# 2nd y axis
add_trace(y = ~z,
visible = FALSE,
type = "scatter",
yaxis = "y2",
name = "Y2-Z") %>%
add_trace(y = ~j,
visible = FALSE,
type = "scatter",
yaxis = "y2",
name = "Y2-J") %>%
add_trace(y = ~k,
visible = TRUE,
type = "scatter",
mode = "markers",
yaxis = "y2",
name = "Y1-K") %>%
layout(xaxis = list(domain = c(0.1, 1)),
yaxis = list(title = "y"),
yaxis2 = list(overlaying = "y", side = "right", title = "y2"),
updatemenus =
list(list(y = 0.7,
buttons = list(
list(
method = "restyle",
args = list("visible", list(TRUE, FALSE, FALSE, FALSE)),
label = "Y"),
list(
method = "restyle",
args = list("visible", list(FALSE, TRUE, FALSE, FALSE)),
label = "Z"),
list(
method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, FALSE)),
label = "J"),
list(
method = "restyle",
args = list("visible", list(FALSE, FALSE, FALSE, TRUE)),
label = "K")
)
),
# second list to add 2nd button on yaxis2
# this is half working
list(y = 0.7,
x = 1.1,
buttons = list(
list(
method = "restyle",
args = list("visible", list(TRUE, FALSE, FALSE, FALSE)),
label = "Y"),
list(
method = "restyle",
args = list("visible", list(FALSE, TRUE, FALSE, FALSE)),
label = "Z"),
list(
method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, FALSE)),
label = "J"),
list(
method = "restyle",
args = list("visible", list(FALSE, FALSE, FALSE, TRUE)),
label = "K")
)
)
)
)
p2
On another note, is there anywhere that documents what exactly args
is? I am having a hard time finding what can be put into args
, making it extremely difficult to troubleshoot where things have not been documented on SO.