I would like to create R plots within four regions with a layout specified by grid. Furthermore, gridBase should be used to actually plot in the regions using base graphics. Below is an example. The problem is that the 4 plots are not plotted in the four different regions, they are rather overlaid.
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
This seems to work: