在plot.ly

时间:2016-09-20 02:18:00

标签: r plotly

我在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图表的屏幕截图

Current plotly output

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分: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