[R] Boxplot across levels of a factor
Prof Brian Ripley
ripley at stats.ox.ac.uk
Tue Aug 24 09:03:58 CEST 2004
On Tue, 24 Aug 2004, Ivar Herfindal wrote:
> I think Paul wanted one plot for each box, not all boxes in one plot (sorry
> if I misunderstand).
>
> One way to solve this can be like this:
>
> par(mfrow=c(1,3))
> with(data, by(1:nrow(data), Type, function(x) boxplot(Locus[x]~data[x]))
Actually, that does not come close to working. Did you try it?
(Please read the documentation for by().) The following does work
by(data, Type, function(x) boxplot(x$Locus,
main=paste("Type", unique(x$Type))))
but that would be a very unusual plot, and one in which the three plots
had different scalings so the boxplots could not be compared.
For something like that, lattice's bwplot is a better option.
bwplot(Locus ~ Type, data=data) 1 panel
bwplot(Locus ~ factor(rep(1, 182)) | Type, data=data) 3 panels
>
> Hope this works for you.
>
> Ivar
>
> On Tue, 24 Aug 2004 07:27:57 +0100 (BST), Prof Brian Ripley
> <ripley at stats.ox.ac.uk> wrote:
>
> > On Tue, 24 Aug 2004, Paul Boutros wrote:
> >
> >> Hello,
> >>
> >> I have a data-frame in which one-column is a factor:
> >>
> >> > str(data);
> >> `data.frame': 194 obs. of 8 variables:
> >> $ Type : Factor w/ 3 levels "Nuclear-Rec..",..: 1 2 2 2 2 2 2 2
> >> 2 2
> >> ...
> >> $ Locus : num 0.000571 0.004000 0.001429 0.004857 0.007429 ...
> >>
> >> And I'd like to make a boxplot of the data$Locus values, where each
> >> level of
> >> the factor gets its own box-and-whiskers plot. I'm weak in R, but I
> >> thought
> >> there might be some shortcut to automating this instead of just creating
> >> a
> >> new data-structure with all the separate values?
> >
> > There are two. The simpler is
> >
> > boxplot(Locus ~ Type, data=data)
> >
> > and you can also use
> >
> > with(data, boxplot(split(Locus, Type)))
> >
> > (split() does automate the construction of a suitable data structure.)
> >
>
>
>
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list