[R] multiple plots with par mfg

Greg Snow Greg.Snow at intermountainmail.org
Tue May 23 16:57:13 CEST 2006

The  following works for my quick tests, but being undocumented it is
not guarenteed to work for all situations.

The best thing to do is to create the first plot, add everything to the
first plot that you need to, then go on to the 2nd plot, etc.  If you
really need to go back to the first plot to add things after plotting
the 2nd plot then here are a couple of ideas:

Look at the examples for the cnvrt.coords function in the TeachingDemos
package (my quick test showed they work with layout as well as

The other option is when you use par(mfg) to go back to a previous plot
you also need to reset the usr coordinates, for example:

> par(mfrow=c(2,2))
> plot(rnorm(10), rnorm(10)
> tmp <- par('usr')
> hist(rgamma(1000,3)) # changes coordinate system
> par(mfg=c(1,1)) # go back to first plot
> points(0,0, col='red')  # wrong place, based on hist coordinate system
> par(usr=tmp)  # reset coordinates to correct values
> points(0,0, col='blue')  # now it is in the right place

Hope this helps, 

Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Yan Wong
Sent: Tuesday, May 23, 2006 6:05 AM
To: R-help
Subject: Re: [R] multiple plots with par mfg

On 23 May 2006, at 12:48, Prof Brian Ripley wrote:

> On Tue, 23 May 2006, Yan Wong wrote:
>> if not, can anyone suggest a way of appending to 2 separate plots on 
>> the fly.
> No, it is user error.  par(mfg=) specifies where the next figure will 
> the drawn, and points() does not draw a figure but adds to one. As the

> help page says:
>      'mfg' A numerical vector of the form 'c(i, j)' where 'i' and 'j'
>           indicate which figure in an array of figures is to be drawn
>           next (if setting) or is being drawn (if enquiring).
>           ^^^^

OK. I didn't appreciate the distinction between drawing and adding.

> You need to use screen() or layout() to switch back to an existing 
> plot.

Thanks, but the help page for screen says "The behavior associated with
returning to a screen to add to an existing plot is unpredictable and
may result in problems that are not readily visible." I assume this to
mean that I shouldn't do it using screen().

I can't find any description of how to add points to several different
plots generated after a layout() call. Is there a way?



R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list