截至2020年和R版本4.0.3,在Mac OS X上,至少在涉及西里尔字母时,
cairo_pdf()
不再是你的朋友,请参见下面的失败图库。如果必须使用西里尔字母,请返回到传统的
png
驱动程序。(并且放弃抗锯齿图表)。
R -e 'png(filename = "ftw.png"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
open ftw.png
![What is old, is new again.](https://istack.dev59.com/TMCrH.webp)
或者如果您使用knitr与Rmarkdown:
R -e 'rmarkdown::render("foo.Rmd", "pdf_document", output_file="foo.pdf", runtime = "static", output_options = list(dev = "png"))'
失败的画廊
如下所示,使用Cairo的“现代”方法在v4.0.3中失败。请注意,这不是(或不仅是)字体嵌入或呈现问题,因为从生成的PDF中选择和粘贴文本也会产生乱码输出。
准备步骤:
- 安装最新的R(版本4.0.3或更高版本,并且所有
capabilities()
都显示TRUE
)
R -e 'install.packages(c("Cairo", "ggplot2"), repos="https://cloud.r-project.org")'
香草配置
R -e 'library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); ggsave("fail1.pdf")'
open fail1.pdf
![Fail Gallery: vanilla config](https://istack.dev59.com/SVwOb.webp)
仅使用cairo_pdf()
R -e 'cairo_pdf("fail2.pdf"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
open fail2.pdf
![Fail Gallery: using cairo_pdf() alone](https://istack.dev59.com/yiwmT.webp)
使用自定义(假定符合Unicode标准)字体的cairo_pdf()
R -e 'cairo_pdf("fail3.pdf", family = "Arial Unicode MS"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
open fail3.pdf
![This is as close as it gets to working with “modern” approaches.](https://istack.dev59.com/6XLOh.webp)
再试一次Comic Sans字体以确保效果:
R -e 'cairo_pdf("fail3bis.pdf", family = "Comic Sans MS"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
open fail3bis.pdf
![Fail Gallery: using cairo_pdf() with family = "MS Comic Sans"](https://istack.dev59.com/6vAPO.webp)
还有一些...
使用旧版的“Dark and Stormy Night”(3.6.2):
/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/R -e 'cairo_pdf("fail4.pdf", family = "Arial Unicode MS"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
open fail4.pdf
![enter image description here](https://istack.dev59.com/f5yd4.webp)
使用@drammock建议的DejaVu Sans
字体:
R -e 'cairo_pdf("fail5.pdf", family = "DejaVu Sans"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
open fail5.pdf
![enter image description here](https://istack.dev59.com/XLIe8.webp)
在旧版 R 上使用 DejaVu Sans:
/Library/Frameworks/R.framework/Versions/3.6/Resources/bin/R -e 'cairo_pdf("fail5bis.pdf", family = "DejaVu Sans"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
open fail5bis.pdf
![enter image description here](https://istack.dev59.com/bAUsA.webp)
cairo_pdf("t.pdf"); ...; dev.off()
对我有效...请参考 https://dev59.com/OW025IYBdhLWcg3wpXod。 - Ben Bolker