[英]Define palette color order in R Plotly
我正在嘗試使用函數創建一些Plotly圖。 我為他們創建了一個調色板。 我使用的數據集具有不同數量的分組因子,我希望它們按我指定的順序使用顏色,但它似乎根據有多少因素選擇不同的順序。 有沒有一種簡單的方法來解決這個問題? 我需要創建一個功能嗎? 謝謝!
問題的一個直觀例子
library(plotly)
#Some example data
x <- 1:10
g <- c("A", "B", "C", "D", "E")
g2 <- c("A", "B")
g3 <- c("A", "B", "C")
y <- 1:20
df <- as.data.frame(cbind(x,g,y))
df2 <- as.data.frame(cbind(x,g2,y))
df3 <- as.data.frame(cbind(x,g3,y))
#Color palette
pal <- c('blue','yellow','green', 'pink', 'black')
#In this plot the order is as in the pal object, this is what I want.
plot1 <- plot_ly(data=df, x=~x, y=~y,
type = "bar", color=~g, colors =pal)
plot1
#In this plot uses the first and the fifth in the pal object
# and I would like to be the first and second colors.
plot2 <- plot_ly(data=df2, x=~x, y=~y,
type = "bar", color=~g2, colors =pal)
plot2
#In this one uses the first, the third and the fifth in that order
# and I would like to be the first, second and third colors.
plot3 <- plot_ly(data=df3, x=~x, y=~y,
type = "bar", color=~g3, colors =pal)
plot3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.