How to Put Multiple Plots on a Single Page in R

To put multiple plots on the same graphics pages in R, you can use the graphics parameter mfrow or mfcol. To use this parameter, you need to supply a vector argument with two elements: the number of rows and the number of columns.

For example, to create two side-by-side plots, use mfrow=c(1, 2):

> old.par <- par(mfrow=c(1, 2))
> plot(faithful, main="Faithful eruptions")
> plot(large.islands, main="Islands", ylab="Area")
> par(old.par)

When your plot is complete, you need to reset your par options. Otherwise, all your subsequent plots will appear side by side (until you close the active graphics device, or window, and start plotting in a new graphics device).

You can use a neat little trick to do this: When you make a call to par(), R sets your new options, but the return value from par() contains your old options. In the previous example, you saved the old options to an object called old.par, and then reset the options after plotting using par(old.par).

image0.jpg

Use mfrow to fill the plot grid by rows, and mfcol to fill the plot grid by columns. The Help page ?par, explains these option in detail, and also points you alternative layout mechanisms (like layout() or split.screen()).

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.