13

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")
3
  • 2
    According to ?layout, layout divides the device up into as many rows and columns as there are in matrix mat, with the column-widths and the row-heights specified in the respective arguments.
    – akrun
    Aug 7, 2016 at 4:26
  • 1
    Here it is a 3*3 matrix and has a figure with four plots. So how does these number correspond to the plots?
    – Bratt Swan
    Aug 7, 2016 at 4:38
  • The best explanation of its functioning that I know sits in the YaRrr! Pirate’s Guide to R. Once the basic reasoning was explained the docs suddenly made all sense.
    – mirh
    Sep 16, 2019 at 15:43

1 Answer 1

35

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

2
  • Your answer is perfect! Thanks
    – Bratt Swan
    Aug 7, 2016 at 4:41
  • 1
    Perfect. Formatting the matrix in this way allows us to understand easily.
    – igorkf
    Sep 23, 2020 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.