如何链接图例迹线以进行图例和颜色选择?

凯文·阿森诺

问题

我移植了一些ggplot/ggvis阴谋plotlyshiny应用。关于跟踪的链接,我遇到了一个问题。我希望能够通过group图例显示/隐藏轨迹,图例在相关数据帧之间共享。

最小的工作实例

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

到目前为止的尝试

我的在线搜索,尤其是阅读密谋的文档并没有带我走远。

我可以添加showlegend = FALSE到第二条痕迹。这确实可以解决挑战,但是,我仍然要根据group显示/隐藏该跟踪

可能的解决方案

基于的体系结构plotly,似乎如果我可以将散布图和线条每条放置到一条迹线上,group那么我将获得所需的行为。但是,跟踪似乎只能有一个“模式”,这就是为什么我采用了我的方法的原因。

如果我继续前进,我想我应该以某种方式捕获图例的“单击时”事件并显示/隐藏group痕迹...但是我不确定如何从此开始。

相关/次要

在我的MWE中,我将colors参数设置viridis虽然这对问题不重要,但我还没有找到一种方法来确保将颜色选择链接到group(例如,如果groupdf1上的迹线是蓝色,我想使groupdf2上的迹线成为相同的蓝色。)是不平凡的,需要提出第二个问题(我搜索并没有找到匹配的内容……可能是因为它不重要,我缺少了一些简单的东西),然后我将分别询问这部分。

凯文·阿森诺

回顾后代的历史

自从首次提出此问题以来ggplot2plotly在此期间以及随后的时间里发生了一些变化在当前版本(4.7.1)中,有一个参数legendgroup可以解决问题。

懒码

请原谅在优雅编码上的最小工作量,但是,对MWE的扩展展示了按组显示/隐藏迹线的能力。

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取消选择。另请注意,已对颜色进行了设置,以使同一组的散布线迹与行迹线匹配。

grouplegend_example

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章