如何创建条形图,其中条形图堆叠在彼此前面(重叠)?

时间:2021-04-08 21:11:33

For each date value (day), I want to show (as an overlayed bar) how many calls were missed and how many were completed.

对于每个日期值(天),我想显示(作为重叠栏)错过了多少次呼叫以及完成了多少次呼叫。

Ideally, it will look like this (produced in Tableau):

理想情况下,它看起来像这样(在Tableau中生成):

如何创建条形图,其中条形图堆叠在彼此前面(重叠)?

The green section of the bars represent the portion of chats that were completed (if applicable), so in this example the user sees that there was 1 completed chat on 1st April 2018 and 4 missed chats, even though the Total bar does in fact possess a value of 5.

条形图的绿色部分表示已完成的聊天部分(如果适用),因此在此示例中,用户看到2018年4月1日有1个完成的聊天,4个错过的聊天,即使总计确实拥有值为5。

This code doesn't match the Tableau example (as it doesn't display Total), but it is headed in the right direction:

此代码与Tableau示例不匹配(因为它不显示Total),但它朝着正确的方向前进:

library(ggplot2)
ggplot(new_data, aes(x = date,
                 y = count,
                 fill = type)) +
  scale_fill_manual(values = c("forestgreen", "red")) +
  geom_bar(data = new_data[new_data$retailer == "Retailer 1", ],
           colour = "black",
           stat = "identity") +
  ggtitle("Completed vs. Missed Calls") +
  geom_bar(data = new_data[new_data$retailer == "Retailer 2", ],
           colour = "black",
           stat = "identity") +
  facet_grid(retailer~.) 

It produces this graph:

它产生了这个图:

如何创建条形图,其中条形图堆叠在彼此前面(重叠)?

The problem with this graph is that the bars are stacked on top of one another. In this example, the Missed (red) column in the facet representing Retailer 1 will be slightly taller than the green (Completed) column if placed behind it, which is how I want it to appear.

该图表的问题在于条形图堆叠在彼此之上。在此示例中,表示零售商1的构面中的Missed(红色)列将比绿色(已完成)列稍高,如果放在它后面,这就是我希望它出现的方式。

What I want to do is to stack one bar in front of the other.

我想要做的是将一个酒吧堆叠在另一个酒吧的前面。

My question is this: how do I produce something that shows the missed chats on top of the completed chats? The best I can come up with is the bars being stacked on top of one another.

我的问题是:如何在完成的聊天之上制作能够显示遗漏聊天内容的内容?我能想到的最好的是将酒吧堆叠在一起。

My data:

date            type        count   retailer
April 17 2018   Completed   12      Retailer 1
April 17 2018   Missed      13      Retailer 1
April 18 2018   Completed   10      Retailer 2
April 18 2018   Completed   11      Retailer 1
April 18 2018   Missed      5       Retailer 1
April 19 2018   Completed   10      Retailer 1
April 19 2018   Missed      1       Retailer 1
April 20 2018   Completed   2       Retailer 2
April 20 2018   Missed      1       Retailer 1
April 21 2018   Completed   2       Retailer 1
April 21 2018   Completed   1       Retailer 2
April 21 2018   Missed      1       Retailer 1
April 23 2018   Completed   2       Retailer 1
April 23 2018   Missed      2       Retailer 2

NOTE:

A future iteration (or the eventual evolution) of this graph will show a Total column (which is the summed value of Completed and Missed) in the background and the Missed column in front. The result is, in fact, an "illusion" of sort, with difference between Total and Missed representing the number of Completed chats. In short, Missed will always be less than or equal to the total (because all chats on a certain day could have been missed. (Missed <= Total.)

此图的未来迭代(或最终演变)将显示背景中的总列(完成和未命中的总和值)和前面的未命中列。事实上,结果是排序的“错觉”,Total和Missed之间的差异代表完成聊天的数量。简而言之,Missed总是小于或等于总数(因为某一天的所有聊天都可能被错过。(错过<=总数。)

1 个解决方案

#1


2  

This solves your problem with tidyr (for spread), dplyr (for mutate) and ggplot2:

这解决了你的tidyr(传播),dplyr(变异)和ggplot2的问题:

library(dplyr)
library(tidyr)
library(ggplot2)

my_df %>%
  spread(type, count, fill = 0) %>%   # Spread the count column in missed and completed
  mutate(Total = Completed + Missed) %>%   # Create the Total column
  ggplot(aes(date, Total)) + 
  geom_col(aes(fill = "Total")) + # total bar (with stat = "identity")
  geom_col(aes(y = Missed, fill = "Missed")) + # missed bar
  geom_text(aes(label = paste("Total chats:", Total)), # add total label
                hjust = -0.05, vjust = 1) + 
  geom_text(aes(label = paste("Missed chats:", Missed)), # add missed label
                hjust = -0.05, vjust = -0.5, color = "red") + 
  scale_fill_manual(name = "",  # Manual fill scale
                    values = c("Total" = "forestgreen", "Missed" = "red")) +
  facet_grid(retailer~.) +  # Displayed per retailer
  scale_y_continuous(limits = c(0, 40)) + # Make labels visible
  coord_flip() + # switch x and y axes
  theme_minimal()

如何创建条形图,其中条形图堆叠在彼此前面(重叠)?

#1


2  

This solves your problem with tidyr (for spread), dplyr (for mutate) and ggplot2:

这解决了你的tidyr(传播),dplyr(变异)和ggplot2的问题:

library(dplyr)
library(tidyr)
library(ggplot2)

my_df %>%
  spread(type, count, fill = 0) %>%   # Spread the count column in missed and completed
  mutate(Total = Completed + Missed) %>%   # Create the Total column
  ggplot(aes(date, Total)) + 
  geom_col(aes(fill = "Total")) + # total bar (with stat = "identity")
  geom_col(aes(y = Missed, fill = "Missed")) + # missed bar
  geom_text(aes(label = paste("Total chats:", Total)), # add total label
                hjust = -0.05, vjust = 1) + 
  geom_text(aes(label = paste("Missed chats:", Missed)), # add missed label
                hjust = -0.05, vjust = -0.5, color = "red") + 
  scale_fill_manual(name = "",  # Manual fill scale
                    values = c("Total" = "forestgreen", "Missed" = "red")) +
  facet_grid(retailer~.) +  # Displayed per retailer
  scale_y_continuous(limits = c(0, 40)) + # Make labels visible
  coord_flip() + # switch x and y axes
  theme_minimal()

如何创建条形图,其中条形图堆叠在彼此前面(重叠)?