我正在针对一个值(2x y轴,一个x轴)绘制两个值,但想要为它们分别上色,每个颜色使用一组不同的十六进制颜色。我尝试了许多设置,但是该图始终只显示第一组颜色。可复制的示例:
mtcars %>% mutate(gear = as.factor(gear)) %>%
plot_ly(x = ~ cyl, color = ~ gear) %>%
add_trace(y = ~ wt,
type = "bar",
colors = c("yellow", "red", "purple")) %>%
add_trace(y = ~ qsec,
type = "scatter",
mode = "lines + markers",
yaxis = "y2",
colors = c("blue", "green", "black")) %>%
layout(yaxis = list(side = "left"),
yaxis2 = list(overlaying = "y", side = "right"))
请注意,两条迹线都取决于同一变量(齿轮)的颜色,但是我想使用不同的颜色(WT变量为“暖色”,QSEC变量为“冷色”)。现在,两条迹线都以“暖色”上色。