绘制多个updatemenus

发布于 2025-01-20 12:18:14 字数 2151 浏览 5 评论 0原文

这篇帖子的基础上,我尝试创建两个updatemenus< R plotly 中的 /code> 允许选择两个因素的每种可能的组合。

这是我到目前为止所做的:

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,我希望仅显示一个数据点,即 (5, 5)

Building on this post, I am trying to create two updatemenus in R plotly that would allow to select every possible combination of two factors.

This is what I have done so far:

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")))
    )
  )
)

The data:

#  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

This is the output, which does not work at all:
enter image description here


Edit
For instance, if F and B are selected, I would expect only a single data point to be displayed, namely (5, 5).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清音悠歌 2025-01-27 12:18:14

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

我使用 gg1 找到了 5 条痕迹以及每条痕迹中的内容。您可以在源代码窗格或 plotly_json() 中查看gg1

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

有五处痕迹。对于每个args,每个跟踪都需要 T 或 F 来实现可见性。

  • 第一条迹线仅包含 (5, 5),即 FB
  • 第二条迹线包含 (1, 1) 和 (3, 3);这些都是 MB
  • 第三条迹线包含 (4, 4) 和 (6, 6);这些都是 FG
  • 第四条迹线包含 (2, 2);这就是 MG
  • 第五条迹线包含所有迹线使用的颜色渐变

这是代码:

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 样式按钮。

When you created this plotly object, plotly divided it into five traces. So when you wanted to use visibility, you needed to address each of these traces. If you have any questions, let me know!

I used gg1 to find five traces and what was in each of these traces. You could look at gg1 in the source pane or plotly_json().

Typically (not always!), you'll find the traces at gg1$x$data where gg1 is the plotly object.

There are five traces. For each args, each trace needs a T or F for visibility.

  • The first trace only contains (5, 5), that's F B
  • The second trace contains (1, 1) and (3, 3); those are both M B
  • The third trace contains (4, 4) and (6, 6); those are both F G
  • The fourth trace contains (2, 2); that's M G
  • The fifth trace contains the color gradient that all traces use

Here's the code:

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


Update

From our comments, I do not believe that it's possible at this time to use updatemenus style buttons that interact with each other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文