6

是否可以在grid包中“强制”R 基图grid.newpage?例如,这很好用:

library(grid)
grid.newpage()
vp1 <- viewport(x=0,y=0.5,width=0.5, height=0.5, just = c("left", "bottom"))
vp2 <- viewport(x=0.5,y=0,width=0.5, height=0.5, just = c("left", "bottom"))
pushViewport(vp1)
grid.rect()
grid.text("vp1", 0.5, 0.5)
upViewport()
pushViewport(vp2)
grid.rect()
grid.text("vp2", 0.5, 0.5)

在此处输入图像描述.

但如果我尝试这样的事情:

grid.newpage()
vp1 <- viewport(x=0,y=0.5,width=0.5, height=0.5, just = c("left", "bottom"))
vp2 <- viewport(x=0.5,y=0,width=0.5, height=0.5, just = c("left", "bottom"))
pushViewport(vp1)
grid.rect()
print(plot(1,2))
grid.text("vp1", 0.5, 0.5)
upViewport()
pushViewport(vp2)
grid.rect()
print(plot(1,2))

R 基础图只是覆盖了grid.newpage. 使用par(new=T)也无济于事。

4

1 回答 1

7

因为没有人回答这个问题,我会自己做。正如安德烈所说,这个问题的答案就在这里。您将需要包中的gridFIG()函数gridBase来绘制 R 基础图,plot.new()而不是grid.newpage()

library(grid)
library(gridBase)
plot.new()
vp1 <- viewport(x=0,y=0.5,width=0.5, height=0.5, just = c("left", "bottom"))
vp2 <- viewport(x=0.5,y=0,width=0.5, height=0.5, just = c("left", "bottom"))
pushViewport(vp1)
grid.rect()
grid.text("vp1", 0.5, 0.5)
par(new=TRUE, fig=gridFIG())
plot(1,2)
upViewport()
pushViewport(vp2)
grid.rect()
grid.text("vp2", 0.5, 0.5)
par(new=TRUE, fig=gridFIG())
plot(1,2)

在此处输入图像描述

于 2013-02-23T10:43:25.870 回答