简体   繁体   English

在 Plotly 中分组的分组条形图

[英]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).我试图在 plotly 中创建一个分组条形图,但我似乎无法在一个组中为我的条形着色(所以它们都是相同的颜色)。 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.当我提供颜色变量时,它不会将 SubCategory 中的所有条形着色为相同的颜色。 Is this is possible bug?这是可能的错误吗? 在此处输入图片说明

Using ggplot2 ....使用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现在使用plotlyggplotly

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最后,使用原始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
)

在此处输入图片说明

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 !虽然我知道这个问题需要一个plotly解决方案,但我想在我的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")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM