[R] Order in barchart

Deepayan Sarkar deepayan at stat.wisc.edu
Tue Feb 3 17:52:26 CET 2004


On Tuesday 03 February 2004 01:26, Prof Brian Ripley wrote:

> I think you misunderstand what barchart does.  It expects a formula, not a
> named vector, as its first argument.
>
> nm <- names(T3)
> z <- factor(nm, levels=nm)
> barchart(z ~ T3, xlab="Mean Values", col="dark blue")
>
> is probably what you are looking for.  The factor z is constructed to be
> in the order you want.
>
> That barchart() accepts a named vector appears to be undocumented.  


This is true in S-PLUS as well (AFAIK), but seemed to be a common enough 
usage. I wasn't able to fiure out exactly what S-PLUS does, but what lattice 
does is the following:

step 1. A call like barchart(x) is converted to barchart( ~ x)

step 2. If x is named, the missing y is generated by y = names(x).
        If x is not named, y = rep('', length(x))

I'll add this information to the documentation.


Anyway, the rest of the explanation is the usual one of factor levels being 
ordered alphabetically by default, which you would have to override as 
indicated by Prof Ripley.

Deepayan



> unnamed vector it does not behave similarly to barplot().
>
> On Mon, 2 Feb 2004, Bliese, Paul D MAJ USAMH wrote:
> > Sorry if this is a FAQ -- I checked the archives and help files, but was
> > stumped.
> >
> > In the data T3 (provided below) the values are sorted from lowest to
> > highest.
> >
> > > T3
> >
> >       10        7       19       13        5        3       15       18
> > 2       24
> > 2.650568 2.666237 2.731649 2.749221 2.777130 2.801124 2.804472 2.813891
> > 2.838316 2.839654
> >       36       20       25       32       26       12       14       29
> > 23        6
> > 2.843043 2.868335 2.882906 2.896539 2.922535 2.931397 2.939590 2.944353
> > 2.983473 3.015235
> >       39        4       17        9       30       21       33       22
> > 8       35
> > 3.017590 3.020495 3.038758 3.066808 3.084511 3.086072 3.106873 3.127783
> > 3.167053 3.173310
> >       28       16       37        1       31       40       27       11
> > 34       38
> > 3.173323 3.221106 3.236643 3.274417 3.274772 3.283696 3.307872 3.355327
> > 3.382744 3.498301
> >
> >
> > If I use barchart in the trellis library, the bars are arranged by factor
> > order of the labels (1,10,11,12,etc.).
> >
> > > barchart(T3,xlab="Mean Values",col="dark blue")
> >
> > Would it be possible to have the bars arranged from highest to lowest in
> > terms of the values (2.65, 2.66, 2.73, etc.) as it is done in barplot?
> >
> > >barplot(T3,xlab="Mean Values",col="dark blue",horiz=T,cex.names=.5)
> >
> > I would like to use barchart instead of barplot because of some of the
> > additional features of the lattice library.
> >
> > Thanks,
> >
> > Paul




More information about the R-help mailing list