title: Combining multiple complex plots as panels in a single figure
body: <h3>Introduction by @backlin</h3>
  
  <p>Multiple simple plots can combined as panels in a single figure by using <code>layout</code> or <code>par(mfrow=...)</code>. However, more complex plots tend to setup their own panel layout internally disabling them from being used as panels. Is there a way to create a nested layout and encapsulating a complex plot into a single panel?</p>
  
  <p>I have a feeling the <code>grid</code> package can accomplish this, e.g. by ploting the panels in separate viewports, but haven't been able to figure out how. Here is a toy example to demonstrate the problem:</p>
  
  <pre><code>my.plot &lt;- function(){
      a &lt;- matrix(rnorm(100), 10, 10)
      plot.new()
      par(mfrow=c(2,2))
      plot(1:10, runif(10))
      plot(hclust(dist(a)))
      barplot(apply(a, 2, mean))
      image(a)
  }
  layout(matrix(1:4, 2, 2))
  for(i in 1:4) my.plot()
  # How to avoid reseting the outer layout when calling `my.plot`?
  </code></pre>
  
  <h2>Original question by @alittleboy</h2>
  
  <p>I use the <code>heatmap.2</code> function in the <code>gplots</code> package to generate heatmaps. Here is a sample code for a single heatmap:</p>
  
  <pre><code>library(gplots)
  row.scaled.expr &lt;- matrix(sample(1:10000),nrow=1000,ncol=10)
  heatmap.2(row.scaled.expr, dendrogram ='row',
            Colv=FALSE, col=greenred(800), 
            key=FALSE, keysize=1.0, symkey=FALSE, density.info='none',
            trace='none', colsep=1:10,
            sepcolor='white', sepwidth=0.05,
            scale="none",cexRow=0.2,cexCol=2,
            labCol = colnames(row.scaled.expr),                 
            hclustfun=function(c){hclust(c, method='mcquitty')},
            lmat=rbind( c(0, 3), c(2,1), c(0,4) ), lhei=c(0.25, 4, 0.25 ),                 
  )
  </code></pre>
  
  <p>However, since I want to compare multiple heatmaps in a single plot, I use <code>par(mfrow=c(2,2))</code> and then call <code>heatmap.2</code> four times, i.e.</p>
  
  <pre><code>row.scaled.expr &lt;- matrix(sample(1:10000),nrow=1000,ncol=10)
  arr &lt;- array(data=row.scaled.expr, dim=c(dim(row.scaled.expr),4))
  par(mfrow=c(2,2))
  for (i in 1:4)
  heatmap.2(arr[ , ,i], dendrogram ='row',
            Colv=FALSE, col=greenred(800), 
            key=FALSE, keysize=1.0, symkey=FALSE, density.info='none',
            trace='none', colsep=1:10,
            sepcolor='white', sepwidth=0.05,
            scale="none",cexRow=0.2,cexCol=2,
            labCol = colnames(arr[ , ,i]),                 
            hclustfun=function(c){hclust(c, method='mcquitty')},
            lmat=rbind( c(0, 3), c(2,1), c(0,4) ), lhei=c(0.25, 4, 0.25 ),                 
  )
  </code></pre>
  
  <p>However, the result is NOT four heatmaps in a single plot, but four separate heatmaps. In other words, if I use <code>pdf()</code> to output the result, the file is four pages instead of one. Do I need to change any parameters somewhere? Thank you so much!</p>