Home > database >  Multiple plots are not rendered as described by layout.show
Multiple plots are not rendered as described by layout.show

Time:01-01

Using the raster package and its plot function, I want to arrange six plots of Raster* objects in 2 rows and 3 columns, by columns. Therefore I set up my layout like this:

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

Calling layout.show(6) results in this preview, which is exactly what I want:

enter image description here

However, after the first plot being placed where the 1 is (as expected), the second plot is placed where the 3 is (not as expected). Why's that?

When I then call layout.show(6) again, I get this, which is NOT what I want:

enter image description here

Interestingly, using par(mfcol=c(2,3)) instead of layout also placed the plots by row (i.e. behaved like mfrow).

(I'm aware I could just rearrange the order of plot calls, but I want to know why this doesn't work as I expect it to.)

CodePudding user response:

In the future, please include a self-contained, minimal reproducible example when asking an R question.

You cannot do this with plot(Raster*) as that sets the layout itself; thus overwriting your settings. Instead of plot(x) you could use image(x), but then you do not get a legend.

Perhaps the best approach is to use the terra package instead (terra is the replacement for raster)

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)

enter image description here

  • Related