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

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

# Generate data
x = 0:10;
y = 0:10;

# Plot 2 setup
# Specify the layout parameters before any plotting

# - Set the number of figures in terms of rows and columns.
# 	- 2 rows, 2 columns, for 4 total figures.
par(mfcol=c(2,2));

# - Set the margins for a single figure. Note that this parameter can
#   be set on a per-figure basis since each figure can have unique margins.
# - If you do not redefine the mar parameter after this, all figures will
#   use this setting.
par(mar=c(5,4,4,2));

# - Set the outer margin area. Note that this parameter applies for the entire
#   figure and altering it while drawing figures may cause problems.
par(oma=c(3,3,3,3));


# Plot 1
plot(x, y, main="Plot with title", type="n", xlab="X", ylab="Y")
text(5,5, "Plot 1", col="red", cex=2)
text(5,4, "Full plot with title", col="red", cex=1)
box("plot", col="red")
box("figure", col="green")


# Plot 2
# Altering the margins for plot 2
par(mar=c(4,4,2,2))
plot(x, y, type="n", xlab="X", ylab="Y")
text(5,5, "Plot 2", col="red", cex=2)
text(5,4, "Full square plot without title", col="red", cex=1)
box("plot", col="red")
box("figure", col="green")

# Plot 3
# Altering the margins for plot 3
par(mar=c(2,2,0,0))
plot(x, y, type="n", xlab="X", ylab="Y", axes=)
text(5,5, "Plot 3", col="red", cex=2)
text(5,4, "Plot with no extra margin space", col="red", cex=1)
box("plot", col="red")
box("figure", col="green")

# Plot 4
# Altering the margins for one of the four
par(mar=c(2,2,2,2))

# - Notice that the X and Y labels are gone, they didn't
#   fit into the space provided for the graphic so they simply
#   were covered.
plot(0:10, 0:10, type="n", xlab="X", ylab="Y")
text(5,5, "Plot 4", col="red", cex=2)
box("plot", col="red")
box("figure", col="green")

# Label the outer margin area so we can see where it is
mtext("Outer Margin Area", side=3, line=1, cex=2, col="blue", outer=TRUE)
box("outer", col="blue")

# Further reading:
# help(mfcol)
# help(par)