[英]Plotly is blending palette colors in R
當我嘗試定義自定義調色板(例如藍色和紅色)時,我得到的是洋紅色而不是藍色。 讓我們考慮一個從可打印文檔中獲取的示例:
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
顯然,結果應該與前面的代碼塊相同,但實際上並非如此 :藍色被洋紅色代替,這很奇怪。
我認為這可能只是因為您有2個調色板映射到3個水平因子-直到從Species
因子中刪除未使用的水平,它仍被視為Species
變量的屬性,並將影響其繪制方式。
降低因子水平會使顏色表現出預期的效果:
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.