[R] bar order using lattice barchart()

Tim Clark mudiver1200 at yahoo.com
Thu May 6 04:57:15 CEST 2010


Dear List,

I am want to plot my data in increasing order using the lattice barchart() function.  I used order() to put my data in the order I want, but when I plot it I get the original order of the data.  I think this has to do with the row index number since order() does not re-number the rows in the new order but instead keeps the original row numbers and puts them in a different order.  For example:

xy<-data.frame(x=letters[1:5],y=c(3,9,2,1,10))

#This produces a dataframe in alphebitical order with row numbers in
#numerical order.
> xy
  x         y
1 a  8.921657
2 b 10.314625
3 c  9.531537
4 d 10.818563
5 e  9.084872


#If I re-order the data based on the value of y

xy<-xy[order(y),]

#I get a dataframe ordered by y but the row numbers are still in #alphebetical order based on x
> xy
  x         y
1 a  8.921657
5 e  9.084872
3 c  9.531537
2 b 10.314625
4 d 10.818563

#I then try to plot the data and it plots it in alphabetical instead of
#numeric order
library(lattice)
barchart(y~x, data=xy)



Why are the rows not re-indexed, and how do I get barchart() to plot my data in increasing numeric order?

Thanks,

Tim




Tim Clark
Department of Zoology 
University of Hawaii



More information about the R-help mailing list