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

Deepayan Sarkar deepayan.sarkar at gmail.com
Sat Oct 28 19:29:24 CEST 2006


On 10/28/06, Geoff Russell <geoffrey.russell at 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<-read.csv("totalwater.csv",strip.white=T)
> 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:



More information about the R-help mailing list