我刚刚尝试了我的第一步grid
。我想设置一个 2 x 2 方形散点图矩阵,中间有一些空间。为了获得空间,我实际上使用了 3 x 3 布局(问题 1:有没有更简单的方法?)。从下面的示例中可以看出,这些点绘制在边界矩形之外。我必须以某种方式指定情节中的限制。问题2:如何做到这一点?最后,我可以使用基本图形来创建绘图吗?[我知道lattice
图形会起作用,并且也layout
可以使用标准(或ggplot2
),但我很感兴趣,如果这也可能的话grid.layout
]
require(grid)
## generate data to be plotted in the top left plot
X <- matrix(rexp(2000), ncol=2)
## plot device
file <- "foo.pdf"
pdf(file=file, width=10, height=10)
## set up grid.layout
gl <- grid.layout(3, 3, respect=rbind(c(0,1,0), c(1,1,1), c(0,1,0)),
widths=unit(c(3,1,3), "inches"), heights=unit(c(3,1,3), "inches")) # define grid layout
pushViewport(viewport(layout=gl)) # use this layout in a viewport
## (1,1) plot
pushViewport(viewport(layout.pos.row=1, layout.pos.col=1, name="11"))
grid.points(X[,1], X[,2], pch=1) # points
grid.rect() # bounding rectangle
grid.xaxis() # x-axis
grid.yaxis() # y-axis
grid.text(expression(italic(X[1])), y=unit(-3, "lines")) # x-axis label
grid.text(expression(italic(X[2])), x=unit(-3, "lines"), rot=90) # y-axis label
grid.text("Plot 1", x=0.86, y=0.9, gp=gpar(fontface="bold", cex=1.6)) # add label
upViewport()
## (1,2) plot
pushViewport(viewport(layout.pos.row=1, layout.pos.col=3, name="13"))
grid.rect()
upViewport()
## (2,1) plot
pushViewport(viewport(layout.pos.row=3, layout.pos.col=1, name="31"))
grid.rect()
upViewport()
## (2,2) plot
pushViewport(viewport(layout.pos.row=3, layout.pos.col=3, name="33"))
grid.rect()
upViewport()
## plot device
dev.off()