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")

问题是我只在一根柱子上得到堆积的百分比。 这是我的输出 我该如何解决它以调整百分比并以预定义的顺序修复图例?

4

1 回答 1

0

我不完全确定这就是你要找的东西,但从我收集到的信息来看:你快到了。您只需在图层中添加position="dodge"参数并在图层geom_bar()中添加附加参数。position=position_dodge(width=0.9)geom_text()

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")

在此处输入图像描述

于 2016-08-16T02:24:44.560 回答