如何在 R plotly 图中为每个轨迹定义单独的调色板?

How to define separate color palettes per trace in R plotly plots?

我想定义两个颜色向量并将它们应用到两个绘图轨迹上。

col2 <- c("#b72b7f", "#2ba5b7", "#3e8236", "#f87b60", "#000000")
col1 <- c("#e65353", "#62e653", "#53a4e6", "#53e6da", "#e6b053")

iris %>%
  plot_ly() %>%
  add_trace(x = ~Sepal.Length, y = 0.3, color = ~Species, colors = col1) %>%
  add_trace(x = ~Sepal.Width, y = 0.6, color = ~Species, colors = col2)

然而,似乎 plotly 仅使用 col1 中的颜色来为两条轨迹着色。 我该怎么做才能达到预期的效果?

由于两条轨迹共享相同的图例,看起来有必要将两个不同的组设为两个颜色组:

library(dplyr)
library(plotly)

col1 <- c("#b72b7f", "#2ba5b7", "#3e8236", "#f87b60", "#000000")
col2 <- c("#e65353", "#62e653", "#53a4e6", "#53e6da", "#e6b053")

col1 <- col1[1:length(unique(iris$Species))]
col2 <- col2[1:length(unique(iris$Species))]

col <- c(col1,col2)

iris %>%
  plot_ly() %>%
  add_trace(x = ~Sepal.Length, y = 0.3, color = ~paste("1 -", Species), colors = col) %>%
  add_trace(x = ~Sepal.Width, y = 0.6, color = ~paste("2 -", Species), colors = col)