在 Plotly 中分组的条形图

问题描述 投票:0回答:3

我正在尝试在 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
  )

以下是我到目前为止的内容,但着色不是基于分组。当我提供颜色变量时,它不会为 SubCategory 中的所有条形图着色相同的颜色。这是可能的错误吗?

r plotly
3个回答
6
投票

使用

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")

现在使用

plotly
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)

最后,用原装

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
)


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")


0
投票

详细说明@petergensler 的非常全面的答案,到目前为止

group
已被弃用。 Plotly 现在更喜欢用户使用 plotly::group_by(),它足够简单:

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)

请注意,plotly 的 group_by() 可能会以更流行的 dplyr::group_by() 方式导致一些令人沮丧的包问题,所以最好具体一点。

© www.soinside.com 2019 - 2024. All rights reserved.