Add extra spacing between a subset of plots
I'm trying to output 6 figures into one image, in a 3x2 layout. I'd like to place extra space between the top row and the bottom two rows. Is this possible using R? I've looked through the documentation for par and plot and can't seem to find an appropriate option.
Here's some example code:
a = rnorm(100,100,10)
b = rnorm(100,100,10)
par(mfrow=c(3,2), oma=c(1,1,1,1), mar=c(2,2,2,2))
hist(a)
hist(b)
plot(a,b)
plot(a,b)
plot(a,b开发者_如何学JAVA)
plot(a,b)
Here's what that code outputs:
Here's what I'd like it to output (I modified this image in an external editor). Note the extra space between the top row and bottom rows.
The layout()
function is your friend. You could for example define a plot matrix
1 2
3 4
5 6
7 8
and then put empty plots in for the third and fourth. Or just stick to six and call par
to add extra spacing at the bottom.
I can think of three ways:
1) Use the mar
graphic parameter to set plot margin
You can retrieve current margins with
currmar <- par()$mar
You can set new margins with
par("mar"=c(5, 4, 4, 2))
with the for numbers being bottom, left, top and right margins (see ?par
)
You can make multiple calls to par
for each plot, so you can change the bottom margin only for the top plots.
2) Use layout to generate an uneven layout grid (see ?layout
for examples)
3) Save the plot in .svg or .pdf and then use Inkscape (or whatever software you like) to move the plots.
I think going with mar
is the way I would do it. However, as it looks like, you want all the plots to be the same. Therefore, you need to have the same amount taken off by mar on every plot on top and bottom.
In your case one could use the following numbers:
1. row: par(mar=c(7,4,4,2))
2. row: par(mar=c(5,4,6,2))
3. row: par(mar=c(7,4,4,2))
This way all plots occupy the same height. Modifie the first and third number in such a way that they are the same for each plot to accomplish this. However, on caveat: There is some extra white space below the plots in the bottom row.
There is nothing to add concerning plots, but I stuggled several times with layout, so here is way to visualize the layout (from here) - using base-R:
# Margins area
par(oma=c(3,3,3,3)) # all sides have 3 lines of space
par(mar=c(5,4,4,2) + 0.1) # mar=c(b,l,t,r)
# Plot
plot(0:10, 0:10, xlab="X", ylab="Y") # type="n" hides the points
# Place text in the plot and color everything plot-related red
text(5,5, "Plot", col="red", cex=2)
box(col="red")
# Place text in the margins and label the margins, all in forestgreen
mtext("Margins", side=3, line=2, cex=2, col="forestgreen")
mtext("par(mar=c(b,l,t,r))", side=3, line=1, cex=1, col="forestgreen")
mtext("Line 0", side=3, line=0, adj=1.0, cex=1, col="forestgreen")
mtext("Line 1", side=3, line=1, adj=1.0, cex=1, col="forestgreen")
mtext("Line 2", side=3, line=2, adj=1.0, cex=1, col="forestgreen")
mtext("Line 3", side=3, line=3, adj=1.0, cex=1, col="forestgreen")
box("figure", col="forestgreen")
# Label the outer margin area and color it blue
# Note the 'outer=TRUE' command moves us from the figure margins to the outer margins.
mtext("Outer Margin Area", side=1, line=1, cex=2, col="blue", outer=TRUE)
mtext("par(oma=c(b,l,t,r))", side=1, line=2, cex=1, col="blue", outer=TRUE)
mtext("Line 0", side=1, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1", side=1, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 2", side=1, line=2, adj=0.0, cex=1, col="blue", outer=TRUE)
box("outer", col="blue")
mtext("Line 0, side=2", side=2, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=2", side=2, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 0, side=3", side=3, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=3", side=3, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 0, side=4", side=4, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=4", side=4, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
Result:
With two plots:
par(mfrow = c(2, 1))
# First plot
# Margins area
par(oma=c(3,3,3,3)) # all sides have 3 lines of space
par(mar=c(5,4,4,2) + 0.1) # mar=c(b,l,t,r)
# Plot
plot(0:10, 0:10, xlab="X", ylab="Y") # type="n" hides the points
# Place text in the plot and color everything plot-related red
text(5,5, "Plot", col="red", cex=2)
box(col="red")
# Place text in the margins and label the margins, all in forestgreen
mtext("Margins", side=3, line=2, cex=2, col="forestgreen")
mtext("par(mar=c(b,l,t,r))", side=3, line=1, cex=1, col="forestgreen")
mtext("Line 0", side=3, line=0, adj=1.0, cex=1, col="forestgreen")
mtext("Line 1", side=3, line=1, adj=1.0, cex=1, col="forestgreen")
mtext("Line 2", side=3, line=2, adj=1.0, cex=1, col="forestgreen")
mtext("Line 3", side=3, line=3, adj=1.0, cex=1, col="forestgreen")
box("figure", col="forestgreen")
# Label the outer margin area and color it blue
# Note the 'outer=TRUE' command moves us from the figure margins to the outer margins.
mtext("Outer Margin Area", side=1, line=1, cex=2, col="blue", outer=TRUE)
mtext("par(oma=c(b,l,t,r))", side=1, line=2, cex=1, col="blue", outer=TRUE)
mtext("Line 0", side=1, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1", side=1, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 2", side=1, line=2, adj=0.0, cex=1, col="blue", outer=TRUE)
box("outer", col="blue")
mtext("Line 0, side=2", side=2, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=2", side=2, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 0, side=3", side=3, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=3", side=3, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 0, side=4", side=4, line=0, adj=0.0, cex=1, col="blue", outer=TRUE)
mtext("Line 1, side=4", side=4, line=1, adj=0.0, cex=1, col="blue", outer=TRUE)
# Second plot
plot(0:10, 0:10, xlab="X", ylab="Y", ) # type="n" hides the points
# Place text in the plot and color everything plot-related red
text(5,5, "Plot", col="red", cex=2)
box(col="red")
# Place text in the margins and label the margins, all in forestgreen
mtext("Margins", side=3, line=2, cex=2, col="forestgreen")
mtext("par(mar=c(b,l,t,r))", side=3, line=1, cex=1, col="forestgreen")
mtext("Line 0", side=3, line=0, adj=1.0, cex=1, col="forestgreen")
mtext("Line 1", side=3, line=1, adj=1.0, cex=1, col="forestgreen")
mtext("Line 2", side=3, line=2, adj=1.0, cex=1, col="forestgreen")
mtext("Line 3", side=3, line=3, adj=1.0, cex=1, col="forestgreen")
box("figure", col="forestgreen")
par(mfrow = c(1, 1))
Result:
精彩评论