Combining Base+Grid Graphics
Oct 4, 2012 metroadminR 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.
Image: Mehrten Soil Summary
Sample Code
# librarieslibrary(grid)library(gridBase)library(lattice)# ... this is a truncated example ...# start new pagepar(mar=c(0,0,0,0), no.readonly=TRUE)plot.new()# setup layoutgl <- grid.layout(nrow=2, ncol=2, widths=unit(c(2,1), 'null'), heights=unit(c(1,3), 'null'))# grid.show.layout(gl)# setup viewportsvp.1 <- viewport(layout.pos.col=1, layout.pos.row=c(1,2)) # dend + profilesvp.2 <- viewport(layout.pos.col=2, layout.pos.row=2) # depth functions# init layoutpushViewport(viewport(layout=gl))# access the first positionpushViewport(vp.1)# start new base graphics in first viewportpar(new=TRUE, fig=gridFIG())p.plot <- plot(p, cex=0.8, label.offset=-3, direction='up', y.lim=c(200,0),x.lim=c(1, length(m)+1), show.tip.label=FALSE)tiplabels(pch=15, col=cols[grps])# get the last plot geometrylastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv)# vector of indices for plotting soil profiles below leaves of dendrogramnew_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 fitsf <- 0.8yoff <- max(lastPP$yy) + 5# add lines at soil depth classesdc <- c(25,50,75,100,150) * sf + yoffabline(h=dc, lty=2, col='grey')profile_plot(m, color="soil_color", plot.order=new_order, cex.id=0.75, max.depth=150,n.depth.ticks=6, scaling.factor=sf, width=0.1, cex.names=0.65, cex.depth.axis=0.75,y.offset=yoff, add=TRUE, name='hzname', id.style='side')# done with the first viewportpopViewport()# move to the next viewportpushViewport(vp.2)# print our lattice graphics hereprint(p.1, newpage=FALSE)# done with this viewportpopViewport(1)X