Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use layout() function in R?

Tags:

plot

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")
like image 585
Bratt Swan Avatar asked Aug 07 '16 04:08

Bratt Swan


People also ask

What does layout function do in R?

The layout() function of R allows to split the plot window in areas with custom sizes. Here are a few examples illustrating how to use it with reproducible code and explanation.

What is the layout function?

The layout functions are used to handle bidirectional languages correctly, to transform text from a format readable for the user to a format suitable for processing, and vice-versa. The layout functions include the following: m_create_layout()

How do you divide a plot area in R?

The mfrow() parameter allows to split the screen in several panels. Subsequent charts will be drawn in panels. You have to provide a vector of length 2 to mfrow() : number of rows and number of columns.

How do I put multiple plots on one page in R?

To arrange multiple ggplot2 graphs on the same page, the standard R functions - par() and layout() - cannot be used. The basic solution is to use the gridExtra R package, which comes with the following functions: grid. arrange() and arrangeGrob() to arrange multiple ggplots on one page.


1 Answers

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

like image 159
jbaums Avatar answered Sep 26 '22 03:09

jbaums