如何用Plotly R实现Y轴与Y2轴独立下拉菜单控制双Y轴图
双Y轴独立下拉菜单的Plotly实现问题
我需要生成一个双Y轴图表,其中Y轴与Y2轴各配备独立的下拉菜单,用于选择对应轴的变量。目前已实现两种方案:
- 单个轴带下拉菜单,另一轴固定变量
- 重复使用
visible参数,手动定义所有Y-Y2变量组合来切换显示
但第二种方案过于繁琐,不符合预期的灵活组合需求。理想效果是两个下拉菜单完全独立,可任意选择Y轴和Y2轴的变量组合。我认为核心问题是对args参数的理解不足,导致无法实现该功能。
手动定义变量组合的可用代码
library(plotly) set.seed(123) 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") %>% 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
尝试双下拉菜单的代码(存在缺陷)
我尝试实现两个独立下拉按钮,但用visible的方法明显有问题。是否可以在args中直接指定Y2轴,只控制该轴的变量,而不影响Y轴的显示?
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
补充问题
有没有详细说明args参数的文档?我很难找到args可接受的具体内容,导致排查问题非常困难。
内容的提问来源于stack exchange,提问作者mchen
相关产品推荐
相关产品推荐

