我试图在plotly中创建一个分组条形图,但我似乎无法在一个组中为我的条形图着色(因此它们都是相同的颜色)。有人知道如何在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)
)
#plot code so far
sample %>%
plot_ly(
x = Category,
y = sales,
type = "bar",
group = SubCategory
)
下面是我到目前为止所做的,但是着色不是基于分组的。当我提供颜色变量时,它不会将子类别中的所有条都着色为相同的颜色。这可能是bug吗?
3条答案
按热度按时间bfrts1fy1#
使用
ggplot2
....现在使用
plotly
的ggplotly
最后,使用原始
plotly
amrnrhlw2#
虽然我知道这个问题需要一个
plotly
的解决方案,但我想在我的go to包中提出一个非常简单的解决方案(对于其他一些人,我肯定),用于图表-ggplot2
!qv7cva1a3#
详细阐述@petergensler非常全面的答案,到目前为止,
group
在plotly中被弃用。Plotly现在更喜欢用户使用plotly::group_by(),这很简单:请注意,plotly的group_by()可能会导致一些令人沮丧的包问题,而更流行的dplyr::group_by(),所以最好是具体的。