[R] problem with mfg argument of par

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Mar 17 12:27:14 CET 2007


The problem here is that setting par(mfg =) does an implicit par(new=TRUE) 
on the next plot (as otherwise it would advance to the next position). 
That is going to be confusing if there has been no plot, and that is what 
you are seeing: the first plot is done at c(1,1), overriding the setting 
of par(mfg =).  (The first plot after par(mfrow =) clears the device and 
set up the parameters, _unless_ it is done with new=TRUE.)

Solutions are to use screen() or layout() or do a first empty plot
after par(mfrow = c(2, 2)).  E.g.

par(mfrow = c(2, 2))
plot.new()
pos <- as.matrix(expand.grid(1:2, 1:2))
for (ix in 4:1) {par(mfg = pos[ix, ]); plot(1:ix)}

does what I think you intended.

On Sat, 17 Mar 2007, Ken Knoblauch wrote:

> I'm having a problem with the mfg option of par.  Am I making an error in my
> usage?  Here is a simple example that I thought would plot to the 4 corners
> of a 2x2 plot but doesn't plot to the lower right and plots twice on the
> upper left.
>
> par(mfrow = c(2, 2))
> pos <- 	as.matrix(expand.grid(1:2, 1:2))
> for (ix in 4:1) {
> 	par(mfg = pos[ix, ])
> 	plot(1:5)
> 	}
>
> Thank you in advance.
>
> R version 2.4.1 Patched (2007-01-23 r40561)
> i386-apple-darwin8.8.1
>
> locale:
> C
>
> attached base packages:
> [1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"
> [7] "base"
>
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list