绘制多个更新菜单

Plotly multiple updatemenus

post 的基础上,我试图在 R plotly 中创建两个 updatemenus,这将允许 select 两个的所有可能组合因素。

这是我目前所做的:

library(plotly)

X <- data.frame(x = 1:6,
                y = 1:6,
                z = 1:6,
                gender = rep(c("M", "F"), each = 3),
                eyes = rep(c("B", "G"), 3))

gg <- ggplot(data = X, aes(x = x, y = y)) + 
  geom_point(aes(color = z, alpha = interaction(gender, eyes))) +
  scale_alpha_manual(values = rep(1, 4))


ggplotly(gg) %>% 
  layout(
    updatemenus = list(
      list(y = 1,
           buttons = list(
             list(method = "restyle",
                  args = list(list(visible = c(TRUE, TRUE, TRUE, TRUE)), 0:3),
                  label = "F&M"),
             list(method = "restyle",
                  args = list(list(visible = c(TRUE, TRUE)), c(0, 2)),
                  label = "F"),
             list(method = "restyle",
                  args = list(list(visible = c(TRUE, TRUE)), c(1, 3)),
                  label = "M"))),
      list(y = .8,
           buttons = list(
             list(method = "restyle",
                  args = list(list(visible = c(TRUE, TRUE, TRUE, TRUE)), 0:3),
                  label = "B&G"),
             list(method = "restyle",
                  args = list(list(visible = c(TRUE, TRUE)), 0:1),
                  label = "B"),
             list(method = "restyle",
                  args = list(list(visible = c(TRUE, TRUE)), 2:3),
                  label = "G")))
    )
  )
)

数据:

#  x y z gender eyes
#  1 1 1      M    B
#  2 2 2      M    G
#  3 3 3      M    B
#  4 4 4      F    G
#  5 5 5      F    B
#  6 6 6      F    G

这是输出,根本不起作用:


编辑
例如,如果 FB 被 selected,我希望只显示一个数据点,即 (5, 5).

当您创建此 plotly 对象时,plotly 将其分为五个轨迹。因此,当您想要使用可见性时,您需要处理这些痕迹中的每一个。如果您有任何问题,请告诉我!

我用 gg1 找到了五条痕迹以及每条痕迹中的内容。您可以查看源代码窗格中的 gg1plotly_json().

通常(并非总是如此!),您会在 gg1$x$data 处找到痕迹,其中 gg1plotly 对象。

有五个踪迹。对于每个 args,每个轨迹都需要一个 T 或 F 才能可见。

  • 第一条trace只包含(5, 5),即F B
  • 第二条轨迹包含 (1, 1) 和 (3, 3);都是M B
  • 第三条轨迹包含(4, 4) 和(6, 6);都是F G
  • 第四条轨迹包含(2, 2);那是M G
  • 第五条轨迹包含所有轨迹使用的颜色渐变

代码如下:

gg1 = plotly_build(gg) 

ggplotly(gg) %>% 
  layout(
    updatemenus = list(

      list(y = 1,
           buttons = list(
             list(method = "restyle",
                  args = list("visible", as.list(unlist(rep(T, 5)))),
                  label = "F&M"),
             list(method = "restyle",
                  args = list("visible", list(T, F, T, F, T)),
                  label = "F"),
             list(method = "restyle",
                  args = list("visible", list(F, T, F, T, T)),
                  label = "M")
             ) # end button
           ),  # end first button list
      list(y = .8,
           buttons = list(
             list(method = "restyle",
                  args = list("visible", as.list(unlist(rep(T, 5)))),
                  label = "B&G"),
             list(method = "restyle",
                  args = list("visible", list(T, T, F, F, T)),
                  label = "B"),
             list(method = "restyle",
                  args = list("visible", list(F, F, T, T, T)),
                  label = "G")
             ) # end button
           ) # end second menu list
    ) # end updatemenus
  ) # end layout


更新

根据我们的评论,我认为目前无法使用相互交互的 updatemenus 样式按钮。