Skip to content
Prev 171147 / 398502 Next

Barplot with Sorted X-Axis

Gundala Viswanath wrote:
Say you have this data frame (I'm too lazy to type in your example):

testdat<-cut(rnorm(100,3),breaks=0:6)

get a table:

testtable<-table(testdat)

testtable
(0,1] (1,2] (2,3] (3,4] (4,5] (5,6]
    3     6    38    34    17     2

now mess up the order of the table:

testtable<-testtable[sample(1:6,6)]
testtable
(4,5] (5,6] (2,3] (3,4] (0,1] (1,2]
   17     2    38    34     3     6

If you send this to barplot, you will get the bars in the new order of 
the table elements. If you sort the unsorted table and then send it to 
barplot:

testtable<-testtable[sort(names(testtable))]

You should get what you want.

Jim