[R] Barplot with Sorted X-Axis

Jim Lemon jim at bitwrit.com.au
Fri Feb 20 10:42:29 CET 2009


Gundala Viswanath wrote:
> ...
> How do you do that? Yes I am using that data exactly for the plotting.
>   

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




More information about the R-help mailing list