1

I'm trying to plot a grouped bar chart with ggplot2 using this R code

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

The problem is that i get stacked percenteges over just one bar. This is my output How can i solve it to adjust percentages and to fix the legend in the predefined order?

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
Karima Touati
  • 37
  • 2
  • 7
  • [This post](http://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2) will help you to adjust labels. And unless you define, order of factor levels is alphabetical. You should define levels by `df$Satisfaction = factor(df$Satisfaction, levels = c("Très insatisfait","Insatisfait", "Moyennement satisfait","Satisfait","Très satisfait"))` instead of `as.factor()`. – cuttlefish44 Aug 15 '16 at 20:05
  • what do you want the final output to look like? can you provide an example of a similar looking graph? – Cyrus Mohammadian Aug 16 '16 at 02:05

1 Answers1

0

I'm not entirely sure this is what you're looking for but from what I gather: your'e almost there. You just need to add the position="dodge" parameter in the geom_bar() layer and an additional position=position_dodge(width=0.9) in the geom_text() layer.

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

Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62