[英]Plotly is blending palette colors in R
When I'm trying to define a custom palette, say of blue and red colors, I get magenta instead of blue. 当我尝试定义自定义调色板(例如蓝色和红色)时,我得到的是洋红色而不是蓝色。 Let's consider an example taken from plotly documentation:
让我们考虑一个从可打印文档中获取的示例:
library(plotly)
pal <- c("red", "blue", "green")
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length,
color = ~Species, colors = pal)
p
This works as expected. 这按预期工作。 Each of three species get it's own color: red, blue or green.
三种物种各自具有自己的颜色:红色,蓝色或绿色。
Then I prepare a new data set, removing "virginica", and plotting the result: 然后,我准备一个新的数据集,删除“ 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
Now "setosa" is red and "versicolor" is blue. 现在“ setosa”是红色,“ versicolor”是蓝色。 We don't use green color, so it seems reasonable to remove it:
我们不使用绿色,因此删除它似乎是合理的:
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
The result, obviously, should be the same, as in previous chunk of code, but in fact it isn't : the blue color is replaced by magenta, and that is very strange. 显然,结果应该与前面的代码块相同,但实际上并非如此 :蓝色被洋红色代替,这很奇怪。
I think this is probably just because you have a 2 colour palette mapped to a 3 level factor - until you drop the unused level from the Species
factor, it's still considered an attribute of the Species
variable and will affect how it is plotted. 我认为这可能只是因为您有2个调色板映射到3个水平因子-直到从
Species
因子中删除未使用的水平,它仍被视为Species
变量的属性,并将影响其绘制方式。
Dropping the factor level makes the colours behave as you expected: 降低因子水平会使颜色表现出预期的效果:
iris_new<- filter(iris, Species == "setosa" | Species == "versicolor") %>%
mutate(Species = droplevels(Species))
p <- plot_ly(data = iris_new, x = ~Sepal.Length, y = ~Petal.Length,
color = ~Species, colors = pal)
p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.