petergensler
petergensler

Reputation: 342

Grouped Bar Chart with grouping in Plotly

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

Upvotes: 1

Views: 15598

Answers (3)

Andrew Taylor
Andrew Taylor

Reputation: 61

Elaborating on @petergensler's very comprehensive answer, to date group is deprecated in plotly. Plotly now prefers users to use plotly::group_by(), which is straightforward enough:

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"
    ) %>% 
      plotly::group_by(Category)

Note that plotly's group_by() can cause some frustrating package problems with the way more popular dplyr::group_by(), so good to be specific.

Upvotes: 1

Cyrus Mohammadian
Cyrus Mohammadian

Reputation: 5193

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

Upvotes: 6

prateek1592
prateek1592

Reputation: 547

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

Upvotes: 1

Related Questions