Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Plotly正在R中混合调色板颜色_R_Graph_Plotly_Palette - Fatal编程技术网

Plotly正在R中混合调色板颜色

Plotly正在R中混合调色板颜色,r,graph,plotly,palette,R,Graph,Plotly,Palette,当我试图定义一个自定义调色板时,比如说蓝色和红色,我得到的是洋红色而不是蓝色。让我们考虑一个从 > >强>文档中的例子: library(plotly) pal <- c("red", "blue", "green") p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species, colors = pal) p library(plotly) pal我认为这可能只是因

当我试图定义一个自定义调色板时,比如说蓝色和红色,我得到的是洋红色而不是蓝色。让我们考虑一个从<强> > >强>文档中的例子:

library(plotly)
pal <- c("red", "blue", "green")
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, 
      color = ~Species, colors = pal)
p
library(plotly)

pal我认为这可能只是因为你有一个2色调色板映射到一个3级因子-直到你从
物种
因子中删除未使用的级别,它仍然被认为是
物种
变量的一个属性,并将影响它的绘制方式

降低“因子”级别将使颜色的行为符合您的预期:

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
iris\u新%
突变(物种=液滴(物种))

p它起作用了!非常感谢你!我认为,你是对的:它试图将这两种颜色延伸到三个层次。有趣!当你只有一个因素水平时,会发生同样的事情吗?
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
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