使用 Tidyverse 重组 Ggplot2 组合分组和堆叠条形图的数据

Restructuring Data for Ggplot2 Combination Grouped and Stacked Barchart Using Tidyverse

library(tidyverse)
library(ggplot2)

我正在尝试创建下面的条形图,但在重组数据时遇到问题。我在下面提供了一些示例数据,我创建得有点快,所以结果可能很奇怪,但我更感兴趣的是如何使用 tidyverse 工具来设置数据。

Q1_Sat<-c("Sat","Sat","Sat","Other","Other","Other","Other","Other")
Q1_VSat<-c("VSat","Other","Other","VSat","VSat","VSat","VSat","VSat")
Q1_M<-c("SatVSat","SatVSat","SatVSat","SatVSat","Other","Other","SatVSat","SatVSat")
Q2_Sat<-c("Sat","Other","Sat","Other","Sat","Sat","Other","Other")
Q2_VSat<-c("VSat","Other","VSat","Other","VSat","VSat","VSat","VSat")
Q2_M<-c("SatVSat","SatVSat","SatVSat","SatVSat","SatVSat","SatVSat","SatVSat","Other")
Q3_Sat<-c("Sat","Other","Sat","Other","Sat","Sat","Sat","Sat")
Q3_VSat<-c("VSat","Other","VSat","Other","Other","Other","Other","VSat")
Q3_M<-c ("SatVSat","SatVSat","SatVSat","Other","Other","Other","Other","Other")

Q4_Sat<-c("Sat","Other","Other","Other","Other","Other","Other","Other")
Q4_VSat<-c("VSat","VSat","VSat","VSat","VSat","VSat","VSat","VSat")
Q4_M<-c("SatVSat","Other","Other","Other","Other","Other","SatVSat","SatVSat")

Q20<-c("Nat","Internat","Nat","Nat","Internat","Internat","Nat","Nat")
Calc_Sat<-c("Sat","Sat","Sat","Other","Other","Other","Sat","Sat")
Calc_VSat<-c("Other","Other","VSat","VSat","VSat","VSat","Other","VSat")
PCode<-c("C11","C11","H12","F33","F33","C11","S33","F33")
CCode<-c("Dept","Camit","Camit","CCT","Dept","CCT","TTT","CCT")
Data<-data_frame(Q1_Sat,Q1_VSat,Q1_M,Q2_Sat,Q2_VSat,Q2_M,Q3_Sat,Q3_VSat,Q3_M,Q4_Sat,Q4_VSat,Q4_M,Q20,PCode,CCode,Calc_Sat,Calc_VSat)

下面是我到目前为止开发的代码,但我一直卡在这一点上,不确定如何为彩色分组条合并 Q20 变量。我想使用 Tidyverse 和 ggplot2 来实现这一点。任何其他有关如何使我的代码更优雅和紧凑的反馈也将不胜感激。

Data%>%
select(-CCode,-Q1_M,-Q2_M,-Q3_M,-Q4_M)%>%
gather(key,value,-PCode,-Q20)%>%
filter(PCode=="C11")%>%
count(Q20,key,value)%>%
mutate(perc=round(n/sum(n),2))%>%
separate(key,c("Question","SatLevel"),sep="_")%>%
filter(value != "Other")%>%
ggplot(aes(x=Question,y=perc,fill=SatLevel))+geom_col()

一般来说,ggplot需要长格式的表格,你的数据好像很宽。也就是说,最后您的数据应该类似于:

Q barColor barShade Y
1 grey light 55
1 grey dark 20
1 blue light 57
1 blue dark 21
2 grey light 23
...

这样你就可以用 aes(color=barColor, y=Y) 等调用 ggplot
我会说 tidyr::gather 应该负责大部分重组,但也请参阅这个很棒的 cheatsheet 以获取其他有用的工具。


编辑:堆叠+分组条形图的可能解决方案,不使用facet_wrap

df = Data%>%
    select(-CCode,-Q1_M,-Q2_M,-Q3_M,-Q4_M)%>%
    gather(key,value,-PCode,-Q20)%>%
    filter(PCode=="C11")%>%
    count(Q20,key,value)%>%
    mutate(perc=round(n/sum(n),2))%>%
    separate(key,c("Question","SatLevel"),sep="_")%>%
    filter(value != "Other") df$Question = c(14, 14, 1, 1, 4, 4, 7, 10,
                15, 2, 2, 5, 5, 8, 8, 11, 11)

ggplot(df, aes(x=Question,y=perc,fill=SatLevel)) + geom_col() +
    theme_bw() +
    scale_x_continuous(breaks=c(1.5, 4.5, 7.5, 10.5, 14.5),
                       labels=c("Q1", "Q2", "Q3", "Q4", "Calc"))