Figure 13-8: A Layout of Plots

Portfolio Categories: All Graphics and SGR Book Graphics.

Demonstration of the Layout Method in R graphics

A Set of R Plots Placed with the Layout Method


# A more complex layout example ------------------------------------------------

png(filename = "illustrations/fig-13-8-layout2.png",
  units = "in",                        # Set measurements in inches
  res = 1200,                          # Set resolution at 1200dpi
  width = 6,                           # Width at 6 inches
  height = 4)                          # Height at 4 inches

par(mai = c(.6, .6, .5, .1))           # Set margins around plots

layout(                                # Set up a multiplot layout
  matrix(c(1, 1, 4, 2, 3, 4),          # Create a matrix of 6 cells
    ncol = 3,                          #   in 3 columns (3 cells per row)
    byrow = T),                        # Order the cells by row
  heights = c(.6, .4),                 # Set row heights
  widths = c(.34, .33, .33))           # Set column widths

layout.show(4)                         # Preview the layout grid

myX = c(0:10)                          # Some data for plots 
myY = log(myX + 1) 
myZ = c(30, 60)

# Some plots to fill in layout
plot(myX, myY, type = "b", main = "Plot 1")
par(mai = c(.6, .6, .5, .1))           # Change plot margins
plot(myX, myX^2, ylim = c(0, 100), type = "o", main = "Plot 2")
par(mai = c(.6, .35, .5, .1))          # Change plot margins
barplot(myZ, ylim = c(0, 100), main = "Plot 3")
par(mai = c(.6, .2, .5, .1))           # Change plot margins
dotchart(10 - myX, main = "Plot 4")

dev.off()                              # Output the png file