我想在四个区域内创建R图,其中布局由网格指定。此外,gridBase应该用于使用基本图形在区域中实际绘图。以下是一个例子。问题是4个地块没有绘制在四个不同的区域,它们相当重叠。
require(grid)
require(gridBase)
## set up the grid layout
gl <- grid.layout(2, 2, widths=unit(c(3, 3), "inches"),
heights=unit(c(3, 3), "inches"))
pushViewport(viewport(layout=gl)) # use this layout in a viewport
## plot
par. <- par(no.readonly=TRUE) # save plot settings
vps <- baseViewports() # create list of grid viewports corresponding to inner, figure, and plot regions
pushViewport(vps$inner, vps$figure, vps$plot)
for(i in 1:2) { # rows
for(j in 1:2) { # columns
pushViewport(viewport(layout.pos.row=i, layout.pos.col=j))
par(plt=gridPLT(), new=TRUE)
x <- 1:10
plot(x, x+runif(10), type="b")
upViewport()
}
}
par(par.) # reset plot settings
答案 0 :(得分:0)
这似乎有效:
require(grid)
require(gridBase)
gl <- grid.layout(2, 2, widths=unit(c(3, 3), "inches"),
heights=unit(c(3, 3), "inches"))
pushViewport(viewport(layout=gl))
par. <- par(no.readonly=TRUE)
for(i in 1:2) {
for(j in 1:2) {
pushViewport(viewport(layout.pos.row=i, layout.pos.col=j))
par(plt=gridPLT())
x <- 1:10
plot(x, x+runif(10), type="b", xlab="", ylab="")
par(new=TRUE) # to be run after first plot
upViewport()
}
}
par(par.)