Reputation: 342
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?
Upvotes: 1
Views: 15598
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
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")
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)
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
)
Upvotes: 6
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")
Upvotes: 1