我试图在plotly中创建一个分组的条形图,但我似乎无法在一组中为我的条纹着色(所以它们都是相同的颜色)。谁有人知道如何在剧情中做到这一点?我想根据SubCategory为我的条形图着色(因此子类别中的所有条形都有自己的颜色)。我试过在图表中添加痕迹,但没有运气。感谢。
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
)
以下是我到目前为止所做的,但着色不是基于分组。当我提供颜色变量时,它不会为SubCategory中的所有条纹着色相同的颜色。这可能是错误吗?
答案 0 :(得分:2)
使用X
....
65535
现在使用ggplot2
&#39; s 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")
plotly
ggplotly(曲线)
最后,使用原始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")
答案 1 :(得分:1)
虽然我理解这个问题要求plotly
解决方案,但我想在我的首选方案中提出一个非常简单的解决方案(对于我确定的其他一些方案)图表 - 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")