Reputation: 55
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
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()
.
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="")
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="")
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="")
Upvotes: 5
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