[R] problem getting multiple densityplots on one page

Gavin Simpson gavin.simpson at ucl.ac.uk
Fri Feb 1 13:26:42 CET 2008


hits=-2.6 tests=BAYES_00
X-USF-Spam-Flag: NO

On Fri, 2008-02-01 at 14:00 +0200, Rainer M Krug wrote:
> On 01/02/2008, Ng Stanley <stanleyngkl at gmail.com> wrote:
> >
> > Hi,
> >
> > I used the following statements to generate unsuccessfully a 5 by 5
> > multiple
> > densityplots on a single page. If I use plot, the whole thing works.
> >
> > > data <- matrix(rnorm(25), 5, 5)
> > > op <- par(mfrow = c(5, 5))
> > > for (x in 1:5) {par(new=TRUE);densityplot(data[,x], xlim=c(.......))}
> 
> 
> See changes in line above. par(new=TRUE) to overplot and xlim() to have
> same  x limits.  The y-limits can be set the same way if necessary.
> untested!
> Rainer

That can't work as densityplot() is a lattice function and thus i) needs
to be wrapped in print( densityplot() ) when in a for loop or similar,
and ii) you can't mix base graphics layout with lattice.

One way to do this is to stack the data one on another and add an ID
variable. stack() does this for you, but there are others or you can
cook it yourself.

dat <- data.frame(matrix(rnorm(25), 5, 5))
dat
sdat <- stack(dat) ## generates variables 'values' and 'ind'
densityplot(~ values | ind, data = sdat)

HTH

G

> 
> > par(op)
> >
> > Thanks
> > Stanley
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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.
> >
> 
> 
> 
> -- 
> 
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list