Dear R-users, I often stack plots that have the same x-axis. To save space and have the plots themselves as large as possible I like to minimize the margins between the plots to zero. I use the "mfrow" and "mar" parameters to achieve this. However, the different margin settings for the individual plots lead to the inner plots being higher than the two outer plots. To make the data in the individual subplots visually comparable, I would like to have all plots with a plotting area of exactly the same height. How would that be done in R? Here's some example code to illustrate my problem: # BEGIN x <- 1:10 a <- runif(10, 0, 5) b <- runif(10, 0, 5) c <- runif(10, 0, 5) ylim <- c(0, 5) par(mfrow=c(3,1)) par(mar=c(0,4.1,2.1,3.1)) plot(x, a, type="o", ylim=ylim, axes=FALSE) axis(1, labels=FALSE) axis(2) axis(3) axis(4) box() par(mar=c(0,4.1,0,3.1)) plot(x, b, type="o", ylim=ylim, axes=FALSE) axis(1, labels=FALSE) axis(2) axis(3, labels=FALSE) axis(4) box() par(mar=c(2.1,4.1,0,3.1)) plot(x, c, type="o", ylim=ylim, axes=FALSE) axis(1) axis(2) axis(3, labels=FALSE) axis(4) box() # END Thanks in advance, Peter
Use outer margins. Try something like:> par(mfrow=c(3,1), mar=c(0,4,0,2)+0.1, oma=c(5,0,3,0)+0.1 )Then do your plots without resetting margins. Also you can use xaxt='n' rather than axes=FALSE to suppress just the x axis and not have to do the y axis and box by hand. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Peter Neuhaus > Sent: Thursday, February 18, 2010 10:11 AM > To: r-help at r-project.org > Subject: [R] problem with multiple plots (mfrow, mar) > > Dear R-users, > > I often stack plots that have the same x-axis. To save space and have > the plots themselves as large as possible I like to minimize the > margins > between the plots to zero. I use the "mfrow" and "mar" parameters to > achieve this. > > However, the different margin settings for the individual plots lead to > the inner plots being higher than the two outer plots. To make the > data in the individual subplots visually comparable, I would like > to have all plots with a plotting area of exactly the same height. > > How would that be done in R? > > Here's some example code to illustrate my problem: > > # BEGIN > x <- 1:10 > a <- runif(10, 0, 5) > b <- runif(10, 0, 5) > c <- runif(10, 0, 5) > > ylim <- c(0, 5) > > par(mfrow=c(3,1)) > par(mar=c(0,4.1,2.1,3.1)) > > plot(x, a, type="o", ylim=ylim, axes=FALSE) > axis(1, labels=FALSE) > axis(2) > axis(3) > axis(4) > box() > > par(mar=c(0,4.1,0,3.1)) > > plot(x, b, type="o", ylim=ylim, axes=FALSE) > axis(1, labels=FALSE) > axis(2) > axis(3, labels=FALSE) > axis(4) > box() > > par(mar=c(2.1,4.1,0,3.1)) > > plot(x, c, type="o", ylim=ylim, axes=FALSE) > axis(1) > axis(2) > axis(3, labels=FALSE) > axis(4) > box() > # END > > Thanks in advance, > > Peter > > ______________________________________________ > 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.
On 02/19/2010 04:10 AM, Peter Neuhaus wrote:> Dear R-users, > > I often stack plots that have the same x-axis. To save space and have > the plots themselves as large as possible I like to minimize the margins > between the plots to zero. I use the "mfrow" and "mar" parameters to > achieve this. > > However, the different margin settings for the individual plots lead to > the inner plots being higher than the two outer plots. To make the > data in the individual subplots visually comparable, I would like > to have all plots with a plotting area of exactly the same height. >Hi Peter, The two par arguments "fin" and "pin" allow a solution. What you want is for the second values in "pin" (Plot dimensions in INches) to be the same for all your plots. You can get an approximation by using the layout function instead of mfrow and setting the height vector to correct for the space used in the top and bottom plots. If you are not doing lots of these plots or you are doing only a few variations, you can just print out par("pin") after each plot and see how much they differ and adjust the height vector until all "pin"s are the same. Try setting the height vector in layout to the "fin"s (Figure dimensions in INches) that are printed out using the default of all heights equal. Jim