[R] complex plots using layout()

Lu, Jiang Jane LuJ at edc.pitt.edu
Thu Sep 28 22:55:06 CEST 2006

```Dear r-help,

I am trying to plot several scatter plots with marginal histograms on
one page. Ideally, a page is equally divided into 4 figure regions.
Within each figure region, a scatter plot with marginal histograms will
be plotted.

I followed Dr. Paul Murrell's code released online to successfully plot
the scatter plot with marginal histograms. The code applies "layout()"
to partition the page.

Right now, I want each of the 4 figure regions on one page to be plotted
a scatter plot with marginal histograms. I tried par(mfrow= ) ahead of
layout(). It does not work. Could I repeat layout() to reach my point?

Following is the code I use. Any advice is greatly appreciated.

=================================================================
x <- demog\$age
y1 <- demog\$mji
y2 <- demog\$nles
xhist <- hist(x,  plot=FALSE)
y1hist <- hist(y1,  plot=FALSE)
y2hist <- hist(y2,  plot=FALSE)

top1 <- max(c(xhist\$counts, y1hist\$counts))
top2 <- max(c(xhist\$counts, y2hist\$counts))

xrange <- range(x,na.rm=TRUE)
y1range <- range(y1,na.rm=TRUE)
y2range <- range(y2,na.rm=TRUE)

par(mfrow=c(2,2))

nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE)

par(mar=c(3,3,1,1))
plot(x, y1, xlim=xrange, ylim=y1range, xlab="Age", ylab="MJI")
lines(lowess(x,y1),col=2)
par(mar=c(0,3,1,1))
barplot(xhist\$counts, axes=FALSE, ylim=c(0, top1), space=0)
par(mar=c(3,0,1,1))
barplot(y1hist\$counts, axes=FALSE, xlim=c(0, top1), space=0, horiz=TRUE)

par(mar=c(3,3,1,1))
plot(x, y2, xlim=xrange, ylim=y2range, xlab="Age", ylab="Numer of
Lesions")
lines(lowess(x,y2),col=2)
par(mar=c(0,3,1,1))
barplot(xhist\$counts, axes=FALSE, ylim=c(0, top2), space=0)
par(mar=c(3,0,1,1))
barplot(y2hist\$counts, axes=FALSE, xlim=c(0, top2), space=0, horiz=TRUE)

par(def.par)

================================================================

Sincerely yours,

Jiang Lu

Statistician

University of Pittsburgh
130 DeSoto Street, 127 Parran Hall
Pittsburgh, PA 15261
USA

```