【发布时间】:2019-03-05 18:27:23
【问题描述】:
使用 Cairo 保存 R 图形有很多优点 (see here, for example)。例如,在保存 PDF 时,cairo_pdf 设备会正确嵌入自定义字体。
cairo_pdf 图形设备的使用很容易,基于 ggplot 的图形与 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 前端:
---
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 也有优势,因为 Cairo correctly deals with DPI。如果将正常保存的高 DPI 的 PNG 放入 Word 或 PowerPoint 文件中,则图形的尺寸会被夸大且不准确。如果将具有相同高 DPI 的基于开罗的 PNG 放入 Word 中,则尺寸正确:
使用ggsave() 很容易将 ggplot 输出保存为高分辨率 Cairo PNG,但语法与保存为 Cairo PDF 略有不同。我们不指定设备,而是指定类型:
ggsave(ugly_plot, filename = "ugly_plot.png",
width = 4, height = 2.5, dpi = 300, type = "cairo")
将该文件放在 Word 或 PowerPoint 中效果很好,并且所有内容的大小都在高分辨率下正确。
当编织成 HTML 或 Word 时,这种对尺寸的误解会延续到 R Markdown 中。在编织时让 knitr 使用 type = "cairo" 会很棒,但是在 R Markdown 中复制这个 dpi = 300, type = "cairo" 会更加困难。 Cairo 库包括像Cairo::CairoPNG() 这样的设备,但ggsave(..., type = "cairo") 不使用这个设备。它使用 R 的标准 PNG 设备,但开启了对 Cairo 的支持。
将dpi=300 添加到块选项中很容易使图形高分辨率,但我无法让knitr 使用启用type = cairo 的内置PNG 设备。我尝试天真地将type: cairo 添加到 YAML 元数据中,但毫无疑问它不起作用。 knitr 生成的 PNG 不使用 Cairo,并且比预期的要大得多(并且在 HTML 和 Word 文档中是巨大的)。
---
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") 获得的相同输出。有没有办法做到这一点?
---
title: "Something"
output:
pdf_document:
dev: cairo_pdf # yay Cairo output
html_document: # What needs to go here?
dev: png
type: cairo
---
【问题讨论】:
-
这可能是由于与
dev.args而不仅仅是dev,如下面我的回答??
标签: r ggplot2 r-markdown knitr cairo