2

I am using the plot.ly library for interactive charting in a shiny app however I am running up against some trouble with managing the colors in the chart.

Reproducible example using plotly 4.3.5 (from 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") 

This creates the chart that I want, however the colours aren't being applied correctly to the trace. I am expecting one of the bars to be red, and the other to be green while the line is a shade of blue.

EDIT Add a screenshot of the plotly chart created

Current plotly output

Dan
  • 2,625
  • 5
  • 27
  • 42
  • This works for me, dropping the `~`. What does your plot look like? – Vance Lopez Sep 20 '16 at 02:45
  • Have added an image based on the original code provided. In response to removing the `~`; I get an error messaging saying `object 'campaign_week' not found`. – Dan Sep 20 '16 at 02:59
  • @VanceLopez what plotly version are you using? OP referenced ver. 4.3.5 (development version from github) which has a number of syntax and behaviour changes from earlier versions. – dww Sep 20 '16 at 03:24
  • @dww I missed that... I was on 3.6. – Vance Lopez Sep 20 '16 at 05:41

2 Answers2

2

Based on the example at https://plot.ly/r/bar-charts/#bar-chart-with-relative-barmode a separate add_trace for each category is the way to go.

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

Note, this gives a warning, because the bar chart traces inherit mode and line attributes from the scatter chart, but these attributes are not supported for bars. You can either ignore the warnings, or you can call the barchart before the scatter to avoid them... Like this:

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")
dww
  • 30,425
  • 5
  • 68
  • 111
  • not the cleanest solution (if I end up with other categories at some point), however the best I think I am likely to get at this point. – Dan Sep 20 '16 at 03:10
  • 1
    @Dan If you end up with many categories, you can add them in a loop. e.g. see [here](http://stackoverflow.com/a/39420523/2761575) – dww Sep 20 '16 at 03:12
0

I reverted the calls and added the 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") 

Still have a problem with the legend:

enter image description here

HubertL
  • 19,246
  • 3
  • 32
  • 51
  • Looks very promising, any thoughts on why the legend colour for the `income` doesn't match what is displayed on the chart? – Dan Sep 20 '16 at 03:00
  • There is same problem with :`plot_ly(data=dt, x = ~campaign_week, y = ~amount, color = ~category, type = "bar")` – HubertL Sep 20 '16 at 03:04
  • I end up with an error `Error in eval(expr, envir, enclos) : object 'category' not found` – Dan Sep 20 '16 at 03:11