The short answer to your query is ?reorder

The longer answer (or a longer answer) gets into a bit of philosophy (so feel 
free to go back to the short answer and skip this if you don't want to get into 
the philosophy, you have been warned).  Let's start with the question: is the 
order of the bars a property of the graph/analysis? The dataset as a whole? Or 
the individual variable?

Some programs take the first approach, the order is a property of the graph or 
analysis, these programs have you specify things like the order at the time of 
creating the graph or doing the analysis.  I don't like this approach because 
it seems to me that this should be more inherent to the data than the output, 
also this means that you have to keep specifying the order for every 
graph/analysis and I am too lazy to like that.

Your attempt was number 2, the order should be a property of the dataset.  For 
your example this makes a lot of sense, you want x ordered by y.  But this does 
not generalize to some other situations, so is not the standard for R.

My favorite (and what is used in R, so apparently I'm not the only one) is to 
have things like the order be a property of the individual variable (x in this 
case).  Your data frame will have x as a factor by default (and even if you 
specifically told R to not convert it to a factor, then many plotting/analysis 
functions will still convert it to a factor).  If you don't specify the order 
of the factor levels then the default is to do it alphabetically and this will 
be used by plotting/analysis functions regardless of the order in the data set. 
 One of the easier ways to change the order of the factor levels, especially 
for a case like yours is using the reoder function, try something like:

> xy$x <- reorder( xy$x, xy$y )

You won't see any obvious differences when you print the data frame, but if you 
print just x then you should and your plots should come out the way that you 
want now.

You did ask "why?" and were warned.  Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


> -----Original Message-----
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
> project.org] On Behalf Of Tim Clark
> Sent: Wednesday, May 05, 2010 8:57 PM
> To: r-help@r-project.org
> Subject: [R] bar order using lattice barchart()
> 
> 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
> 
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to