2

Im using the code bellow to plot number of clicks by user. My dataframe is ordered descending:

          name   value
0  xxxx xxxxxxx  215930
1     yyyyyyyyy  176058
2        zzzzzz  143640
3        qqqqqq  101732

but the bars are randomly plotted.

print ggplot(aes(x="name", y="value"), df)  + geom_bar(stat='bar', fill='blue') + theme(axis_text_x  = element_text(angle = 90, hjust = 1)) + ylab("Count") + xlab("User")

enter image description here

How can I make the bars (x axis) be ordered by the value on Y?

Thanks

Diego
  • 34,802
  • 21
  • 91
  • 134
  • By default, the order is lexical. If it was `r`, the answer would be to [reorder factor levels](http://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph), but I'm unsure if that's possible from python. – tonytonov Jul 02 '15 at 16:34
  • Actually, adding `+ scale_x_discrete(limits = c("x", "y", "q", "z"))` would also work, see if you can adapt that without messing with the data. – tonytonov Jul 02 '15 at 16:36
  • thanks, but that presumes you know in advance the order, doesnt it? – Diego Jul 03 '15 at 10:47
  • 1) If `df` is already sorted, then (using R notation) `+ scale_x_discrete(limits = df$name))`, otherwise 2) check the accepted answer I linked, it has `sort` in there. – tonytonov Jul 03 '15 at 10:55

0 Answers0