[R] multiple plots per page using hist and pdf

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Feb 27 17:45:44 CET 2008


On Wed, 2008-02-27 at 11:31 -0500, Ben Tupper wrote:
> Hello,
> 
> I am puzzled by the behavior of hist() when generating multiple plots  
> per page on the pdf device. In the following example two pdf files  
> are generated. The first results in 4 plots on one pdf page as  
> expected.  However, the second, which swaps one of the plot() calls  
> for hist(), results in a 4 page pdf with one plot per page.
> 
> How might I get the histogram with 3 other scatter plots onto a  
> single pdf page?

Look a bit more closely and you'll see what is wrong ;-)

In the second example, you forgot the par(mfrow=c(2,2)) bit, so of
course there was no split plotting region.

Note that the par(mfrow=c(2,2)) bit in the first version (with all
plot() calls) pertains to the pdf device you just opened, it doesn't
persist as you closed that device ofter plotting with dev.off(). In the
second example you need to change the par again to what you require.

And as a result, your orig.par and par(orig.par) are irrelevant here as
you didn't change any device that was open when you reset the parameters
using par(orig.par).

This is how your second example should have been called:

pdf(file = "hist_and_plots.pdf")
## set up the new plotting device (pdf)
par(mfrow = c(2,2))
## draw the plot
hist(iris$Sepal.Length, main = "Plot 1")
plot(iris$Petal.Length, iris$Petal.Width, main = "Plot 2")
plot(iris$Sepal.Length, iris$Petal.Length, main = "Plot 3")
plot(iris$Sepal.Width, iris$Petal.Width, main = "Plot 4")
## close the device to do the drawing
dev.off()

HTH

G

> 
> platform       powerpc-apple-darwin8.10.1
> version.string R version 2.6.1 (2007-11-26)
> 
> Thanks!
> Ben
> 
> ###BEGIN
> data(iris)
> 
> orig.par = par(no.readonly = TRUE)
> 
> pdf(file = "just_plots.pdf")
> 
> par(mfrow=c(2,2))
> 
> plot(iris$Sepal.Length, iris$Sepal.Width, main = "Plot 1")
> plot(iris$Petal.Length, iris$Petal.Width, main = "Plot 2")
> plot(iris$Sepal.Length, iris$Petal.Length, main = "Plot 3")
> plot(iris$Sepal.Width, iris$Petal.Width, main = "Plot 4")
> 
> dev.off()
> 
> pdf(file = "hist_and_plots.pdf")
> 
> hist(iris$Sepal.Length, main = "Plot 1")
> plot(iris$Petal.Length, iris$Petal.Width, main = "Plot 2")
> plot(iris$Sepal.Length, iris$Petal.Length, main = "Plot 3")
> plot(iris$Sepal.Width, iris$Petal.Width, main = "Plot 4")
> 
> dev.off()
> 
> 
> par(orig.par)
> ###END
> 
> Ben Tupper
> PemaquidRiver at tidewater.net
> 
> I GoodSearch for Ashwood Waldorf School.
> 
> Raise money for your favorite charity or school just by searching the  
> Internet with GoodSearch - www.goodsearch.com - powered by Yahoo!
> 
> ______________________________________________
> 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