0

How do I get the barchart in qplot to be arranged according to frequency of occurrence (x-axis) instead of according to the alphabetical order of the term (y-axis)?

# create a table to store the top 50 most frequent words and plot a barchart
word_freqs = sort(rowSums(tdm), decreasing=TRUE)
top50words <- head(word_freqs, 50)
termFrequency <- rowSums(as.matrix(top50words))
qplot(names(termFrequency), termFrequency, geom="bar", xlab="Terms") + coord_flip()

top50words arranges the terms in descending order already (from most number of occurrences to least number of occurrences. However, in qplot, it is arranged according to alphabetical order instead.

Outrigger
  • 149
  • 1
  • 3
  • 10
  • See http://stackoverflow.com/questions/3253641/how-to-change-the-order-of-a-discrete-x-scale-in-ggplot and http://stackoverflow.com/questions/8713462/ggplot2-change-order-of-display-of-a-factor-variable-on-an-axis – SchaunW Jun 07 '13 at 02:15
  • It's always best to provide a reproducible example (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This is also a very common question here (reordering barplots). In short, you need to make your x-axis term a factor that is ordered in the way you want it. – alexwhan Jun 07 '13 at 02:16
  • See this blog post for when the words `plot` and `arrange` are used in the same sentence: http://trinkerrstuff.wordpress.com/2012/10/15/how-do-i-re-arrange-ordering-a-plot/ – Tyler Rinker Jun 07 '13 at 02:19
  • The behavior you are describing suggests that alpha-sorting of factor levels are the problem. It's a bit hard (for me anyway) to guess which object/objects needs to be reordered here ... but with an example it would be pretty straightforward to examine structures and to test solutions. – IRTFM Jun 07 '13 at 02:20

0 Answers0