簡體   English   中英

Plotly:如何自定義堆疊條形圖中的顏色?

[英]Plotly: How to customize colors in a stacked bar chart?

我想問你是否可以幫助我在 plotly 創建的堆疊條形圖中自定義顏色。

問題如下 - 我必須重新創建一個儀表板(從 excel 文件到 html 文件)。 儀表板的一部分是一個圖表,為我們提供有關每個實體的早期生產的信息。 該圖表是 plotly 的堆疊條形圖類型。 由於整個儀表板中每個實體都由特定顏色(以 RGB 定義)定義,因此我也需要將這些顏色保留在圓環圖中。 但有一個問題。 我總是收到以下警告:

警告消息:在 RColorBrewer::brewer.pal(N, "Set2") 中:n 太大,調色板 Set2 允許的最大值為 8 返回您要求的具有這么多顏色的調色板

生成的圓環圖僅包含一個未指定顏色的實體。 此外,圖例中的顏色不是定義的顏色。

知道如何處理它嗎? 非常感謝你提前。

代碼:

library(dplyr)
library(plotly)

dt <- as.data.frame(matrix(ncol = 13, nrow = 19))
colnames(dt) <- c("Entity", month.abb)

for (i in 1:nrow(dt)) {
  dt[i, 1] <- paste("Entity", i, sep="")
  dt[i, -1] <- floor(runif(12, min=0, max=100))
}

# assign colors to entities

dt$"EntityColor" <- c("#074263", "#0B5394", "#3D85C6", "#6D9EEB", "#A4C2F4", "#CFE2F3", "#5B0F00", "#85200C", "#A61C00", "#CC4125", "#DD7E6B", "#E6B8AF", "#F8CBAD", "#F4CCCC", "#274E13", "#38761D", "#E06666", "#CC0000", "#20124D")

data.table::melt(dt) %>%

  plot_ly(x = ~variable,
          y = ~value,
          type = "bar",
          color = ~Entity,
          marker = list(colors = ~EntityColor)
  ) %>%

  layout(yaxis = list(title = ""),
         xaxis = list(title = ""),
         barmode = 'stack')

陰謀:

在此處輸入圖像描述

評論后的改進方法:

由於顏色有點棘手(請參閱下面的初步建議)我不得不將整個事情分解並在循環中使用plot_ly()add_traces()的組合以確保 plotly 設置不應用顏色以錯誤的順序。 下面的情節應該正是你要找的。

陰謀:

在此處輸入圖像描述

請注意,我附加了一個連續的數字列ID 為什么? 因為您希望名稱按字母順序排列,並且行會按照它們在您的源代碼中出現的順序添加到繪圖中。 這有點棘手,因為使用dt %>% arrange((Entity))直接向上排序會給你Entity1, Enitity10, Entity11等。如果你想以任何其他方式調整它,請告訴我。

代碼:

library(dplyr)
library(plotly)

# data
set.seed(123)

dt <- as.data.frame(matrix(ncol = 13, nrow = 19))
colnames(dt) <- c("Entity", month.abb)
for (i in 1:nrow(dt)) {
  dt[i, 1] <- paste("Entity", i, sep="")
  dt[i, -1] <- floor(runif(12, min=0, max=100))
}

# assign colors to entities
dt$"EntityColor" <- c("#074263", "#0B5394", "#3D85C6", "#6D9EEB", "#A4C2F4", "#CFE2F3", "#5B0F00", "#85200C", "#A61C00", "#CC4125", "#DD7E6B", "#E6B8AF", "#F8CBAD", "#F4CCCC", "#274E13", "#38761D", "#E06666", "#CC0000", "#20124D")



# sort data
dt$ID <- seq.int(nrow(dt))
dt <- dt %>% arrange(desc(ID))


# specify month as factor variable to ensure correct order
months=names(dt)[2:13]
months<- factor(months, levels = c(months))

# plotly setup
p <- plot_ly(type = 'bar')

# add trace for each entity
nrows = nrow(dt)
for(i in 1:nrows) {
    p <- p %>% add_trace(x=months, y = unlist(dt[i,2:13], use.names=F), type = 'bar',
                         #name = paste(dt[i,1], dt[i,14], sep = "_"),
                         name = dt[i,1],
                         type = 'bar',  
                         marker=list(color = dt[i,14])) %>%
       layout(barmode = 'stack')

}

# Edit layout
p <- p %>% layout(title = list(xanchor='right', text='Correct colors, orderered legend'),
                  yaxis = list(title = ''),
                  xaxis = list(title = 'month'))
p

顏色正確性驗證:

在此處輸入圖像描述

初步建議

這是一個初步的建議。 首先, color = ~Entity必須離開。 marker = list(color = ~EntityColor)marker = list(colors = ~EntityColor)給出了兩個不同的結果。 奇怪的是餅圖文檔使用:

marker = list(colors = colors, ...)

...並且條形圖文檔使用:

marker = list(color = c('rgba(204,204,204,1)', 'rgba(222,45,38,0.8)', ...)

...沒有color末尾的s

無論哪種方式,您都應該測試marker = list(color = ~EntityColor)marker = list(colors = ~EntityColor)並查看適合您的方法。

陰謀:

在此處輸入圖像描述

代碼:

dt <- as.data.frame(matrix(ncol = 13, nrow = 19))
colnames(dt) <- c("Entity", month.abb)

for (i in 1:nrow(dt)) {
  dt[i, 1] <- paste("Entity", i, sep="")
  dt[i, -1] <- floor(runif(12, min=0, max=100))
}

# assign colors to entities

dt$"EntityColor" <- c("#074263", "#0B5394", "#3D85C6", "#6D9EEB", "#A4C2F4", "#CFE2F3", "#5B0F00", "#85200C", "#A61C00", "#CC4125", "#DD7E6B", "#E6B8AF", "#F8CBAD", "#F4CCCC", "#274E13", "#38761D", "#E06666", "#CC0000", "#20124D")

data.table::melt(dt) %>%

  plot_ly(x = ~variable,
          y = ~value,
          name= ~Entity,
          type = "bar",
          #color = ~Entity,
          marker = list(colors = ~EntityColor)
  ) %>%

  layout(yaxis = list(title = ""),
         xaxis = list(title = ""),
         barmode = 'stack')

看看它是如何為你工作的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM