title: How to export arrangeGrob output via win.metafile()
body: <p>I need to save plots from R as EMF format (windows metafile format) because this makes the chart look good on screen and paper in Microsoft Word. No other option (PNG, postscript etc) works well on both. The <code>PNG</code> device produces poor res plots. Tinkering with <code>res</code> parameters blows up the graph elements and I can't find anything that clearly explains how to mitigate this. Using <code>postscript</code> print output is pretty good. However, Word's EPS filters are busted so that I can't see the EPS file on screen. I need something that works well on screen and on paper. <code>win.metafile</code> is only thing that does both.</p>
  
  <p>Here's the twist. I am using <code>gridExtra</code> to customise the layout of my plots. From what I gather, this means that I am writing multiple plots onto one device (which I then want to export to EMF). But I know that <code>win.metafile</code> only allows one plot per file. From <code>?win.metafile</code>:</p>
  
  <blockquote>
    <p>For win.metafile only one plot is allowed per file</p>
  </blockquote>
  
  <p>So the following shouldn't work:</p>
  
  <pre><code>library(ggplot2)
  library(gridExtra)
  # g_legend pinched from Hadley:
  # https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
  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)
  }
  win.metafile(file='test.emf', width=6, height=4)
  p &lt;- ggplot(mtcars, aes(x=cyl, y=mpg, colour=factor(gear)))
  pl &lt;- p + geom_point()
  legend &lt;- g_legend(pl)
  lwidth &lt;- sum(legend$width)
  pp &lt;- arrangeGrob(pl + theme(legend.position="none"), legend)
  pp
  dev.off()
  </code></pre>
  
  <p>In fact I get the following error message:</p>
  
  <blockquote>
    <p>Error in grid.newpage() : metafile 'test.emf' could not be created</p>
  </blockquote>
  
  <p>Ok. So here's my question: <strong>how can I trick <code>win.metafile</code> to see only one plot from the arrangeGrob output</strong>? Can I stuff its output into <em>something</em> and get <em>one</em> plot out? It must be possible because if I use RStudio's export function, I get an excellent looking chart on screen and paper. But I want to codify this so I don't have to manually export the files. </p>
  
  <p>I've scoured the web and haven't been able to find anything that addresses this. Help would be greatly appreciated!</p>