如何link 绘制图例和颜色选择的痕迹?

How to link plotly traces for legend and colour selection?

问题

我正在将一些 ggplot/ggvis 绘图迁移到 shiny 应用程序中的 plotly。我遇到了一个关于跟踪链接的问题。我希望能够通过 group 在相关数据帧之间共享的图例上 show/hide 跟踪。

最小工作示例

# load libraries
library(dplyr)
library(plotly)
library(viridis)

# contrived data to represent actual data points
df1 <- data.frame(x = rnorm(100),
                  y = rnorm(100),
                  group = rep(c("G1", "G2", "G3", "G4"), 25))

# contrived data to represent theoretical relationship
df2 <- data.frame(x = c(rep(-2, 4), rep(2, 4)),
                  y = c(seq(1.9, 1, -0.3), seq(-1, -1.9, -0.3)),
                  group = rep(c("G1", "G2", "G3", "G4"), 2))

# create plot with scatter and line traces
df1 %>%
  plot_ly(x = x,
          y = y,
          color = group,
          colors = viridis(n_distinct(group)),
          mode = "markers") %>%
  add_trace(x = x,
            y = y,
            color = group,
            colors = viridis(n_distinct(group)),
            mode = "lines",
            data = df2)

到目前为止的尝试

我的在线搜索,尤其是阅读 plotly 文档并没有让我走得太远。

我可以将 showlegend = FALSE 添加到第二条轨迹。这确实在一定程度上解决了挑战,但是,我仍然希望 show/hide 根据 group 值进行跟踪。

可能的解决方案

基于 plotly 的体系结构,似乎如果我可以将散点图和线放在每个 group 的一个轨迹上,那么我将获得所需的行为。但是,似乎一条跟踪可能只有一个 "mode",这就是为什么我采用了我所采用的方法。

如果我继续我已经开始的路径,我想我需要以某种方式捕获图例的 "on click" 事件和 show/hide group 痕迹...但是我不太确定从哪里开始。

相关/次要

在我的 MWE 中,我已将 colors 参数设置为 viridis。虽然这对问题不重要,但我还没有找到一种方法来确保颜色选择链接到 group(即,如果 df1 上 group 的轨迹是蓝色,我想制作在 df2 的跟踪上相同 group 蓝色。如果这是非常重要的并且需要第二个问题(我搜索并发现没有匹配......可能是因为它是微不足道的而且我遗漏了一些简单的东西),那么我这部分会单独问

为后人重温历史

自首次提出此问题以来,ggplot2plotly 发生了一些变化。在当前版本(4.7.1)中有一个参数legendgroup来解决这个问题。

懒代码

请原谅在优雅编码上付出的最少努力,但是,MWE 的这个扩展展示了 show/hide 按组跟踪的能力。

df1_G1 <- df1 %>% filter(group == "G1")
df2_G1 <- df2 %>% filter(group == "G1")
df1_G2 <- df1 %>% filter(group == "G2")
df2_G2 <- df2 %>% filter(group == "G2")
df1_G3 <- df1 %>% filter(group == "G3")
df2_G3 <- df2 %>% filter(group == "G3")
df1_G4 <- df1 %>% filter(group == "G4")
df2_G4 <- df2 %>% filter(group == "G4")

plot_ly(type = "scatter", mode = "markers") %>%
  add_trace(df1_G1, x = df1_G1$x, y = df1_G1$y, color = I("red"),
            legendgroup = "G1", name = "G1 - scatter") %>%
  add_trace(df2_G1, x = df2_G1$x, y = df2_G1$y, color = I("red"),
            legendgroup = "G1", name = "G1 - line", mode = "lines") %>%
  add_trace(df1_G2, x = df1_G2$x, y = df1_G2$y, color = I("green"), 
            legendgroup = "G2", name = "G2 - scatter") %>%
  add_trace(df2_G2, x = df2_G2$x, y = df2_G2$y, color = I("green"),
            legendgroup = "G2", name = "G2 - line", mode = "lines") %>%
  add_trace(df1_G3, x = df1_G3$x, y = df1_G3$y, color = I("blue"),
            legendgroup = "G3", name = "G3 - scatter") %>%
  add_trace(df2_G3, x = df2_G3$x, y = df2_G3$y, color = I("blue"),
            legendgroup = "G3", name = "G3 - line", mode = "lines") %>%
  add_trace(df1_G4, x = df1_G4$x, y = df1_G4$y, color = I("orange"), 
            legendgroup = "G4", name = "G4 - scatter") %>%
  add_trace(df2_G4, x = df2_G4$x, y = df2_G4$y, color = I("orange"), 
            legendgroup = "G4", name = "G4 - line", mode = "lines")

示例输出

显示第一组 G1 已取消选择。另请注意,已使颜色与同一组的散点图和线条图相匹配。