Reputation: 9437
I'm using the layout function to arrange two plots using the following code:
#Set up layout
m <- matrix(c(1,1,2, 1,1,2), nrow = 3, ncol = 2)
layout(m)
#First plot
plot(data$WG,data$RPOP, pch=21, cex=0.8, col=data$circle, bg=data$fill, xaxt='n', yaxt='n', bty="L", xlab="", ylab="Y label 1",xlim=c(0.02,1), ylim=c(0.02,1))+axis(side=1, at=seq(0,1, .1), pos=-0.02, tck=-.01, labels=FALSE)+axis(side=2, at=seq(0,1, .1), pos=-0.02, tck=-.01, las=1)
#Second plot
datum<-data.frame(prop=c(0.027879, 0.031515,0.001212,0,0,0,0,0,0,0,0),freq=c(0, 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0))
plot(datum$freq,datum$prop, cex=0, xaxt='n', yaxt='n', bty="L", xlab="X label", ylab="Y label 2", xlim=c(0.02,1), ylim=c(0.001,0.05))+axis(side=1, at=seq(0, 1, 0.1), pos=-0.001, tck=-.01)+axis(side=2, at=seq(0, 0.05, 0.01), pos=-0.02, tck=-.01, las=1)+lines(datum$freq,datum$prop, lwd=2, col="red")
And this is the result I get the following graph:
I would like to get rid of the space between the two graphs and at the same time I would like the width and height of the top plot to be the same. Is there a way to achieve this with the layout function?
Upvotes: 0
Views: 989
Reputation: 93761
Change the margins to get the plots closer together. The default margins are 5.1, 4.1, 4.1, 2.1 in the order bottom, left, top, right. So we'll make the bottom margin smaller for the top plot and the top margin smaller for the bottom plot. To reset the margins to the default after you're done, do par(mar=c(5,4,4,2) + 0.1)
:
#Set up layout
m <- matrix(c(1,1,2, 1,1,2), nrow = 3, ncol = 2)
layout(m)
# Smaller bottom margin for top plot
par(mar=c(1,4,4,2))
#First plot
plot(data$WG,data$RPOP, pch=21, cex=0.8, col=data$circle, bg=data$fill, xaxt='n', yaxt='n', bty="L", xlab="", ylab="Y label 1",xlim=c(0.02,1), ylim=c(0.02,1))+axis(side=1, at=seq(0,1, .1), pos=-0.02, tck=-.01, labels=FALSE)+axis(side=2, at=seq(0,1, .1), pos=-0.02, tck=-.01, las=1)
#Second plot
datum<-data.frame(prop=c(0.027879, 0.031515,0.001212,0,0,0,0,0,0,0,0),freq=c(0, 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0))
# Smaller top margin for bottom plot
par(mar=c(5,4,1,2))
plot(datum$freq,datum$prop, cex=0, xaxt='n', yaxt='n', bty="L", xlab="X label", ylab="Y label 2", xlim=c(0.02,1), ylim=c(0.001,0.05))+axis(side=1, at=seq(0, 1, 0.1), pos=-0.001, tck=-.01)+axis(side=2, at=seq(0, 0.05, 0.01), pos=-0.02, tck=-.01, las=1)+lines(datum$freq,datum$prop, lwd=2, col="red")
UPDATE: One way to get a square plot would be to set par(pty="s")
(the default is "m"), which gives a 1/1 aspect ratio. However, after you reset par(pty="m")
for the second plot, the two plots might not have the same width and you would have to play around with the plot size to get them lined up.
Another option would be to set the output file size to get the desired aspect ratio when you save it to png
or pdf
. For example:
png("test.png", 600, 800)
... All of your plotting code, including call to layout...
dev.off()
pdf("test.pdf", 6, 8)
... All of your plotting code, including call to layout...
dev.off()
There's probably a programmatic way to ensure the desired aspect ratio and line up the plots correctly without any manual adjustments, but I'm not sure how to do it.
Upvotes: 3