Dan
Dan

Reputation: 2675

managing colors in R for plot.ly

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

Upvotes: 2

Views: 3283

Answers (2)

dww
dww

Reputation: 31452

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

Upvotes: 2

HubertL
HubertL

Reputation: 19544

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

Upvotes: 0

Related Questions