# Re: [R] What determines the order of rows in a lattice barchart?

From: Deepayan Sarkar <deepayan.sarkar_at_gmail.com>
Date: Sat 28 Oct 2006 - 17:29:24 GMT

On 10/28/06, Geoff Russell <geoffrey.russell@gmail.com> wrote:
> Hi,
>
> What determines the order of the rows in a barchart?

That's not a well defined question, as barchart is generic, and methods are free to choose their own order (and much more). See below for an answer for the formula method, which is what you are using.

> Cheers,
> Geoff.
>
> Here is my code, and the data follows. If I have z in alpha order, the
> barchart is in
> some order I can't determine. I originally tried
> rownames(twater)~twater\$Cat, but the
> chart wasn't in rownames(twater) order either.
>
> library(lattice)
> twater\$Cat<-ordered(twater\$Cat,levels=(sort(levels(twater\$Cat))))
> sel<-twater\$Cat=="A"
> nm<-sort(rownames(twater))
> z<-factor(nm,levels=nm)

The order should be levels(z), which in this case is

> nm

```[1] "Agriculture" "Beef"        "Cotton"      "Diary"       "Fruit&Veg"
[6] "Grapes"      "Household"   "Rice"        "Sugar"

```

and that's what I see (after modifying your call, which doesn't work for me).

-Deepayan

> chart<-barchart(z~twater\$Volume,
> xlab="Volume of Water '000 ML",
> main="Water Use In Australia for Food",
> scales=list(col='dark green',
> cex=1.2,
> fontface='bold',
> ),
> panel=function(x,y) {
> panel.barchart(x[!sel],y[!sel],col='blue')
> panel.barchart(x[sel],y[sel],col='red')
> })
> pdf("Rplot-totalwater.pdf",height=4)
> plot(chart)
> dev.off()
>
> Volume, Cat, Source
> Agriculture, 16660, A, WA
> Cotton, 2908, A, WA
> Rice, 1951, A, WA
> Sugar, 1388, A, BA
> Grapes, 729, A, WA
> Fruit&Veg, 1358, A, WA
> Household, 2181, H, WA
> Beef, 3229, B, BA
> Diary, 3542, B, BA
>
> csv data:

R-help@stat.math.ethz.ch 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. Received on Sun Oct 29 04:45:10 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 28 Oct 2006 - 18:30:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.