I am trying to make a horizontal grouped bar plot showing how people have answered different questions.
Is there a way I can divide the questions into sections, putting a bold heading above each section stating what it is? Preferably without it resulting in separate plot areas the way it would if I used panels.
For the example below, suppose the headings I want to insert are "Vowels" and "Consonants" (hand-drawn in red in the picture in this example)
library('ggplot2')
library('stringr')
set.seed(5)
questions <- str_wrap(c('Blah blah blah blah blah blah B?',
'Blbbity blah blibbity blah C?',
'Blah blah blibbity blah blah blah D?',
'Blah blah blah A?',
'Blah blah blah blibbity E?',
'Blah blah blibbity blah I?'),15)
status <- data.frame(matrix(data=NA, nrow=18,ncol=3))
names(status) <- c('varname','type','percent')
status['varname'] <- factor(c(rep(1,3),rep(2,3),rep(3,3),rep(4,3),rep(5,3),rep(6,3)),labels=questions)
status['type'] <- c(rep(c('Cohabiting','Married','Divorced'),6))
status['percent'] <- c(rnorm(18,.5,.2))
ggplot(status, aes(varname, percent)) +
theme(axis.title.y = element_blank(), legend.title = element_blank(), plot.title = element_text(hjust = 0.5), legend.position = "top") +
geom_bar(aes(fill = type), position = "dodge", stat="identity") + coord_flip() + scale_fill_manual(values=c('cadetblue1','cadetblue3','darkcyan')) +
ggtitle('By couple type') + labs(y='Percent') + ylim(0,1)