[R] complex plots using layout()

Paul Murrell p.murrell at auckland.ac.nz
Wed Oct 4 22:25:49 CEST 2006


Hi


Lu, Jiang Jane wrote:
> Dear Dr. Murrell,
> 
> Thank you very much for your suggestion. I have successfully produced
> the ideal plot following your advice. Please see the attached PDF file
> (layouttest.pdf) for results.
> 
> However, I do not understand why the left and right outer margins are so
> big that it occupies almost half of the page. I did not specify any
> outer margins in my program, which I attached to this email at the end.
> Any comment is appreciated.


The blank space is there because the layout has 'respect=TRUE', which
means that a column width of 1 has the same physical size as a row
height of 1 (i.e., the scatterplots are square and, more importantly,
the two marginal histograms are the same "height").  Overall, the layout
is square, but the page is rectangular, so the layout cannot use all of
the page.  Try removing the 'respect=TRUE' (I doubt you'll like the
result).  You could try "partial respect", for example ...

layout(matrix(c(2,0,0,1,3,7,5,0,0,4,6,8), nrow=4, ncol=3,
              byrow=TRUE),
       widths=c(3,1,3), heights=c(1,3,1,3),
       respect=matrix(c(0,1,0,0,0,0,0,1,0,0,0,0), nrow=4, ncol=3,
                      byrow=TRUE))

... (again, untested) just to enforce the histogram heights, but it can
take a bit of experimenting to understand what is going on with partial
respect.

BTW, I think your par(mar)'s are not quite right (the marginal
histograms "stick out" beyond the edges of the scatterplot).  I think
something like ...

par(mar=c(5,5,1,1))
plot(x, y1, xlim=xrange, ylim=y1range, xlab=predtname[1], ylab="MJI")
lines(lowess(x[is.na(x)==FALSE],y1[is.na(x)==FALSE]),col=2,lwd=3)
par(mar=c(0,5,5,1))
barplot(xhist$counts, axes=FALSE, ylim=c(0, top1),
        space=0,main=paste("Correlation Coeffcient = ",y1corr))
par(mar=c(5,0,1,1))
barplot(y1hist$counts, axes=FALSE, xlim=c(0, top1), space=0, horiz=TRUE)

... would be better.

Paul


> =================================================================
> y1 <- demograc$MYOJEOP
> y2 <- log(demograc$ntotles)
> x  <- demograc$agebl
> xc <- demograc$AGECAT
> 
> predtname <- c("Age","Categorical Age")
> prdtlevel <- list(ctnage=0,
> ftage=c("<50","50-59","60-69",">=70"))
> 
> postscript(file="C:/Lu/MyR/Trials/Layout/testlayout.eps",horizontal=TRUE
> )
> 
> 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)
> 
> y1corr <- round(cor(x,y1,use="complete.obs"),2)
> y2corr <- round(cor(x,y2,use="complete.obs"),2)
> 
> def.par <- par(no.readonly = TRUE) # save default, for resetting...
> 
> nf <- layout(matrix(c(2,0,0,1,3,7,5,0,0,4,6,8),nrow=4,ncol=3,
> byrow=TRUE),widths=c(3,1,3),heights=c(1,3,1,3),respect=TRUE)
> 
> 
> par(mar=c(5,5,1,1))
> plot(x, y1, xlim=xrange, ylim=y1range, xlab=predtname[1], ylab="MJI")
> lines(lowess(x[is.na(x)==FALSE],y1[is.na(x)==FALSE]),col=2,lwd=3)
> par(mar=c(0,3,5,1))
> barplot(xhist$counts, axes=FALSE, ylim=c(0, top1),
> space=0,main=paste("Correlation Coeffcient = ",y1corr))
> par(mar=c(3,0,1,1))
> barplot(y1hist$counts, axes=FALSE, xlim=c(0, top1), space=0, horiz=TRUE)
> 
> par(mar=c(5,5,1,1))
> plot(x, y2, xlim=xrange, ylim=y2range, xlab=predtname[1],
> ylab="log(Numer of Lesions)")
> lines(lowess(x[is.na(x)==FALSE],y2[is.na(x)==FALSE]),col=2,lwd=3)
> par(mar=c(0,3,5,1))
> barplot(xhist$counts, axes=FALSE, ylim=c(0, top2),
> space=0,main=paste("Correlation Coeffcient = ",y2corr))
> par(mar=c(3,0,1,1))
> barplot(y2hist$counts, axes=FALSE, xlim=c(0, top2), space=0, horiz=TRUE)
> 
> y1pvalue <- round(anova(lm(y1~factor(xc)))[1,5],3)
> y2pvalue <- round(anova(lm(y2~factor(xc)))[1,5],3)
> 
> par(mar=c(4,5,2,2))
> boxplot(y1~xc,varwidth=TRUE,main=paste("p-value of t test or ANOVA =
> ",y1pvalue),
> 	
> names=prdtlevel[[2]],ylab='MJI',xlab=predtname[2],na.action=na.omit)
> 
> boxplot(y2~xc,varwidth=TRUE,main=paste("p-value of t test or ANOVA =
> ",y2pvalue),
> 				names=prdtlevel[[2]],ylab='log(Number of
> Lesions)',xlab=predtname[2],na.action=na.omit)
> 
> par(def.par)
> 
> dev.off()
> ========================================================================
> 
> 
> Jiang Lu
> 
> BARI 2D Coordinating Center
> University of Pittsburgh
> 130 DeSoto Street, 127 Parran Hall
> Pittsburgh, PA 15261
> Tel: (412)624-5219
> 
> -----Original Message-----
> From: Paul Murrell [mailto:p.murrell at auckland.ac.nz] 
> Sent: Tuesday, October 03, 2006 8:50 PM
> To: Lu, Jiang Jane
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] complex plots using layout()
> 
> Hi
> 
> 
> Lu, Jiang Jane wrote:
>> 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.
> 
> 
> You could just repeat the initial layout four times (untested because I
> could not run your code sample because I do no have 'demog') ...
> 
> layout(matrix(c(2,0,5,0,1,3,4,6,8,0,11,0,7,9,10,12), 4, 4, byrow=TRUE),
>        c(3,1,3,1), c(1,3,1,3), TRUE)
> 
> ... adding extra (zero-ed) columns/rows to allow for empty space between
> plots as necessary.
> 
> Paul
> 
> 
>> =================================================================
>> 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)
>>
>> def.par <- par(no.readonly = 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
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch 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 Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list