title: managing colors in R for plot.ly
body: <p>I am using the <code>plot.ly</code> library for interactive charting in a <code>shiny</code> app however I am running up against some trouble with managing the colors in the chart.</p>
  
  <p>Reproducible example using plotly 4.3.5 (from <a href="https://github.com/ropensci/plotly" rel="nofollow noreferrer">github</a>):</p>
  
  <pre><code>library(data.table)
  library(plotly)
  
  dt &lt;- 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 &lt;- dt[, .(amount = sum(amount)), by = campaign_week][,.(campaign_week, amount = cumsum(amount))]
  
  y &lt;- 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") %&gt;%
    add_trace(data = dt, x = ~campaign_week, y = ~amount, color = ~category, type = "bar",
              colors = c("#00ff00", "#ff0000")) %&gt;%
    layout(yaxis = y, barmode = "relative") 
  </code></pre>
  
  <p>This creates the chart that I want, however the colours aren't being applied correctly to the <code>trace</code>. I am expecting one of the bars to be red, and the other to be green while the line is a shade of blue.</p>
  
  <p><strong>EDIT</strong> Add a screenshot of the <code>plotly</code> chart created</p>
  
  <p><a href="https://i.stack.imgur.com/DRvVY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DRvVY.png" alt="Current plotly output"></a></p>