R provides several frameworks for composing figures. Base graphics is the simplest, grid is more advanced, and the lattice/ggplot packages provide convenient abstractions of the grid graphics system. Multi-element figures can be readily created in base graphics using either par() or layout(), with analogous functions available in grid. Mixing the two systems is a little more complicated, somewhat fragile, but entirely possible.

I recently needed to combine base+grid graphics on a single page; two sets of base graphics on the left, and the output from xyplot() (grid) on the right. Following some tips from Paul Murrell posted on r-help, the solution was fairly simple. A truncated example of the processes is listed below, corresponding to the attached figure. While the result was "good enough" for a quick summary, there are clearly some improvements that would make figure more useful.

mehrten_soils.preview.png
Image: Mehrten Soil Summary

Sample Code

# libraries
library(grid)
library(gridBase)
library(lattice)
# ... this is a truncated example ...
# start new page
par(mar=c(0,0,0,0)no.readonly=TRUE)
plot.new()
# setup layout
gl <- grid.layout(nrow=2ncol=2widths=unit(c(2,1)'null')heights=unit(c(1,3)'null'))
# grid.show.layout(gl)
# setup viewports
vp.1 <- viewport(layout.pos.col=1layout.pos.row=c(1,2)) # dend + profiles
vp.2 <- viewport(layout.pos.col=2layout.pos.row=2) # depth functions
# init layout
pushViewport(viewport(layout=gl))
# access the first position
pushViewport(vp.1)
# start new base graphics in first viewport
par(new=TRUEfig=gridFIG())
p.plot <- plot(pcex=0.8label.offset=-3direction='up'y.lim=c(200,0),
x.lim=c(1length(m)+1)show.tip.label=FALSE)
tiplabels(pch=15col=cols[grps])
# get the last plot geometry
lastPP <- get("last_plot.phylo"envir = .PlotPhyloEnv)
# vector of indices for plotting soil profiles below leaves of dendrogram
new_order <- sapply(1:lastPP$Ntip,
  function(i) which(as.integer(lastPP$xx[1:lastPP$Ntip]) == i))
# plot the profiles, in the ordering defined by the dendrogram
# with a couple fudge factors to make them fit
sf <- 0.8
yoff <- max(lastPP$yy) + 5
# add lines at soil depth classes
dc <- c(25,50,75,100,150) * sf + yoff
abline(h=dclty=2col='grey')
profile_plot(mcolor="soil_color"plot.order=new_ordercex.id=0.75max.depth=150,
n.depth.ticks=6scaling.factor=sfwidth=0.1cex.names=0.65cex.depth.axis=0.75,
y.offset=yoffadd=TRUEname='hzname'id.style='side')
# done with the first viewport
popViewport()
# move to the next viewport
pushViewport(vp.2)
# print our lattice graphics here
print(p.1newpage=FALSE)
# done with this viewport
popViewport(1)
X