在工作周和另一个小组中堆积的条形图

时间:2019-01-18 13:09:10

标签: r ggplot2 bar-chart stacked

我有几个要创建堆叠条形图的条件。

我的标准是: 工作周,数量,客户,标签(已接收,已发货)

这个(facet_grid(〜Tag))看起来还可以,但并不是我真正需要的。

ggplot(allRecords, aes(x = reorder(WW,Date), y = Quantity, fill = 
Customer)) + 
geom_bar(stat = "identity", position = "fill", colour = "black") + 
facet_grid(~Tag) +
labs(x = "Work Week") + 
ggtitle("Goods last 3 months by core customers") +
theme_bw()

我需要一个堆积图,用于每个工作周的客户“接收”和“发货”。

3 个答案:

答案 0 :(得分:0)

一些示例行:

WW <-c(1,1,1,2,2,2,3,3,3)

数量<-c(12,23,12,34,56,23,10,11,23)

日期<-lubridate :: ymd_hms(c(“ 2019-01-03 10:07:31”,“ 2019-01-03 10:07:31”,“ 2019-01-03 10:07:31 “,“ 2019-01-09 15:49:41”,“ 2019-01-09 15:49:4”,“ 2019-01-09 15:49:4”,“ 2019-01-16 09:53” :56“,” 2019-01-16 09:53:56“,” 2019-01-16 09:53:56“))

客户<-c(“ C1”,“ C2”,“其他”,“ C1”,“ C2”,“其他”,“ C1”,“ C2”,“其他”)

标签<-as.factor(c(“已接收”,“已接收”,“已发货”,“已发货”,“已接收”,“已接收”,“已发货”,“已接收”,“已接收”)))< / p>

记录<-data.frame(WW,数量,日期,客户,标签)

答案 1 :(得分:0)

也许这就是您想要的?

ggplot(records, aes(x = reorder(WW,Date), y = Quantity, fill = 
                         Tag)) + 
  geom_bar(stat = "identity", position = "fill", colour = "black") + 
  facet_grid(~Customer) +
  labs(x = "Work Week") + 
  ggtitle("Goods last 3 months by core customers") +
  theme_bw()

enter image description here

答案 2 :(得分:0)

数据

allRecords <- data.frame( 
    Quantity = c(12,23,12,34,56,23,10,11,23), 
    WW = lubridate::week(c("2019-01-03 10:07:31", "2019-01-03 10:07:31", "2019-01-03 10:07:31", "2019-01-09 15:49:41", "2019-01-09 15:49:4", "2019-01-09 15:49:4", "2019-01-16 09:53:56",     "2019-01-16 09:53:56", "2019-01-16 09:53:56")), 
    Customer = c("C1", "C2", "Other","C1", "C2", "Other","C1", "C2", "Other"), 
    Tag = as.factor(c("Received", "Received", "Shipped", "Shipped", "Received", "Received", "Shipped", "Received", "Received")) 
)

代码

ggplot(allRecords, aes(x = Tag, y = Quantity, fill = Customer)) + 
  geom_bar(stat = "identity", position = "stack", colour = "black") + 
  facet_grid(~WW) +
  labs(x = "Work Week") + 
  ggtitle("Goods last 3 months by core customers") +
  theme_bw()

输出

stacked bar chart