# Title: R Margins, multiple figure example - layout
# Date: 2008-02-01
# Project: CFL R graphics 

rm(list = ls())		# clear objects
graphics.off() 		# close graphics windows 

# Plot 3
par(oma=c(3,3,3,3))

# The layout command is complex. The first parameter is a matrix telling us first the order
# of the plots (left to right, top to bottom), followed by the parameters for the number of 
# rows and number of columns. This layout is saying this:
#	2	0
#	1	3
# Note that '0' indicates no plot, not a 0-indexed counting system. 
# The first plot that we create will go in the bottom left, then the next in the top left,
# and the third on the bottom right.
# The 'widths' parameter tells layout the widths of the columns in inches, and similarly
# the 'heights' parameter tells layout the heights of the rows. 
# I'm not sure what the last parameter actually does, but it's refered to as 'respect', 
# and it is probably a bad idea to not have any respect. So we leave that as true.

nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), widths=c(6,1), heights=c(1,6), TRUE)

# If you want to see your layout, this command will create a chart labelling it clearly.
#layout.show(nf)

# First plot, goes in position one, so lower left. This is our large plot.
par(mar=c(4,4,1,1))
plot(0:10, 0:10, type="n", xlab="X", ylab="Y", col="green")
box("figure", col="green")
text(5,5,"Plot 1", col="green", cex=2)

# Second plot, top left
par(mar=c(1,1,1,1))
plot(0:10, 0:10, type="n", xlab="X", ylab="Y", axes=FALSE, col="green")
box("figure", col="red")
text(5,5,"Plot 2", col="red", cex=2)

# Third plot, bottom right.
par(mar=c(1,1,1,1))
plot(0:10, 0:10, type="n", xlab="X", ylab="Y", axes=FALSE, col="green")
box("figure", col="blue")
text(5,5,"Plot 3", col="blue", cex=2)

# Label the outer margin area to show it is still there
mtext("Outer Margin Area", side=1, line=1, cex=2, col="black", outer=TRUE)
box("inner", col="black")

# Further Reading:
# help(layout)
# help(par)
# help(matrix)
box("outer", col="black")