3

I am trying to draw a grouped bar plot with the following data

groups  gents   ladies
rd  3.62    2.12
ab  2.38    1.55
dh  1.98    1.65
na  2.71    1.52
pg  2.25    1.8
ac  2.37    1.77
nb  2.28    1.68
it  2.3     1.46
ha  3.06    2.05
th  2.22    1.63
hy  2.66    1.59
ad  2.83    1.85
hy  4.16    2.53
mj  2.83    1.98
ng  3.1     2.32

Here is my code.

> library(reshape2)
> library(ggplot2)
> df.long<-melt(data)
Using groups as id variables
> ggplot(df.long,aes(x=groups,y=value,fill=variable))+ labs(x = "groups", y = "connections") +
+     geom_bar(stat="identity",position="dodge")

Using my code,I get the X-axis(groups) of bar plot in an alphabetical order(ab,ac,ad,dh).I would like to get the X-axis in an order of my data (rd,ab,dh etc.) What I have to do for that?

amy
  • 33
  • 3
  • @Axeman My question is different because I don't need to order the x-axis based on the frequency. I need to order x-axis based on the data of my groups column. – amy Nov 24 '15 at 08:15

1 Answers1

3

This would solve your problem.

     library(reshape2)
     library(ggplot2)
     df.long<-melt(data)
     df.long$groups <- factor(df.long$groups, levels=unique(df.long$groups))
     ggplot(df.long,aes(x=groups,y=value,fill=variable))+ labs(x = "groups", y = "connections") + geom_bar(stat="identity",position="dodge")
Mr K
  • 446
  • 1
  • 4
  • 16