Arrange plots in a layout which cannot be achieved by 'par(mfrow ='

12,875

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 :

layout with horizontal third figure

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 :

layout with a vetrtical third figure

Another workaround is to save your figure as a pdf and edit it with a tool like inscape to "center" your third figure.

Share:
12,875
Marco
Author by

Marco

Updated on August 04, 2022

Comments

  • Marco
    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
    Marco about 13 years
    Yes, 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
    mdsumner about 13 years
    you'll need to clarify further, but I think I know what you want now - layout
  • juba
    juba about 13 years
    Ah yes, a quite clever usage of layout ! (clever in the sense of "but why, oh why, didn't I thought of it myself !") :-)
  • Marco
    Marco about 13 years
    Thank you for this answer. I am sure I will use it later!