1
votes

I am trying to create a grouped bar chart in plotly, but I cannot seem to color my bars within a group(so they are all the same color). Does anyone know how to do this in plotly? I would like to color my barchart according to the SubCategory(so all bars in a sub-category have their own color). I have tried adding traces to a graph, but no luck. Thanks.

sample <- data.frame(
  Category <- c("Furniture","Furniture","Furniture","Furniture",
                "Office Supplies","Office Supplies", "Office Supplies", "Office Supplies",
                "Office Supplies", "Office Supplies", "Office Supplies", "Office Supplies",
                "Office Supplies", "Technology","Technology","Technology","Technology"),
  SubCategory <- c("Bookcases","Chairs","Furnishings","Tables","Appliances","Art","Binders","Envelopes", 
                   "Fasteners","Labels","Paper","Storage",  "Supplies", "Accessories","Copiers","Machines",
                   "Phones"),
  sales <- c(889222.51,920892.65,239840.16,445823.93,614737.91,225594.68,281494.68,104903.88,50156.06,44269.30,
             150113.36,692903.08,152196.19,463383.33,965899.78,458655.43,1005525.38)
)

#plot code so far
sample %>%
  plot_ly(
    x = Category,
    y = sales,
    type = "bar",
    group = SubCategory
  )

Below is what I have so far, but the coloring is not based on the grouping. When I supply a color variable, it does not color all the bars within the SubCategory the same color. Is this is possible bug? enter image description here

2
I can't reproduce the above graph with your original data and code...Cyrus Mohammadian
Sorry, there were a few naming bugs in the code. Should be fixed nowpetergensler
your new code does not produce the above graph still....Cyrus Mohammadian
what version of plotly are you using? I'm using 3.6.0. I just ran the code in my r session, and it worked fine. I just made the edits after I posted my answer, so try it again, and see if you still have issues reproducing the graph.petergensler
I have a solution using ggplotly() actually but there are spaces between the bars for furniture and technology because they don't have as many subcategories as office supplies.Cyrus Mohammadian

2 Answers

6
votes

Using ggplot2....

library(ggplot2)
library(cowplot) #ggplot2 white theme 

sample <- data.frame(
  Category <- c("Furniture","Furniture","Furniture","Furniture",
                "Office Supplies","Office Supplies", "Office Supplies", "Office Supplies",
                "Office Supplies", "Office Supplies", "Office Supplies", "Office Supplies",
                "Office Supplies", "Technology","Technology","Technology","Technology"),
  SubCategory <- c("Bookcases","Chairs","Furnishings","Tables","Appliances","Art","Binders","Envelopes", 
                   "Fasteners","Labels","Paper","Storage",  "Supplies", "Accessories","Copiers","Machines",
                   "Phones"),
  sales <- c(889222.51,920892.65,239840.16,445823.93,614737.91,225594.68,281494.68,104903.88,50156.06,44269.30,
             150113.36,692903.08,152196.19,463383.33,965899.78,458655.43,1005525.38)
)

colnames(sample)<-c("category","subcategory","Sales")

ggplot(sample, aes(category, Sales)) +   
  geom_bar(aes(fill = category, color = subcategory), position = "dodge", stat = "identity")+scale_color_manual(values = c(rep("white", 17)))+theme(legend.position = "none")

enter image description here

Now using plotly 's ggplotly

plot<-ggplot(sample, aes(category, Sales)) +   
      geom_bar(aes(fill = category, color=subcategory), position = "dodge", stat="identity")+scale_color_manual(values=c(rep("white", 17)))+theme(legend.position="none")
ggplotly(plot)

enter image description here

Finally, using original plotly

sample <- data.frame(
  Category <- c("Furniture","Furniture","Furniture","Furniture",
                "Office Supplies","Office Supplies", "Office Supplies", "Office Supplies",
                "Office Supplies", "Office Supplies", "Office Supplies", "Office Supplies",
                "Office Supplies", "Technology","Technology","Technology","Technology"),
  SubCategory <- c("Bookcases","Chairs","Furnishings","Tables","Appliances","Art","Binders","Envelopes", 
                   "Fasteners","Labels","Paper","Storage",  "Supplies", "Accessories","Copiers","Machines",
                   "Phones"),
  sales <- c(889222.51,920892.65,239840.16,445823.93,614737.91,225594.68,281494.68,104903.88,50156.06,44269.30,
             150113.36,692903.08,152196.19,463383.33,965899.78,458655.43,1005525.38)
)

sample %>%
  plot_ly(
    x = SubCategory,
    y = sales,
    type = "bar",
    group = Category
)

enter image description here

1
votes

While I understand that the question asks for a plotly solution, I would like to put forth a perfectly simple solution in my go-to package (and for quite a few others I'm certain) for charting - ggplot2 !

library(ggplot2)

sample <- data.frame(
  Category = c("Furniture","Furniture","Furniture","Furniture",
                "Office Supplies","Office Supplies", "Office Supplies", "Office Supplies",
                "Office Supplies", "Office Supplies", "Office Supplies", "Office Supplies",
                "Office Supplies", "Technology","Technology","Technology","Technology"),
  SubCategory = c("Bookcases","Chairs","Furnishings","Tables","Appliances","Art","Binders","Envelopes", 
                   "Fasteners","Labels","Paper","Storage",  "Supplies", "Accessories","Copiers","Machines",
                   "Phones"),
  sales = c(889222.51,920892.65,239840.16,445823.93,614737.91,225594.68,281494.68,104903.88,50156.06,44269.30,
             150113.36,692903.08,152196.19,463383.33,965899.78,458655.43,1005525.38)
)

ggplot(sample,aes(x=Category,y=sales)) + 
  geom_bar(stat="identity",width=0.5, position="dodge", aes(fill=SubCategory),
       color="black")

enter image description here