Re: [R] problem getting multiple densityplots on one page

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Fri, 01 Feb 2008 12:26:42 +0000

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@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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
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 Fri 01 Feb 2008 - 12:36:42 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 Fri 01 Feb 2008 - 13:30:10 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