title: Inserting a table under the legend in a ggplot2 and saving everything to a file
body: <p>I have the following data.frame which I would like to plot with ggplot2:</p>
  
  <pre><code>df = data.frame(mean=c(1.96535,2.133604,1.99303,1.865004,2.181713,1.909511,2.047971,1.676599,2.143763,1.939875,1.816028,1.95465,2.153445,1.802517,2.141799,1.722428),
  sd=c(0.0595173,0.03884202,0.0570006,0.04934336,0.04008221,0.05108064,0.0463556,0.06272475,0.04321496,0.05283728,0.05894342,0.05160038,0.04679423,0.05305525,0.04626291,0.0573123),
  par=as.factor(c("p","p","m","m","p","p","m","m","m","m","p","p","m","m","p","p")),
  group=as.factor(c("iF","iF","iF","iF","iM","iM","iM","iM","RF","RF","RF","RF","RM","RM","RM","RM")),
  rep=c(1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2))
  </code></pre>
  
  <p>This is my code for doing that:</p>
  
  <pre><code>p &lt;- ggplot(df,aes(factor(rep),y=mean,ymin=mean-2*sd,ymax=mean+2*sd,color=factor(par)))
  p &lt;- p + geom_pointrange()+facet_wrap(~group, ncol = 4)+scale_color_manual(values = c("blue","red"),labels = c("p","m"),name = "par id")
  p &lt;- p + ggtitle("test")
  p &lt;- p + labs(y="log(y)",x="rep")
  </code></pre>
  
  <p>which produces this figure:
  <img src="https://i.stack.imgur.com/gXJYq.jpg" alt="enter image description here"></p>
  
  <p>I would like to add this data.frame under the legend:</p>
  
  <pre><code>leg.df = data.frame(statistic = c("pp(par)","pp(g)","pp(s)","fc(p/m)"), value = c(0.96,0.94,0.78,1.5))
  </code></pre>
  
  <p>I followed this <a href="https://stackoverflow.com/questions/11883844/inserting-a-table-under-the-legend-in-a-ggplot2-histogram">stackoverflow thread</a> for inserting a table under the legend in a ggplot2 using this code:</p>
  
  <pre><code>g_legend &lt;- function(a.gplot){
      tmp &lt;- ggplot_gtable(ggplot_build(a.gplot))
      leg &lt;- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
      legend &lt;- tmp$grobs[[leg]]
      return(legend)
  }
  legend &lt;- g_legend(p)
  grid.newpage()
  vp1 &lt;- viewport(width = 0.75, height = 1, x = 0.375, y = .5)
  vpleg &lt;- viewport(width = 0.25, height = 0.5, x = 0.85, y = 0.75)
  subvp &lt;- viewport(width = 0.3, height = 0.3, x = 0.85, y = 0.25)
  print(p + theme(legend.position = "none"), vp = vp1)
  upViewport(0)
  pushViewport(vpleg)
  grid.draw(legend)
  upViewport(0)
  pushViewport(subvp)
  my_table &lt;- tableGrob(leg.df, gpar.coretext = gpar(fontsize=8), gpar.coltext=gpar(fontsize=8), gpar.rowtext=gpar(fontsize=8), show.rownames = FALSE) 
  grid.draw(my_table)
  </code></pre>
  
  <p>which produces exactly what I want:
  <img src="https://i.stack.imgur.com/85ZhC.jpg" alt="enter image description here"></p>
  
  <p>The only problem is that I cannot get it to print everything to a file. I tried specifying <code>pdf(&lt;file&gt;)</code> in front of the entire block of code and <code>dev.off()</code> after it, but that doesn't seem to work. So I can only get it to print to the R screen.</p>
  
  <p>So I guess I should be handling the legend part differently but I have no idea how.</p>
  
  <p>Any suggestions?</p>