0

I'm using this R code:

library(ggplot2)
library(dplyr)
library(scales)
library(RColorBrewer)
#q5
d=data.frame(
 rbind(as.data.frame(table(data$q5_a))[2,2],
    as.data.frame(table(data$q5_b))[2,2],
    as.data.frame(table(data$q5_c))[2,2],
    as.data.frame(table(data$q5_d))[2,2],
    as.data.frame(table(data$q5_e))[2,2],
    as.data.frame(table(data$q5_f))[2,2],
    as.data.frame(table(data$q5_g))[2,2],
    as.data.frame(table(data$q5_h))[2,2]))
  d[is.na(d)] <-0
  colnames(d)="Freq"

 df=data.frame(Pourcentage=round((d$Freq/61),2),
 Motivation=c("Proximité","Sécurité et confiance",
 "Souplesse dans la procédure","Services adaptés à mes besoins",
  "Faiblesse des coûts","Célérité dans le traitement des opérations",              
"Accessibilité et simplicité des services" ,"Autres"))

 df$Pourcentage <-factor(df$Pourcentage,
  levels=df[order(df$Motivation), "Motivation"])
 p=ggplot(data=df, aes(x=Motivation, y=Pourcentage, fill=Motivation)) +
 geom_bar(stat="identity",width=0.5,fill="purple", colour="darkblue")+ 
 geom_text(aes(label=percent(Pourcentage)), hjust=1.4, 
 color="white",size=3.5)+
 theme_minimal()+
 ggtitle("Quelles sont les motivatons de votre choix ?")+
scale_y_continuous(labels=percent,,breaks=NULL)+ theme_classic()+
coord_flip()
p + theme(
 axis.title.x = element_blank(),
 axis.title.y = element_blank())

And i get this plot as output

Output

The problem is that i want to rearange bars with a predefined order to optimize my plot

mpalanco
  • 12,960
  • 2
  • 59
  • 67
Karima Touati
  • 37
  • 2
  • 7
  • 1
    @Hack-R Thanks. Yeah, I realized that after the fact then deleted my comment. I don't do this often I guess. – steveb Aug 14 '16 at 18:18
  • Closing as a duplicate, but I recommend the third answer in the link with `scale_x_discrete(limits=myorder)`, the others are workarounds. – Pierre L Aug 14 '16 at 18:40
  • in fact i get one bar missed in the plot with this warning message Warning messages: 1: Removed 1 rows containing missing values (position_stack). 2: Removed 1 rows containing missing values (geom_text). – Karima Touati Aug 15 '16 at 09:32

0 Answers0