gpt4 book ai didi

r - 如何链接图例和颜色选择的 plotly 轨迹?

转载 作者:行者123 更新时间:2023-12-01 02:01:01 29 4
gpt4 key购买 nike

问题

我正在迁移多个 ggplot/ggvis plotly 到 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)

迄今为止的尝试

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

我可以加 showlegend = FALSE到第二条轨迹。这确实解决了挑战,但是,我仍然想根据 group 显示/隐藏该跟踪。值(value)。

可能的解决方案

基于 plotly的架构,似乎如果我可以将散点和线放在每个 group 的一条轨迹上然后我会得到所需的行为。但是,似乎跟踪可能只有一个“模式”,这就是我采用我所拥有的方法的原因。

如果我继续沿着我开始的道路前进,我想我需要以某种方式为图例捕获“点击”事件并显示/隐藏 group痕迹......但我不确定从哪里开始。

相关/次要

在我的 MWE 中,我设置了 colors论据 viridis .虽然这对问题并不重要,但我还没有找到一种方法来确保颜色选择与 group 相关联。 (即,如果 df1 上 group 的跟踪是蓝色的,我想在 df2 的跟踪上设置相同的 group 蓝色。如果这不是微不足道的,并且需要提出第二个问题(我搜索并没有找到匹配项.. . 可能是因为它是微不足道的,我错过了一些简单的东西),然后我会单独问这部分。

最佳答案

为后人重温历史
ggplot2的几处变化和 plotly自从第一次问这个问题以来的中间时间。在当前版本 (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

关于r - 如何链接图例和颜色选择的 plotly 轨迹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36415819/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com