[R] How to add a common title (or xlab, ylab) for multi-plots in the same window?
Martin Maechler
maechler at stat.math.ethz.ch
Tue Aug 3 11:31:42 CEST 2004
>>>>> "Sundar" == Sundar Dorai-Raj <sundar.dorai-raj at pdf.com>
>>>>> on Mon, 02 Aug 2004 12:53:43 -0500 writes:
Sundar> F Duan wrote:
>> I am using par(mfrow=c()) to plot multi-figures in the
>> same window. And I like to put a common title (and xlab,
>> ylab) for all of plots. I have already left some margin
>> by resetting omi values in par() and hided all (xlab,
>> ylab) for each sub-plot. Could anyone tell me how to do that?
>> Thanks a lot,
>>
>> Frank
Sundar> ?mtext allows this if outer = TRUE.
and others have mentioned the same.
I'm not answering the "(or xlab, ylab)" part in the original
question, something which *is* answered by ``use mtext(..)''.
Because it such a common task to want an overall title for
"multi-plots", and furthermore, because in many typical
situations, the default "spacing" {par("mar") etc} around the
individual plots is too large (i.e. the plots get smaller than
necessary with their white space margins using too much of your
device's real estate), I had written a small utility function,
mult.fig(), many years ago {in 1990! for S-plus}, and made it
available in the 'sfsmisc' package for a while now.
The start of its help page is :
>> mult.fig package:sfsmisc R Documentation
>>
>> Plot Setup for MULTiple FIGures, incl. Main Title
>>
>> Description:
>>
>> Easy Setup for plotting multiple figures (in a rectangular layout)
>> on one page. It allows to specify a main title and uses _smart_
>> defaults for several 'par' calls.
a typical use after
install.packages("sfsmisc")
library(sfsmisc)
is simply
mult.fig(<#{plots}>, main = <overall title>)
such as the one in example(mult.fig) :
mult.fig(5, main= expression("Sine Functions " * sin(n * pi * x)))
x <- seq(0, 1, len = 201)
for (n in 1:5)
plot(x, sin(n * pi * x), ylab ="", main = paste("n = ",n))
par(old.par)
[BTW: The function has one really ugly feature (that I've unfortunately
not eliminated long time ago and now it would break too
much code): It defines the *GLOBAL* variable 'old.par'
as seen in the above example.
Good programming practice would use the return value of
mult.fig() (which *does* contain old.par!)
Yuck! - what a bad programming style I was using 14 years ago....
]
Martin Maechler
More information about the R-help
mailing list