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()
答案 0 :(得分:0)
通常,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
...
这样您就可以使用ggplot
等来致电aes(color=barColor, y=Y)
我会说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"))