library(tidyverse)
library(ggplot2)
I am attempting to create the bar chart below but I'm having trouble restructuring the data. I provided some sample data below which I created kind of fast, so the results may be strange, but I'm more interested in how to use tidyverse tools to set up the data.
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)
Below is the code I've developed so far but I'm stuck at this point and not sure how to incorporate the Q20 variable for the coloured grouped bars. I would like to use Tidyverse and ggplot2 to achieve this. Any other feedback about how to make my code more elegant and compact would also be greatly appreciated.
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()