Nisha G. Nisha G. - 1 year ago 112
R Question

Custom Colors Bar Graph Plotly - R

I have a grouped bar graph and I am trying to customize the colors for each of the variables or columns. I was able to to it for the first, but if I tired that with the second a third bar would populate.

What would be the best method to go about this?
My code is below:

SpendOpt <- plot_ly(
x= TV_Attribute_Solver$Channel,
y= TV_Attribute_Solver$Current.spend,
name="Current Spend",
type = "bar",
marker = list(color = "#33aFFF"))

SpendOpt <- add_trace(
name = "Optimized Spend",
type = "bar"

the data would be:

> dput(data)

structure(list(Channel = c("13th Street", "7 TWO Sydney", "7MATE Sydney",
"Arena", "ATN-7 Sydney", "BBC Knowledge"), Current.spend = c(2782L,
2075L, 990L, 1194L, 32534L, 356L), Optimized.Spend = c(3060.2,
2282.5, 891, 1313.4, 33410.127, 391.344)), .Names = c("Channel",
"Current.spend", "Optimized.Spend"), class = "data.frame", row.names = c(NA,

> data
Channel Current.spend Optimized.Spend
1 13th Street 2782 3060.200
2 7 TWO Sydney 2075 2282.500
3 7MATE Sydney 990 891.000
4 Arena 1194 1313.400
5 ATN-7 Sydney 32534 33410.127
6 BBC Knowledge 356 391.344

Also is there a way to create more space between the two groupings?
thank you

Answer Source

I figured I'd just go ahead and add an answer. I've only used plotly for the first time ever, so I'm not really sure how the code you have works. That said, if I understand the question correctly, you want colors based on whether or not it's Optimized.spend or Current.spend. I just extended what you did above to the second part, and I get this:

SpendOpt <- plot_ly(
   x= data$Channel,
   y= data$Current.spend,
   name="Current  Spend",
   type = "bar",
   marker = list(color = "#33aFFF")

SpendOpt <- add_trace(
   name = "Optimized Spend",
   type = "bar",
   marker = list(color = "#afafaf")


I get this:

enter image description here

If that's what you expected, perhaps something else is going on with your setup. I don't see the mentioned "third bar." You might want to try ls() to see what variables you have created or double check something else.

If this isn't what you were trying to achieve, feel free to comment/clarify. This goes back to my suggestion in the comments that reproducible examples and visuals speak so much louder than words: take a screenshot of what you get and mark it up with labels or hand-drawn corrections in the editor of your choice, for example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download