[R] multiple plots with par mfg

Henrik Bengtsson hb at stat.berkeley.edu
Wed May 24 02:11:30 CEST 2006

```saveSubplot <- function() {
if (!exists("subplotPars", mode="list"))
subplotPars <<- list();
mfg <- p\$mfg;
key <- mfg[1]*(mfg[3]-1)+mfg[2];
subplotPars[[key]] <<- p;
invisible(key);
}

restoreSubplot <- function(mfg) {
opar <- par();
if (length(mfg) == 2)
mfg <- c(mfg, par("mfg")[3:4]);
key <- mfg[1]*(mfg[3]-1)+mfg[2];
p <- subplotPars[[key]];
# Move 'mfg' last
mfg <- p\$mfg;
p\$mfg <- NULL;
p\$mfg <- mfg;
par(p);
invisible(opar);
}

par(mfrow=c(2,2));
par(lwd=2, pch=19);
plot(rnorm(10), rnorm(10));
saveSubplot();

par(lwd=1, pch=0);
hist(rgamma(1000,3));
saveSubplot();

restoreSubplot(c(1,1));
points(0,0, col="red");

/Henrik

On 5/23/06, Romain Francois <francoisromain at free.fr> wrote:
> Hi,
>
> An other possibility might be to use two devices and use dev.set to go
> from one to another :
>
> x11() # the first device (may be windows() or quartz() depending on you OS)
> plot(1,1, col="blue") # blue plot
> x11() # the second
> plot(1.2,1.2, col="red") # red plot
> points(1.1,1.1) # appears to bottom left of red point
>
> dev.set(dev.prev()) # switch plots
> points(1.1,1.1)
>
> Le 23.05.2006 17:54, Yan Wong a écrit :
> > On 23 May 2006, at 15:57, Greg Snow wrote:
> >
> >
> >> 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.
> >>
> >
> > Yes, I realise that. The problem is that the data are being simulated
> > on the fly, and I wish to display multiple plots which are updated as
> > the simulation progresses. So I do need to return to each plot on
> > every generation of the simulation.
> >
> >
> >> 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
> >> par(mfrow=...)).
> >>
> >> 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:
> >>
> >
> > Aha. I didn't realise that the usr coordinates could be stored and
> > reset using par.
> >
> >
> >> Hope this helps,
> >>
> >
> > I think that's exactly what I need. Thank you very much.
> >
> > Yan
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> >
> >
> >
>
>
> --
> visit the R Graph Gallery : http://addictedtor.free.fr/graphiques
> mixmod 1.7 is released : http://www-math.univ-fcomte.fr/mixmod/index.php
> +---------------------------------------------------------------+
> | Romain FRANCOIS - http://francoisromain.free.fr               |
> | Doctorant INRIA Futurs / EDF                                  |
> +---------------------------------------------------------------+
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help