[R] (Small) problem with barchart

G. Jay Kerns gkerns at ysu.edu
Sun Feb 3 23:01:20 CET 2008


Dear Kimmo,

It doesn't appear that anyone has yet mentioned pareto.chart() in the
qcc package; it may serve your purposes.  Please note that it does not
require the data frame to be ordered beforehand.

x <- DATA[[2]]
names(x) <- DATA[[1]]

library(qcc)
pareto.chart(x)

Best wishes,
Jay



On Feb 3, 2008 11:53 AM, Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote:
>
> 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.
>



-- 



***************************************************
G. Jay Kerns, Ph.D.
Assistant Professor / Statistics Coordinator
Department of Mathematics & Statistics
Youngstown State University
Youngstown, OH 44555-0002 USA
Office: 1035 Cushwa Hall
Phone: (330) 941-3310 Office (voice mail)
-3302 Department
-3170 FAX
E-mail: gkerns at ysu.edu
http://www.cc.ysu.edu/~gjkerns/



More information about the R-help mailing list