我怎样才能创建一个条形图,其中的条形图堆叠在(重叠)彼此的前面?
How can I create a bar chart where the bars are stacked in front of (overlaying) one another?
对于每个日期值(天),我想显示(作为叠加条)有多少电话未接以及有多少电话已完成。
理想情况下,它看起来像这样(在 Tableau 中生成):
条形的绿色部分代表已完成的聊天部分(如果适用),因此在此示例中,用户看到 2018 年 4 月 1 日有 1 个已完成的聊天和 4 个错过的聊天,即使 Total
bar 实际上拥有 5 的值。
此代码与 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~.)
它生成这张图:
这张图的问题是条形图一个接一个地堆叠。在此示例中,表示 Retailer 1
的方面中的 Missed
(红色)列如果放在它后面,将略高于绿色(Completed
)列,这就是我想要的出现。
我想要做的是将一根柱子 堆叠在另一根柱子的前面 。
我的问题是:如何生成在已完成的聊天之上显示未接聊天的内容?我能想到的最好的办法是将条形图堆叠在一起。
我的数据:
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
注意:
此图的未来迭代(或最终演变)将在背景中显示一个 Total
列(这是 Completed
和 Missed
的总和值)和Missed
列在前面。事实上,结果是 "illusion" 排序,Total
和 Missed
之间的差异表示 Completed
聊天的数量。简而言之,Missed
将始终小于或等于总数(因为某一天的所有聊天可能都被错过了。(Missed <= Total
。)
这解决了 tidyr
(对于 spread
)、dplyr
(对于 mutate
)和 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()
对于每个日期值(天),我想显示(作为叠加条)有多少电话未接以及有多少电话已完成。
理想情况下,它看起来像这样(在 Tableau 中生成):
条形的绿色部分代表已完成的聊天部分(如果适用),因此在此示例中,用户看到 2018 年 4 月 1 日有 1 个已完成的聊天和 4 个错过的聊天,即使 Total
bar 实际上拥有 5 的值。
此代码与 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~.)
它生成这张图:
这张图的问题是条形图一个接一个地堆叠。在此示例中,表示 Retailer 1
的方面中的 Missed
(红色)列如果放在它后面,将略高于绿色(Completed
)列,这就是我想要的出现。
我想要做的是将一根柱子 堆叠在另一根柱子的前面 。
我的问题是:如何生成在已完成的聊天之上显示未接聊天的内容?我能想到的最好的办法是将条形图堆叠在一起。
我的数据:
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
注意:
此图的未来迭代(或最终演变)将在背景中显示一个 Total
列(这是 Completed
和 Missed
的总和值)和Missed
列在前面。事实上,结果是 "illusion" 排序,Total
和 Missed
之间的差异表示 Completed
聊天的数量。简而言之,Missed
将始终小于或等于总数(因为某一天的所有聊天可能都被错过了。(Missed <= Total
。)
这解决了 tidyr
(对于 spread
)、dplyr
(对于 mutate
)和 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()