如何链接图例迹线以进行图例和颜色选择? [英] How to link plotly traces for legend and colour selection?

查看:133
本文介绍了如何链接图例迹线以进行图例和颜色选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在shiny应用程序中将许多ggplot/ggvis地块迁移到plotly.关于跟踪的链接,我遇到了一个问题.我希望能够通过图例上的group显示/隐藏轨迹,该轨迹在相关数据帧之间共享.

I am migrating a number of ggplot/ggvis plots to plotly in a shiny application. There is an issue I've encountered regarding the linking of traces. I want to be able to show/hide traces by group on the legend, which is shared between related data frames.

# 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 文档,并没有使我走得很远.

Attempts so far

My online searches and especially reading the plotly documentation have not taken me far.

我可以将showlegend = FALSE添加到第二条迹线.这确实可以解决挑战,但是,我仍然想基于group值显示/隐藏该跟踪.

I can add showlegend = FALSE to the second trace. That does go part way to addressing the challenge, however, I still want to show/hide that trace based on the group value.

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

Based on the architecture of plotly, it seems that if I could put the scatter and line onto one trace per group then I would get the desired behaviour. However, it seems that a trace may only have a single "mode", which is why I've taken the approach that I have.

如果我沿着自己开始的道路继续前进,我想我需要以某种方式捕获图例的单击时"事件并显示/隐藏group痕迹...但是我不确定如何确定首先.

If I continue down the path I've started, I think I need to somehow trap the "on click" event for the legend and show/hide the group traces... but I'm not really sure where to begin with that.

在我的MWE中,我已将colors参数设置为viridis.虽然这对问题不重要,但我还没有找到一种方法来确保将颜色选择链接到group(即,如果df1上group的迹线是蓝色的,我想使用相同的group df2轨迹上的蓝色.如果这很重要,并提出了第二个问题(我搜索并没有找到匹配的结果……可能是因为它很琐碎,我缺少了一些简单的东西),那么我将分别询问这部分

In my MWE, I have set the colors argument to viridis. While that is unimportant to the problem, I have not found a way to ensure the colour selection is instead linked to the group (i.e. if the trace for group on df1 is blue, I want to make the same group blue on the trace for df2. If this is non-trivial and warrants a second question (I searched and found no match... possibly because it is trivial and I'm missing something simple), then I'll ask this part separately.

推荐答案

重访后代的历史记录

自从首次提出此问题以来,在这段时间内对ggplot2plotly进行了多次更改.在当前版本(4.7.1)中,有一个参数legendgroup可以解决问题.

Revisiting history for posterity

Several changes to ggplot2 and plotly in the intervening time since this question was first asked. In the current version (4.7.1) there is an argument legendgroup that attends to solving the problem.

请原谅我们花最少的精力在优雅的编码上,但是,对MWE的扩展证明了可以按组显示/隐藏迹线的能力.

Please forgive the minimum effort placed on elegant coding, however, this extension to the MWE demonstrates the ability to show/hide traces by group.

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.另外请注意,颜色已进行设置,以使同一组的散布线迹与行迹线匹配.

Sample output

Showing the first group G1 deselected. Also note that the colours have been made to match between the scatter and line traces for the same group.

这篇关于如何链接图例迹线以进行图例和颜色选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆