简体   繁体   English

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

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

I want to define two vectors of colors and apply them to two plotly traces.我想定义两个 colors 向量并将它们应用于两个 plotly 轨迹。

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)

However, it seems that plotly use only the colors in col1 to color both of the traces.但是,似乎 plotly 仅使用col1中的 colors 来为两条迹线着色。 What can I do to achieve the desired result?我该怎么做才能达到预期的效果?

显示两行点的图像。它们都使用相同的颜色组。

As both traces share the same legend, looks like it will be necessary to make two distinct groups to have two colors groups:由于两条迹线共享相同的图例,看起来有必要创建两个不同的组以拥有两个 colors 组:

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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM