Re: Jump between figures in multifigure environment

Paul Murrell via R-help <[email protected]>
Newsgroups gmane.comp.lang.r.general
Message-ID <[email protected]>
Hi

Base graphics is designed for drawing everything in a figure before 
moving on to the next figure.  Also, 'mfg' can only be set via par().
Are you looking for something like this ... ?


par(mfrow = c(2, 2))
for (i in 1:2) {
     for (j in 1:2) {
         par(mfg = c(i, j))
         plot(c(0, 1), c(0, 1), type = "n")
         text(0.5, 0.5, labels = paste("(", i, ",", j, ")"))
     }
}


If you really want to revisit previous figures, you might try 
split.screen(), but it is pretty fragile and, as the example below 
shows, still not designed for revisiting a previous *plot* (just a 
previous figure region) ...


split.screen(c(2, 2))
for (i in 1:4) {
     screen(i)
     plot(c(0, 1), c(0, 1), type = "n")
}
for (i in 1:2) {
     for (j in 1:2) {
         screen(2*(i - 1) + j)
         text(0.5, 0.5, labels = paste("(", i, ",", j, ")"))
     }
}


If you REALLY want to revisit previous drawing, you might need to look 
at {grid} viewports, which are designed to allow you to get back to the 
original drawing context ...


library(grid)
grid.newpage()
pushViewport(viewport(layout=grid.layout(2, 2)))
for (i in 1:2) {
     for (j in 1:2) {
         pushViewport(viewport(layout.pos.row=i, layout.pos.col=j,
                               name=paste0(i, j)))
         grid.rect(width=.8, height=.8)
         upViewport()
     }
}
for (i in 1:2) {
     for (j in 1:2) {
         downViewport(paste0(i, j))
         grid.text(paste("(", i, ",", j, ")"))
         upViewport()
     }
}


Paul


On 13/11/25 07:28, Naresh Gurbuxani wrote:
> After drawing multiple figures, I want to select an earlier figure to add some low level commands.  Using mfg parameter runs all commands in the most recent figure only.  How can an earlier figure be selected?
> 
> Thanks,
> Naresh
> 
> par(mfrow = c(2, 2))
> for (i in 1:4) plot(c(0, 1), c(0, 1), type = "n”)
> for (i in 1:2) {for (j in 1:2) text(0.5, 0.5, labels = paste("(", i, ",", j, ")"), mfg = c(i, j, 2, 2))}
> #All text is in bottom right figure only
> ______________________________________________
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Dr Paul Murrell (he/him)
Te Kura Tatauranga | Department of Statistics
Waipapa Taumata Rau | The University of Auckland
Private Bag 92019, Auckland 1142, New Zealand
64 9 3737599 x85392
[email protected]
www.stat.auckland.ac.nz/~paul/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.