2016-08-15 53 views
1

我试图使用该R代码如何使用ggplot2调整方面堆积条形图顶部的碰撞百分比?

library(ggplot2) 
library(scales) 
#q6 
d=data.frame(
rbind(as.data.frame(table(data$q6_a))[2,2], 
    as.data.frame(table(data$q6_b))[2,2], 
    as.data.frame(table(data$q6_c))[2,2], 
    as.data.frame(table(data$q6_d))[2,2], 
    as.data.frame(table(data$q6_e))[2,2])) 
d[is.na(d)] <-0 
colnames(d)="Freq" 

df=data.frame(Pourcentage=c(2.4,14.3,14.3,52.4,16.7,2.4,11.9 
,19,47.6,19,0,12.2,17.1,53.7,17.1,0,7.3,19.5,43.9,29.3, 
0,17.1,19.5,39,24.4), 
Satisfaction=rep(c("Très insatisfait","Insatisfait", 
"Moyennement satisfait","Satisfait","Très satisfait"),5), 
    Processus=rep(c("Clarté des informations fournis", 
    "Simplicité des formulaires à remplir","Clarté des formulaires à remplir", 
    "Délai d'exécution de l'opération d'ouverture", 
    "Retour de l'information"),each=5)) 

blank_theme = 
theme(axis.ticks = element_blank(), 
    plot.title=element_text(size=14, face="bold.italic") 
) 

df$Processus = as.factor(as.vector(df$Processus)) 
df$Satisfaction=as.factor(df$Satisfaction) 
p=ggplot(data=df, aes(Processus,Pourcentage, fill=Satisfaction))+ 
geom_bar(aes(fill = Satisfaction),stat="identity",width=0.6)+ 
geom_text(data=df,  aes(label=paste(round(Pourcentage,1),"%")), 
size=4,vjust=0.5,hjust=0.5,position ="stack")+ 
blank_theme+ggtitle("Evaluez votre satisfaction par rapport 
au processus d'ouverture de votre compte courant ?")+ 
theme_classic()+ 
scale_y_continuous(labels=percent,breaks=NULL)+ 
xlab("Procéessus d'ouverture du compte courant")+ 
scale_fill_discrete(breaks=c("Très insatisfait","Insatisfait", 
"Moyennement satisfait","Satisfait","Très satisfait"), 
        labels=c("Très insatisfait","Insatisfait", 
"Moyennement satisfait","Satisfait","Très satisfait"))+ 
coord_flip()+scale_fill_brewer(palette="PuBuGn") 
p + theme(
axis.title.x = element_blank(), 
axis.title.y = element_blank(), 
legend.title=element_blank(), 
legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"), 
legend.position="bottom") 

的问题是,我得到堆叠percenteges在仅有的一个酒吧密谋与GGPLOT2分组条形图。 This is my output 我怎样才能解决它调整百分比,并在预定义的顺序修复图例?

+0

本帖](http://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2)将帮助你调整标签。除非你定义,因素水平的顺序是按字母顺序的。您应该通过'df $ Satisfaction = factor(df $ Satisfaction,levels = c(“Trèsinsatisfait”,“Insatisfait”,“Moyennement satisfait”,“Satisfait”,“Trèssatisfait”))''而不是'as'来定义级别。因子()'。 – cuttlefish44

+0

你想要最终的输出看起来像什么?你能提供一个类似的图表的例子吗? –

回答

0

我不完全确定这是你在找什么,但从我收集的东西:你的'几乎在那里。您只需在geom_bar()图层中添加position="dodge"参数并在geom_text()图层中添加其他position=position_dodge(width=0.9)

library(ggplot2) 
library(scales)  
df=data.frame(Pourcentage=c(2.4,14.3,14.3,52.4,16.7,2.4,11.9 
    ,19,47.6,19,0,12.2,17.1,53.7,17.1,0,7.3,19.5,43.9,29.3, 
    0,17.1,19.5,39,24.4), 
    Satisfaction=rep(c("Très insatisfait","Insatisfait", 
    "Moyennement satisfait","Satisfait","Très satisfait"),5), 
     Processus=rep(c("Clarté des informations fournis", 
     "Simplicité des formulaires à remplir","Clarté des formulaires à remplir", 
     "Délai d'exécution de l'opération d'ouverture", 
     "Retour de l'information"),each=5)) 

blank_theme = 
theme(axis.ticks = element_blank(), 
    plot.title=element_text(size=14, face="bold.italic") 
) 


ggplot(data=df, aes(Processus,Pourcentage, fill=Satisfaction)) + 
    geom_bar(width = 1, aes(fill = Satisfaction),stat="identity", position="dodge", size=1)+ 
    geom_text(data=df, aes(label=paste(round(Pourcentage,1),"%")), 
      size=3,position=position_dodge(width=0.9), vjust=0.7, hjust=-0.1)+ 
    blank_theme+ggtitle("Evaluez votre satisfaction par rapport 
         au processus d'ouverture de votre compte courant ?")+ 
    theme_classic()+ 
    scale_y_continuous(labels=percent,breaks=NULL)+ 
    xlab("Procéessus d'ouverture du compte courant")+ 
    scale_fill_discrete(breaks=c("Très insatisfait","Insatisfait", 
           "Moyennement satisfait","Satisfait","Très satisfait"), 
         labels=c("Très insatisfait","Insatisfait", 
           "Moyennement satisfait","Satisfait","Très satisfait"))+ 
    coord_flip()+scale_fill_brewer(palette="PuBuGn") + theme(
    axis.title.x = element_blank(), 
    axis.title.y = element_blank(), 
    legend.title=element_blank(), 
    legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"), 
    legend.position="bottom") 

enter image description here