Bratt Swan
Bratt Swan

Reputation: 1146

How to use layout() function in R?

I just took an example which produces four plots combined with the layout function. However, I cannot figure out how the matrix inside layout() connects to the layout of these plots.

layout(matrix(c(1, 1, 1,
                2, 3, 4,
                2, 3, 4), nr=3, byrow=T))
hist(rnorm(25), col="VioletRed")
hist(rnorm(25), col="VioletRed")
hist(rnorm(25), col="VioletRed") 
hist(rnorm(25), col="VioletRed")

Upvotes: 16

Views: 39706

Answers (1)

jbaums
jbaums

Reputation: 27388

For your example, the graphics device is split into a 3 x 3-cell grid, with columns/rows having equal width/height (since that is the default behaviour when you don't provide widths and heights arguments).

After calling layout, the first subsequent plot will fill the cells for which the matrix has value 1 (i.e., the top three cells). The second plot will fill the cells for which the matrix has value 2 (bottom-left and middle-left cells), and so on.

To get a preview of the ensuing layout, you can use layout.show:

layout(matrix(c(1, 1, 1,
                2, 3, 4,
                2, 3, 4), nrow=3, byrow=TRUE))
layout.show(n=4)

enter image description here

Upvotes: 38

Related Questions