Arrange plots in a layout which cannot be achieved by 'par(mfrow ='
Solution 1
You probably want layout
, you can set up pretty complex grids by creating a matrix.
m <- matrix(c(1, 0, 1, 3, 2, 3, 2, 0), nrow = 2, ncol = 4)
##set up the plot
layout(m)
## now put out the 3 plots to each layout "panel"
plot(1:10, main = "plot1")
plot(10:1, main = "plot2")
plot(rnorm(10), main = "plot3")
Use layout.show
to see each panel.
Print out the matrix to see how this works:
m
[,1] [,2] [,3] [,4]
[1,] 1 1 2 2
[2,] 0 3 3 0
There are 1s for the first panel, 2s for the second, etc. 0s for the "non-panel".
See help(layout)
.
Solution 2
Not exactly what you are asking for, as the third figure is not horizontally centered but stretched to the full device width, but the layout
function allows for a much more flexible configuration.
For example, the following layout definition :
R> layout(matrix(c(1,2,3,3), 2, 2, byrow = TRUE))
R> plot(rnorm(100),col=1)
R> plot(rnorm(100),col=2)
R> plot(rnorm(100),col=3)
Gives the following result :
You can also use a "vertical" stretch with the following layout :
R> layout(matrix(c(1,3,2,3), 2, 2, byrow = TRUE))
R> plot(rnorm(100),col=1)
R> plot(rnorm(100),col=2)
R> plot(rnorm(100),col=3)
Which gives :
Another workaround is to save your figure as a pdf and edit it with a tool like inscape to "center" your third figure.
Marco
Updated on August 04, 2022Comments
-
Marco almost 2 years
I have three plots which I would to arrange in a single window. I can arrange similar-sized plots on a regular 2*2 grid using
par(mfrow = c(2, 2))
:par(mfrow = c(2, 2)) plot(1:10, main = "plot1") plot(10:1, main = "plot2") plot(rnorm(10), main = "plot3")
However, I want to position "plot1" and "plot2" beside each other on the top row, and "plot3" below them, centered horizontally. How can I achieve this?
-
Marco about 13 yearsYes, this is one possibility. But still, is it possible to have 2 rows and 2 columns? By the way, thank you for the clarification.
-
mdsumner about 13 yearsyou'll need to clarify further, but I think I know what you want now - layout
-
juba about 13 yearsAh yes, a quite clever usage of
layout
! (clever in the sense of "but why, oh why, didn't I thought of it myself !") :-) -
Marco about 13 yearsThank you for this answer. I am sure I will use it later!