Ola,
In my last post I wrote about how to plot multiple graphs together in R. It was an absolutely amazing post, the best I have ever read. You should check it our -> I am that awesome post he is exaggerating about
Now, what if we want to control how many graphs should be there in each row or column? Exited ? Click read more
So, suppose we don't want all the slots of the grid to be filled, if we do this with par(), we have to create an empty plot at the location where we want nothing and again that would be a blank space and one can clearly discern something missing. See, I am not taking anything away from par(). Par() is awesome, but even Superman slows down in presence of Kryptonite. Got it...yeah
So, here is our superman struggling in front of Kryptonite and in comes the savior - Layout().
Lets start by understanding the syntax.
>Layout(matrix)
So, yes it needs a matrix. And how should that matrix be?
Suppose we want to plot only three graphs such that there is one graph in the bottom row and two graphs in the top. For this matrix will need to have 2 rows and 2 columns, such that we will merge the columns of row 2 and keep the columns of row 1 as they are.
And what should be the data of the matrix? For convenience make the byrow argument of the matrix True. Now, the graphs will be filled row wise from left to right. Each graph will have a serial number. In our example we are having three graphs. So, there will be three serial numbers viz. 1, 2 and 3.
Now, we only want i graph in the entire bottom row, for this the data of our matrix will be 1,2,3,3. By doing this we are specifying that cell(2,1) and cell(2,2) will share a graph and cell(1,1),cell(1,2) will have different graphs.
So, the matrix will look something like this -
matrix(c(1,2,3,3),2,2,byrow=T)
And this will be our input to layout function
>layout(matrix(c(1,2,3,3),2,2,byrow=T)
So how will the overall code look? Like this -
> layout(matrix(c(1,2,3,3),2,2,byrow=T))
> hist(mtcars$wt)
> hist(mtcars$wt)
> hist(mtcars$wt)
And how will the graph look ? Like this -
For this we will modify our matrix as follows -
matrix(c(1,2,3,2),2,2,byrow=T)
The code will be as follows
> layout(matrix(c(1,2,3,2),2,2,byrow=T))
> hist(mtcars$wt)
> hist(mtcars$wt)
> hist(mtcars$wt)
And the output as follows -
The super awesomeness of layout() does not stop over there.
What if after controlling the number of graphs in each row/column you want to
control their sizes as well? Yes sir, even that is possible!
Let me reveal to you two more arguments to layout() apart from
matrix, the width argument and the height arguments.
The input to width argument is a vector of values that will
control the relative widths of the columns. So, if you got two columns in your
chart. You will input two values to the vector and the widths of the columns
will get divided in the ratio of the inputs.
Width=c(1,3,2) will arrange the widths of the columns 1, 2
and 3 in the ratio of 1:3:2
The height argument controls the height of the rows. The
usage is same as that of width argument.
Height=c(2,3,4) will arrange the heights of the rows 1, 2
and 3 in the ratio of 2:3:4
Let us put this into use for our above example. Let’s
arrange the column widths in the ratio of 2:1 and the height of rows in the
ratio of 1:2. Our code will be modified as following –
>Layout(matrix(c(1,2,3,2),2,2,byrow=T),width=c(2,1),height=c(1,2))
>Hist(mtcars$wt)
>Hist(mtcars$wt)
>Hist(mtcars$wt)
And voila –
Hope you liked it.
Let me know your thoughts on this. You will have some thoughts on this right? I am sure you do.
Till then,
Stay Awesome,







No comments:
Post a Comment