Re: [R] (Small) problem with barchart

From: Deepayan Sarkar <deepayan.sarkar_at_gmail.com>
Date: Sun, 3 Feb 2008 08:53:44 -0800

On 2/3/08, K. Elo <maillists_at_nic.fi> wrote:
> Hi,
>
> I have a small problem when using barchart. I have the following data:
>
> letters a
> 6 f 18
> 1 a 15
> 10 j 12
> 9 i 12
> 4 d 9
> 5 e 6
>
> The data is from a survey and summaries the alternatives selected in one
> question. The idea is to have a bar chart illustrating the count of
> each selection in descending order. The data frame is already ordered
> in descending order. Thus, the chart _should_ look like this:
> f |xxxxxxxxxxxxxxxxxx
> a |xxxxxxxxxxxxxxx
> j |xxxxxxxxxxxx
> i |xxxxxxxxxxxx
> d |xxxxxxxxx
> e |xxxxxx
>
> But if I use the command:
> > barchart(DATA[[2]] ~ DATA[[1]])
>
> The bars are displayed in alphabetical order.
>
> How could I get the graph I would like to have?

You have the choice of

(1) ordering the levels by their appearance in DATA

(2) ordering them in increasing order of 'a' (irrespective of original order)

For (1), you should use the 'levels' argument of factor(). For (2), the reorder() function is helpful, and more general for your use (but note that it's first argument already has to be a factor).

Assuming that DATA is a data frame and the 'letters' variable is already a factor, you could try

barchart(factor(letters, levels = unique(letters) ~ a, data = DATA)

or

barchart(reorder(letters, a) ~ a, data = DATA)

I would also encourage you to use 'origin = 0', or if you don't like that, use dotplot() instead of barchart().

-Deepayan



R-help_at_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. Received on Sun 03 Feb 2008 - 16:58:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Feb 2008 - 00:30:11 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.

list of date sections of archive