Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change plot panel in multipanel plot in R [duplicate]

Tags:

plot

r

I want to be able to follow a running simulation with multiple plots in R. The easy way to do it is to create a multi-panel plot (in my case I just use par(mfrow = c(2,2))), and then plot each of the four plots in turn. The problem is that it has to fully redraw the plots each time, and each time the function reaches the last (4th) panel the entire window gets redrawn. What I would like is to be able to shift back to e.g. the first panel and then plot the next points on top of the previous. If this were separate windows I could change between them with dev.set(), but is there something similar for panels?

like image 459
Michael K. Borregaard Avatar asked Jul 01 '13 18:07

Michael K. Borregaard


1 Answers

If you set up the plots to be the correct final size to begin with, you can use par(mfg= to switch between the panels and add to them.

An example:

pars <- c('plt','usr')

par(mfrow=c(2,2))

plot(anscombe$x1, anscombe$y1, type='n')
par1 <- c(list(mfg=c(1,1,2,2)), par(pars))
plot(anscombe$x2, anscombe$y2, type='n')
par2 <- c(list(mfg=c(1,2,2,2)), par(pars))
plot(anscombe$x3, anscombe$y3, type='n')
par3 <- c(list(mfg=c(2,1,2,2)), par(pars))
plot(anscombe$x4, anscombe$y4, type='n')
par4 <- c(list(mfg=c(2,2,2,2)), par(pars))

for( i in 1:11 ) {
    par(par1)
    points(anscombe$x1[i], anscombe$y1[i])
    Sys.sleep(0.5)
    par(par2)
    points(anscombe$x2[i], anscombe$y2[i])
    Sys.sleep(0.5)
    par(par3)
    points(anscombe$x3[i], anscombe$y3[i])
    Sys.sleep(0.5)
    par(par4)
    points(anscombe$x4[i], anscombe$y4[i])
    Sys.sleep(0.5)
}
like image 199
Greg Snow Avatar answered Oct 07 '22 15:10

Greg Snow