1
當我試圖定義一個定製的調色板,說的藍色和紅色兩種顏色,我讓紅色而不是藍色。讓我們考慮從plotly文檔獲取一個例子:Plotly的混合顏色的調色板中的R
library(plotly)
pal <- c("red", "blue", "green")
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length,
color = ~Species, colors = pal)
p
可正常工作。三種物種都有自己的顏色:紅色,藍色或綠色。
然後,我準備一個新的數據集,刪除「錦葵」,並繪製出結果:
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」爲紅色,「花斑癬」是藍色的。我們不使用綠色的顏色,所以我們有理由將其刪除:
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
結果,很明顯,應該是相同的,如以前的代碼塊,但實際上它不是:藍色被洋紅色代替,這很奇怪。
它的工作原理!非常感謝!我想,你是對的:它試圖此兩種顏色延伸到三級的因素。 –
有趣!當你只有一個因素水平時,會發生同樣的事情嗎? – patrick