Multiple Plot Figure
refered from http://sphaerula.com/legacy/R/multiplePlotFigure.html
Table of Contents
Introduction
There are three basic ways to place multiple plots in a single figure:
par(mfrow)andpar(mfcol)split.screen()layout()
par(mfrow) and par(mfcol)
The
mfrow parameter to the par() function sets up drawing of multiple plots by row; mfcol sets up drawing of multiple plots by column. A vector with two values must be provided that sets the number of rows and columns.
This command sets up plotting of multiple plots in a two row by three column grid, with the plots drawn along rows first:
## Open a new default device.
get( getOption( "device" ) )()
## Set up plotting in two rows and three columns, plotting along rows first.
par( mfrow = c( 2, 3 ) )
## The first plot is located in row 1, column 1:
plot( rnorm( n = 10 ), col = "red", main = "plot 1", cex.lab = 1.1 )
## The second plot is located in row 1, column 2:
plot( runif( n = 10 ), col = "blue", main = "plot 2", cex.lab = 1.1 )
## The third plot is located in row 1, column 3:
plot( rt( n = 10, df = 8 ), col = "springgreen4", main = "plot 3",
cex.lab = 1.1 )
## The fourth plot is located in row 2, column 1:
plot( rpois( n = 10, lambda = 2 ), col = "black", main = "plot 4",
cex.lab = 1.1 )
## plot.new() skips a position.
plot.new()
## The fifth plot is located in row 2, column 3:
plot( rf( n = 10, df1 = 4, df2 = 8 ), col = "gray30", main = "plot 5",
cex.lab = 1.1 )
The output is:
Multiple plot figure from using
par(mfrow)
split.screen()
## Open a new default device. get( getOption( "device" ) )() ## Split the screen into two rows and one column, defining screens 1 and 2. split.screen( figs = c( 2, 1 ) ) ## Split screen 1 into one row and three columns, defining screens 3, 4, and 5. split.screen( figs = c( 1, 3 ), screen = 1 ) ## Split screen 2 into one row and two columns, defining screens 6 and 7. split.screen( figs = c( 1, 2 ), screen = 2 ) ## The first plot is located in screen 3: screen( 3 ) plot( rnorm( n = 10 ), col = "red", main = "plot 1" ) ## The second plot is located in screen 4: screen( 4 ) plot( runif( n = 10 ), col = "blue", main = "plot 2" ) ## The third plot is located in screen 5: screen( 5 ) plot( rt( n = 10, df = 8 ), col = "springgreen4", main = "plot 3" ) ## The fourth plot is located in screen 6: screen( 6 ) plot( rpois( n = 10, lambda = 2 ), col = "black", main = "plot 4" ) ## The fifth plot is located in screen 7: screen( 7 ) plot( rf( n = 10, df1 = 4, df2 = 8 ), col = "gray30", main = "plot 5" ) ## Close all screens. close.screen( all = TRUE )
The output is:
Multiple plot figure from using
split.screen()
layout()
def.par <- par(no.readonly = TRUE) nf <- layout(matrix(c(1,2), 1,2,byrow=T), c(1,1), c(1,1), T) layout.show(nf) plot(rnorm(10,0,1)) plot(rnorm(10,0,0.1)) par(def.par)
Overall title
The trick to creating an overall title for a window containing multiple plots is to use
mtext(), which draws text in the margin of a figure.
This example, derived from code posted to the R-help list by Chuck Cleland, uses
layout() to configure the multiple plots.## Open a new default device.
get( getOption( "device" ) )()
## Set the outer margins so that bottom, left, and right are 0
## and top is 3 lines of text.
old.par <- par( no.readonly = TRUE )
par( oma = c( 0, 0, 3, 0 ) )
## Plot using layout().
nf <- layout( matrix( c( 1, 2 ), 1, 2, byrow = TRUE),
c( 1, 1 ), c( 1, 1 ), TRUE )
layout.show( nf )
## Create the two plots.
plot( rnorm( n = 10, mean = 0, sd = 1 ) )
plot( rpois( n = 10, lambda = 3 ) )
## Create an overall title.
mtext( "Centered Overall Title", outer = TRUE )
## Restore the old plotting parameters.
par( old.par )
The output is:
Example of creating an overall title for a multiple plot figure
This example, also derived from code posted to the R-help list by Chuck Cleland, uses
par(mfrow)to configure the multiple plots.## Open a new default device. get( getOption( "device" ) )() ## Set up multiple plots in a one row by two column configuration. ## Set the outer margin so that bottom, left, and right are 0 and ## top is 2 lines of text. par( mfrow = c( 1, 2 ), oma = c( 0, 0, 2, 0 ) ) ## Create the two plots. plot( rnorm( n = 10, mean = 0, sd = 1 ), main = "Normal" ) plot( rpois( n = 10, lambda = 2 ), main = "Poisson" ) ## Create an overall title. title( "Centered Overall Title", outer = TRUE )
The output is:
Example of creating an overall title for a multiple plot figure
No comments:
Post a Comment