title: How to use Cairo PNGs in R Markdown
body: <p>There are many advantages to using Cairo to save R graphics (<a href="https://www.andrewheiss.com/blog/2017/09/27/working-with-r-cairo-graphics-custom-fonts-and-ggplot/" rel="noreferrer">see here, for example</a>). When saving PDFs, for instance, the <code>cairo_pdf</code> device correctly embeds custom fonts.</p>
  
  <p>Using the <code>cairo_pdf</code> graphics device is easy with ggplot-based graphics with <code>ggsave()</code>:</p>
  
  
  
  <pre class="lang-r prettyprint-override"><code>library(ggplot2)
  
  ugly_plot &lt;- ggplot(mtcars, aes(x = wt, y = mpg)) +
    geom_point() +
    labs(title = "Some data about cars") +
    theme_gray(base_family = "Papyrus")
  ugly_plot
  
  ggsave(ugly_plot, filename = "ugly_plot.pdf", 
         width = 4, height = 2.5, device = cairo_pdf)
  </code></pre>
  
  <p>Using the <code>cairo_pdf</code> device in R Markdown with knitr is also easy—add <code>dev: cairo_pdf</code> to the YAML front matter:</p>
  
  <pre class="lang-r prettyprint-override"><code>---
  title: "Cairo stuff"
  output:
    pdf_document:
      dev: cairo_pdf
  ---
  
  ```{r make-ugly-plot, fig.width=4, fig.height=2.5}
  library(ggplot2)
  
  ugly_plot &lt;- ggplot(mtcars, aes(x = wt, y = mpg)) +
    geom_point() +
    labs(title = "Some data about cars") +
    theme_gray(base_family = "Papyrus")
  ugly_plot
  ```
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ARpmZ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ARpmZ.png" alt="PDF output"></a></p>
  
  <p>There are also advantages to using Cairo-based PNGs, since <a href="http://gforge.se/2013/02/exporting-nice-plots-in-r/#update" rel="noreferrer">Cairo correctly deals with DPI</a>. If you place a normally-saved PNG with a high DPI into a Word or PowerPoint file, the dimensions of the figure are exaggerated and not accurate. If you place a Cairo-based PNG with the same high DPI into Word, the dimensions are correct:</p>
  
  <p><a href="https://i.stack.imgur.com/KAOmj.png" rel="noreferrer"><img src="https://i.stack.imgur.com/KAOmj.png" alt="Weird Word dimensions"></a></p>
  
  <p>Saving ggplot output as high resolution Cairo PNGs is easy with <code>ggsave()</code>, but the syntax is slightly different from saving as Cairo PDFs. Instead of specifying a device, we specify type:</p>
  
  <pre class="lang-r prettyprint-override"><code>ggsave(ugly_plot, filename = "ugly_plot.png", 
         width = 4, height = 2.5, dpi = 300, type = "cairo")
  </code></pre>
  
  <p>Placing that file in Word or PowerPoint works great and everything is sized correctly at high resolution.</p>
  
  <p>This misinterpretation of dimensions carries over into R Markdown when knitting to HTML or Word. It'd be great to have knitr use <code>type = "cairo"</code> when knitting, but replicating this <code>dpi = 300, type = "cairo"</code> in R Markdown, however, is more difficult. The Cairo library includes devices like <code>Cairo::CairoPNG()</code>, but <code>ggsave(..., type = "cairo")</code> doesn't use this device. It uses R's standard PNG device, but with Cairo support turned on.</p>
  
  <p>Making the figure high resolution is easy enough with adding <code>dpi=300</code> to the chunk options, but I can't get knitr to use the built-in PNG device with <code>type = cairo</code> enabled. I tried naively adding <code>type: cairo</code> to the YAML metadata, but it unsurprisingly doesn't work. The PNG that knitr generates doesn't use Cairo and is much larger than expected (and is gigantic in HTML and Word documents).</p>
  
  <pre class="lang-r prettyprint-override"><code>---
  title: "Cairo stuff"
  output:
    html_document: 
      self_contained: no  # to see resulting figure as a file
      dev: png
      type: cairo  # this doesn't do anything
  ---
  
  ```{r make-ugly-plot, fig.width=5, fig.height=3.5, dpi=300}
  library(ggplot2)
  
  ugly_plot &lt;- ggplot(mtcars, aes(x = wt, y = mpg)) +
    geom_point() +
    labs(title = "Some data about cars") +
    theme_gray(base_family = "Papyrus")
  ugly_plot
  ```
  </code></pre>
  
  <hr>
  
  <p>In sum, I'm looking for a way to use the same output you'd get from <code>ggsave(..., filename = "blah.png", dpi = 300, type = "cairo")</code> in knitr. Is there a way to do this?</p>
  
  <pre class="lang-r prettyprint-override"><code>---
  title: "Something"
  output:
    pdf_document:
      dev: cairo_pdf  # yay Cairo output
    html_document:  # What needs to go here?
      dev: png
      type: cairo
  ---
  </code></pre>