簡體   English   中英

在 R 中為 plot.ly 管理顏色

[英]managing colors in R for plot.ly

我正在使用plot.ly庫在一個shiny應用程序中進行交互式圖表,但是我在管理圖表中的顏色時遇到了一些問題。

使用 plotly 4.3.5 的可重現示例(來自github ):

library(data.table)
library(plotly)

dt <- data.table(campaign_week = c(1,2,3,1,2,3), category = c(rep("income",3),rep("cost",3)),
                 amount = c(100,50,35,-500,-20,-15))
dt_net <- dt[, .(amount = sum(amount)), by = campaign_week][,.(campaign_week, amount = cumsum(amount))]

y <- list(title = "Income", tickformat = "$,.0f",hoverformat = "$,.2f") 

plot_ly(dt_net, x = ~campaign_week, y = ~amount, type = "scatter",
        mode= "lines+markers",
        line = list(color = "#00AEFF"), name = "Net Income") %>%
  add_trace(data = dt, x = ~campaign_week, y = ~amount, color = ~category, type = "bar",
            colors = c("#00ff00", "#ff0000")) %>%
  layout(yaxis = y, barmode = "relative") 

這將創建我想要的圖表,但是顏色沒有正確應用於trace 我期望其中一個條形為紅色,另一個為綠色,而線條為藍色陰影。

編輯添加創建的plotly圖表的屏幕截圖

當前情節輸出

根據https://plot.ly/r/bar-charts/#bar-chart-with-relative-barmode 中的示例,可以為每個類別單獨添加 add_trace。

plot_ly(dt_net, x = ~campaign_week, y = ~amount, type = "scatter",
        mode= "lines+markers",
        line = list(color = "#00AEFF"), name = "Net Income") %>%
  add_trace(data =  dt[category=="income",] , x = ~campaign_week, y = ~amount,  type = "bar", name = "income",
            marker=list(color = "#00ff00")) %>%
  add_trace(data =  dt[category=="cost",] , x = ~campaign_week, y = ~amount,  type = "bar", name = "cost",
            marker=list(color = "#ff0000")) %>%
  layout(yaxis = y, barmode = "relative") 

在此處輸入圖片說明

請注意,這會發出警告,因為條形圖跟蹤從散點圖繼承modeline屬性,但條形不支持這些屬性。 您可以忽略警告,也可以在分散之前調用條形圖以避免它們......像這樣:

plot_ly() %>%
  add_trace(data =  dt[category=="income",] , x = ~campaign_week, y = ~amount,  type = "bar", name = "income",
            marker=list(color = "#00ff00")) %>%
  add_trace(data =  dt[category=="cost",] , x = ~campaign_week, y = ~amount,  type = "bar", name = "cost",
            marker=list(color = "#ff0000"))  %>%
  add_trace(data =  dt_net, x = ~campaign_week, y = ~amount, type = "scatter", mode= "lines+markers",
            line = list(color = "#00AEFF"), name = "Net Income") %>%
  layout(yaxis = y, barmode = "relative")

我恢復了調用並添加了inherit=FALSE

library(data.table)
library(plotly)

dt <- data.table(campaign_week = c(1,2,3,1,2,3), category = c(rep("income",3),rep("cost",3)),
                 amount = c(100,50,35,-500,-20,-15))
dt_net <- dt[, .(amount = sum(amount)), by = campaign_week][,.(campaign_week, amount = cumsum(amount))]

y <- list(title = "Income", tickformat = "$,.0f",hoverformat = "$,.2f") 

plot_ly(data=dt, x = ~campaign_week, y = ~amount, color = ~category, type = "bar",
            colors = c("#00ff00", "#ff0000")) %>%
  add_trace( data=dt_net, x = ~campaign_week, y = dt_net$amount, type = "scatter",
            mode= "lines+markers",
            line = list(color = "#00AEFF"), name = "Net Income", inherit=FALSE) %>%
  layout(yaxis = y, barmode = "relative") 

還是有圖例問題:

在此處輸入圖片說明

暫無
暫無

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

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