【发布时间】:2019-03-08 06:24:45
【问题描述】:
我正在尝试制作一个水平分组条形图,显示人们如何回答不同的问题。
有没有办法可以将问题分成几个部分,在每个部分上方加上一个粗体标题,说明它是什么?如果没有它,最好不要像我使用面板那样产生单独的绘图区域。
对于下面的示例,假设我要插入的标题是“元音”和“辅音”(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)
【问题讨论】: