当我试图定义自定义调色板时,比如说蓝色和红色,我得到的是洋红色而不是蓝色。让我们来看一个来自
情节
library(plotly)
pal <- c("red", "blue", "green")
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length,
color = ~Species, colors = pal)
p
这正如预期的那样有效。三个物种中的每一个都有自己的颜色:红色、蓝色或绿色。
然后,我准备一个新的数据集,删除“virginica”,并绘制结果:
library(dplyr)
pal <- c("red", "blue", "green")
iris_new<- filter(iris, Species == "setosa" | Species == "versicolor")
p <- plot_ly(data = iris_new, x = ~Sepal.Length, y = ~Petal.Length,
color = ~Species, colors = pal)
p
现在“setosa”是红色,“versicolor”是蓝色。我们不使用绿色,因此删除它似乎是合理的:
pal <- c("red", "blue")
iris_new<- filter(iris, Species == "setosa" | Species == "versicolor")
p <- plot_ly(data = iris_new, x = ~Sepal.Length, y = ~Petal.Length,
color = ~Species, colors = pal)
p
它不是
:蓝色被洋红色取代,这很奇怪。