本文介绍了如何在R Markdown中使用Cairo PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Cairo保存R图形有很多优点(请参阅此处,例如).例如,保存PDF时,cairo_pdf设备会正确嵌入自定义字体.

There are many advantages to using Cairo to save R graphics (see here, for example). When saving PDFs, for instance, the cairo_pdf device correctly embeds custom fonts.

使用cairo_pdf图形设备对带有ggsave()的基于ggplot的图形很容易:

Using the cairo_pdf graphics device is easy with ggplot-based graphics with ggsave():

library(ggplot2)

ugly_plot <- 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)

在R Markdown中通过knitr使用cairo_pdf设备也很容易-将dev: cairo_pdf添加到YAML前端:

Using the cairo_pdf device in R Markdown with knitr is also easy—add dev: cairo_pdf to the YAML front matter:

---
title: "Cairo stuff"
output:
  pdf_document:
    dev: cairo_pdf
---

```{r make-ugly-plot, fig.width=4, fig.height=2.5}
library(ggplot2)

ugly_plot <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(title = "Some data about cars") +
  theme_gray(base_family = "Papyrus")
ugly_plot
```

使用基于开罗的PNG也有优势,因为开罗正确处理DPI .如果将具有高DPI的正常保存的PNG放置到Word或PowerPoint文件中,则图形的尺寸会被夸大且不准确.如果将具有相同高DPI的基于Cairo的PNG放入Word中,则尺寸是正确的:

There are also advantages to using Cairo-based PNGs, since Cairo correctly deals with DPI. 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:

使用ggsave()可以很容易地将ggplot输出保存为高分辨率Cairo PNG,但是语法与另存为Cairo PDF稍有不同.我们没有指定设备,而是指定类型:

Saving ggplot output as high resolution Cairo PNGs is easy with ggsave(), but the syntax is slightly different from saving as Cairo PDFs. Instead of specifying a device, we specify type:

ggsave(ugly_plot, filename = "ugly_plot.png",
       width = 4, height = 2.5, dpi = 300, type = "cairo")

将该文件放置在Word或PowerPoint中的效果很好,并且所有文件都可以在高分辨率下正确调整大小.

Placing that file in Word or PowerPoint works great and everything is sized correctly at high resolution.

在编织为HTML或Word时,对尺寸的这种误解会延续到R Markdown中.编织时让knitr使用type = "cairo"是很棒的,但是在R Markdown中复制此dpi = 300, type = "cairo"会更困难.开罗库包含诸如Cairo::CairoPNG()之类的设备,但是ggsave(..., type = "cairo")不使用此设备.它使用R的标准PNG设备,但已启用Cairo支持.

This misinterpretation of dimensions carries over into R Markdown when knitting to HTML or Word. It'd be great to have knitr use type = "cairo" when knitting, but replicating this dpi = 300, type = "cairo" in R Markdown, however, is more difficult. The Cairo library includes devices like Cairo::CairoPNG(), but ggsave(..., type = "cairo") doesn't use this device. It uses R's standard PNG device, but with Cairo support turned on.

通过在块选项中添加dpi=300来使图形更容易实现高分辨率,但是我无法让knitr使用启用了type = cairo的内置PNG设备.我尝试过天真地将type: cairo添加到YAML元数据中,但是毫不奇怪,它不起作用. Knitr生成的PNG不使用开罗,并且比预期的要大得多(并且在HTML和Word文档中是巨大的).

Making the figure high resolution is easy enough with adding dpi=300 to the chunk options, but I can't get knitr to use the built-in PNG device with type = cairo enabled. I tried naively adding type: cairo 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).

---
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 <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(title = "Some data about cars") +
  theme_gray(base_family = "Papyrus")
ugly_plot
```


总而言之,我正在寻找一种方法来使用您从knitr中的ggsave(..., filename = "blah.png", dpi = 300, type = "cairo")获得的相同输出.有办法吗?


In sum, I'm looking for a way to use the same output you'd get from ggsave(..., filename = "blah.png", dpi = 300, type = "cairo") in knitr. Is there a way to do this?

---
title: "Something"
output:
  pdf_document:
    dev: cairo_pdf  # yay Cairo output
  html_document:  # What needs to go here?
    dev: png
    type: cairo
---

推荐答案

使用knitr选项,而不是YAML标头.

Use knitr options, not the YAML header.

您可以使用knitr选项更改特定设备的类型(逸辉的建议):

You can use knitr options to change the type of a specific device (Yihui's recommendation):

knitr::opts_chunk$set(dev.args = list(png = list(type = "cairo")))

或者,您可以根据输出有条件地执行此操作:

Alternately, you could do it conditionally based on the output:

if (!knitr::is_latex_output()) {
  knitr::opts_chunk$set(dpi = 300, dev.args = list(type = "cairo"))
})

我现在已经在一些文档中使用了它. 注意:我仅将其用于从R命令行执行rmarkdown::render(...)的文档.

I've used this on a couple of documents now. Note: I've only used this for documents doing rmarkdown::render(...) from the R command line.

这篇关于如何在R Markdown中使用Cairo PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 04:58