user1448268
user1448268

Reputation: 55

Two-row-plot formatting

I have the following problem: I want to make a plot in R where two figures are above each other. However, the upper figure should take up most of the space and the lower figure should just have a very low height (it just serves to indicate special positions in the "main" figure). At the moment I have this code, but I don't know how to set the space each row should take up (and the second plot should be immediately beneath the first one):

dev.new()
png("multitest.png")
par( mfrow = c( 2, 1 ) ) 
plot( rnorm( n = 10 ), col = "blue", main = "plot 1", cex.lab = 1.1,ylab="yname", xlab='')
plot( rnorm( n = 10 ), col = "red", main = "", cex.lab = 1.1, xaxt='n',yaxt='n',xlab="xname", ylab="")
dev.off()

I have to run it on a server where I don't know if additional packages are available for R. If there is a suggestions using additional packages, I will try it though. Thank you in advance, I have used plots in R just for simple histograms so far.

Upvotes: 1

Views: 822

Answers (2)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

As indicated by @Roland, layout() would be much better for this purpose than using par(mfrow...). Here are some basic examples to show you how to get started with layout. None of these examples do the work for you: I'm just giving examples so you can figure out how to experiment with it. A useful function here is also layout.show().

  1. Two plots, one above the other. The first plot is wider than the other.

    First, think of a matrix of how you want your figures to appear. Number them sequentially. 1 = Figure 1, 2 = Figure 2, 0 = Nothing plotted in this space.

         [,1] [,2] [,3] [,4]
    [1,]    1    1    1    1
    [2,]    0    2    2    0
    

    Use this matrix in layout():

    layout(matrix(c(1, 1, 1, 1, 0, 2, 2, 0), 2, 4, byrow=TRUE))
    plot(rnorm(n = 10), col = "blue", main = "plot 1", 
         cex.lab = 1.1, ylab="yname", xlab='')
    plot(rnorm(n = 10), col = "red", main = "", cex.lab = 1.1, 
         xaxt='n', yaxt='n', xlab="xname", ylab="")
    

    enter image description here

  2. Notice that the heights of each plot is still the same. You can use the heights argument to control for this:

    layout(matrix(c(1, 1, 1, 1, 0, 2, 2, 0), 2, 4, byrow=TRUE), 
           heights = c(7, 3))
    plot(rnorm(n = 10), col = "blue", main = "plot 1", 
         cex.lab = 1.1, ylab="yname", xlab='')
    plot(rnorm(n = 10), col = "red", main = "", cex.lab = 1.1, 
         xaxt='n', yaxt='n', xlab="xname", ylab="")
    

    enter image description here

  3. Here is an example with three plots.

    layout(matrix(c(1, 1, 2, 3), 2, 2, byrow=TRUE))
    plot(rnorm(n = 10), col = "blue", main = "plot 1", 
         cex.lab = 1.1, ylab="yname", xlab='')
    plot(rnorm(n = 10), col = "red", main = "", cex.lab = 1.1, 
         xaxt='n', yaxt='n', xlab="xname", ylab="")
    plot(rnorm(n = 10), col = "green", main = "", cex.lab = 1.1, 
         xaxt='n', yaxt='n', xlab="zname", ylab="")
    

    enter image description here

Upvotes: 5

January
January

Reputation: 17090

Use layout:

nf <- layout( c( 1, 2 ), heights= c( 10, 2 ) )
plot( ... )
plot( .... )

The first argument is a matrix that mimicks how your screen is divided (here we have a vector as an argument, but you could use matrix( c( 1, 2 ), nrow=2, ncol=1 ).

Upvotes: 3

Related Questions