2016-09-20 46 views
1

我使用plot.ly庫在shiny應用程序中進行交互式製圖,但是我遇到了管理圖表中顏色的一些麻煩。管理R中的顏色爲plot.ly

使用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圖表的截圖創建

Current plotly output

+0

這對我有用,放下'〜'。你的情節是什麼樣子? –

+0

已根據提供的原始代碼添加圖像。迴應刪除'〜';我收到一條錯誤信息,說'object'campaign_week'not found'。 – Dan

+0

@VanceLopez你使用的是什麼劇情版本? OP引用版本。 4.3.5(來自github的開發版本)與早期版本有許多語法和行爲改變。 – dww

回答

1

根據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") 

enter image description here

注意,這給出了一個警告,因爲條形圖的痕跡繼承散點圖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") 
+0

不是最乾淨的解決方案(如果我在某個時候最終得到其他類別),但是我認爲我可能在這一點上最好。 – Dan

+1

@Dan如果最後有很多類別,可以將它們添加到一個循環中。例如看[這裏](http://stackoverflow.com/a/39420523/2761575) – dww

0

我恢復了通話,並添加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") 

還有一個問題與傳說:

enter image description here

+0

看起來非常有希望,關於爲什麼「收入」的圖例顏色與圖表上顯示的內容不匹配的任何想法? – Dan

+0

還有一個問題:'plot_ly(data = dt,x =〜campaign_week,y =〜amount,color =〜category,type =「bar」)' – HubertL

+0

我最終發現錯誤'eval(expr, envir,enclos):找不到對象'category' – Dan