BioMan
BioMan

Reputation: 704

Reorder geom_bar based on value-column

I am trying to reorder my bar_plot from highest to lowest value. What is wrong with my code?

 head(summary.m)
   Var1   Var2 value
2   u-A Length  1155
3  u-AA Length   422
4 u-AAA Length   119
5 u-AAC Length     6
6 u-AAG Length    19
7 u-AAT Length    49

ggplot(summary.m, aes(x=reorder(Var1,-value),y=value)) + geom_bar(stat="identity") +
  theme_bw(base_size=8) +
  theme(axis.text.x = element_text(colour = "black",angle=90 ))

Upvotes: 1

Views: 275

Answers (1)

Mamoun Benghezal
Mamoun Benghezal

Reputation: 5314

you just need to specify the function used in reorder. Just try this

ggplot(summary.m, aes(x=reorder(Var1,value, FUN=sum),y=value)) + geom_bar(stat="identity") +
  theme_bw(base_size=8) +
  theme(axis.text.x = element_text(colour = "black",angle=90 ))

Note : your code works fine for me, so I have inverted the order.

Upvotes: 2

Related Questions