在R中的多面板绘图中更改绘图面板 [英] Change plot panel in multipanel plot in R

查看:110
本文介绍了在R中的多面板绘图中更改绘图面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在R中使用多个图跟踪运行的模拟.最简单的方法是创建多面板图(在我的情况下,我仅使用par(mfrow = c(2,2))),然后绘制每个图.这四个情节依次出现.问题在于,每次都必须完全重绘绘图,并且每次函数到达最后一个(第4个)面板时,整个窗口都会被重绘.我想要的是能够转移回例如第一个面板,然后在前一个面板的顶部绘制下一个节点.如果这是单独的窗口,则可以使用dev.set()在两个窗口之间进行切换,但是面板是否有类似的东西?

解决方案

如果将图设置为正确的最终最终尺寸,则可以使用par(mfg=在面板之间进行切换并添加到面板中. /p>

一个例子:

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)
}

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?

解决方案

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)
}

这篇关于在R中的多面板绘图中更改绘图面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆