Skip to content

Barplot with Sorted X-Axis

5 messages · Gundala Viswanath, Jim Lemon, Greg Snow

#
Hi,

I have a binned data that looks like this.

  (8.048,18.05] (-21.95,-11.95] (-31.95,-21.95]   (18.05,28.05] (-41.95,-31.95]
             81              76              18              18              12
    (-132,-122]     (-122,-112]     (-112,-102]     (-162,-152]   (-102,-91.95]
              6               6               6               5               5
(-91.95,-81.95]     (-192,-182]   (28.05,38.05]   (38.05,48.05]   (58.05,68.05]
              5               4               4               4               4
  (78.05,88.05]     (98.05,108]     (-562,-552]     (-512,-502]     (-482,-472]
              4               4               3               3               3
    (-452,-442]     (-412,-402]     (-282,-272]     (-152,-142]   (48.05,58.05]
              3               3               3               3               3
  (68.05,78.05]       (118,128]       (128,138]     (-582,-572]     (-552,-542]
              3               3               3               2               2
    (-532,-522]     (-422,-412]     (-392,-382]     (-362,-352]     (-262,-252]
              2               2               2               2               2
    (-252,-242]     (-142,-132] (-81.95,-71.95]       (148,158]   (-1402,-1392]
              2               2               2               2               1
  (-1372,-1362]   (-1342,-1332]     (-942,-932]     (-862,-852]     (-822,-812]
              1               1               1               1               1
    (-712,-702]     (-682,-672]     (-672,-662]     (-632,-622]     (-542,-532]
              1               1               1               1               1
    (-502,-492]     (-492,-482]     (-472,-462]     (-462,-452]     (-442,-432]
              1               1               1               1               1
    (-432,-422]     (-352,-342]     (-332,-322]     (-312,-302]     (-302,-292]
              1               1               1               1               1
    (-202,-192]     (-182,-172]     (-172,-162] (-51.95,-41.95]   (88.05,98.05]
              1               1               1               1               1
      (108,118]       (158,168]       (168,178]       (178,188]       (298,308]
              1               1               1               1               1
      (318,328]       (328,338]       (338,348]       (368,378]       (458,468]
              1               1               1               1               1


How can I plot the data so that the bin is sorted from most negative
in the left to most positive in the right?

Currently my graph look like this:

http://docs.google.com/Doc?id=dcvdrfrh_5cm5qkchw

Notice that it is unsorted at all,
In particular the second bar (value = 76)  id placed on the right  to the first.


  (8.048,18.05] (-21.95,-11.95]
             81              76


This is the command I use to plot:

barplot(x,ylab="Number of Unique Tags", xlab="Expected - Observed")

- Gundala Viswanath
Jakarta - Indonesia
#
Gundala Viswanath wrote:
Hi Gundala,
How about sorting the levels of the data first:

attr(mydata,"levels")<-sort(levels(mydata))

or sorting the columns of the above table if that is what you are using 
to plot.

Jim
#
Hi Jim,
How do you do that? Yes I am using that data exactly for the plotting.

- GV.
#
Can you show us the code used to get the data?

The usual methods that I can think of would have sorted the columns correctly for you.  The fact that this is not the case indicates that you are using a different method, or doing something that looses the information along the way.  If you show us the steps you are taking, then we can help track down where the problem is occurring.
1 day later
#
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