Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
In R, there are a couple of packages that allow you to create multi-panel figures (see examples here and here), but, of course, you can also make multi-panel figures in the base package*. Below I provide a simple example for creating a multi-panel figure in the R base package with the focus on making the text and symbols the same size in all of your figures, which is a desirable trait for a set of figures that will appear in the same manuscript.
The mfrow argument of the par( ) function determines the arrangement of the plots in the graphics device by specifying the number of rows and columns and the order that the graphics device is filled, i.e., by row (use mfcol to fill by columns). Below is example code and the figure that is produced from the code.
png(filename="ThreeByTwo.png",width=600,height=600) par(mfrow=c(3,2)) plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y") plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y") plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y") plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y") plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y") plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y") dev.off()
png(filename="TwoByTwo.png",width=600,height=400) par(mfrow=c(2,2)) plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y") plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y") plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y") plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y") dev.off()
png(filename="OneByTwo.png",width=600,height=200) par(mfrow=c(1,2)) plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y") plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y") dev.off()
We know from the documentation that a 2×2 figure involves a reduction of 83% relative to the default. We need to reduce the size of text/symbols by another 79.5% to achieve the same reduction as in the 3×2 figure (0.83*0.795=0.66), which is applied to the numbers on the axis (cex.axis), axis labels (cex.lab), and symbols (cex).
png(filename="TwoByTwoAdj.png",width=600,height=400) par(mfrow=c(2,2),cex.axis=0.795,cex.lab=0.795) plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y",cex=0.795) plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y",cex=0.795) plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y",cex=0.795) plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y",cex=0.795) dev.off()
png(filename="OneByTwoAdj.png",width=600,height=200) par(mfrow=c(1,2),cex.axis=0.66,cex.lab=0.66) plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y",cex=0.66) plot(rnorm(50),rnorm(50),pch=16,xlab="X",ylab="Y",cex=0.66) dev.off()
* Other options for multi-panel figures in the R base package include layout( ) and split.screen( ).
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.