多个图未按照 layout.show 所述呈现

Multiple plots are not rendered as described by layout.show

使用 raster 包及其 plot 函数,我想将六个 Raster* 对象的图按列排列成 2 行 3 列。因此我这样设置布局:

layout(mat=matrix(1:6, nrow=2, ncol=3), heights=c(1,1), widths=c(2,1,1))

调用 layout.show(6) 结果是这个预览,这正是我想要的:

但是,在将第一个绘图放置在 1 所在的位置(如预期的那样)之后,第二个绘图将放置在 3 的位置(并非如预期的那样)。 这是为什么?

当我再次调用 layout.show(6) 时,我得到了这个,这不是我想要的:

有趣的是,使用 par(mfcol=c(2,3)) 而不是 layout 按行放置图(即表现得像 mfrow)。

(我知道我可以重新安排 plot 调用的顺序,但我想知道为什么这不能像我预期的那样工作。)

您不能使用 plot(Raster*) 执行此操作,因为它会自行设置布局;从而覆盖您的设置。您可以使用 image(x) 而不是 plot(x),但是您不会得到图例。

也许最好的方法是使用 terra 包(terraraster 的替代品)

f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
layout(mat=matrix(1:6, nrow=2, ncol=3), heights=c(1,1), widths=c(2,1,1))
plot(r)
plot(r*2)
plot(r*3)
plot(r*4)
plot(r*5)
plot(r*6)