extrafonts-package:将绘图保存为pdf且缺少文本的问题

时间:2018-02-21 14:23:36

标签: r fonts

我使用R中的extrafonts-package导入字体' roboto浓缩'并将它用于我的情节。只要我在RStudio中生成绘图,它就可以工作,但如果我想将绘图导出为pdf,则会失败。我收到了许多警告,如font width unknown for character 0x41,但R仍会生成pdf,但没有任何文字。我试图嵌入字体,但如果我调用embed_fonts()没有任何反应,R卡在函数调用中,直到我逃脱它。

library(extrafont)
font_import(pattern = "Roboto")

# creates plot in R with correct font
loadfonts(device = "win")
par(mar = c(0, 0, 0, 0), pin = c(4, 1), xpd = TRUE, family = "Roboto Condensed")

plot(1, type = "n", xlim = c(.5, 5.5), ylim = c(0, 1), xaxt = "n", yaxt = "n", xlab = "", ylab = "")
abline(v = 1:5, xpd = FALSE, col = "grey")
text(x = 1:5, y = .5, labels = LETTERS[1:5])


# creates plot and tries to export it as a pdf - fails
loadfonts(device = "pdf")
pdf(file = "test.pdf", family = "Roboto Condensed")

par(mar = c(0, 0, 0, 0), pin = c(4, 1), xpd = TRUE, family = "Roboto Condensed")
plot(1, type = "n", xlim = c(.5, 5.5), ylim = c(0, 1), xaxt = "n", yaxt = "n", xlab = "", ylab = "")
abline(v = 1:5, xpd = FALSE, col = "grey")
text(x = 1:5, y = .5, labels = LETTERS[1:5])

dev.off()

# embedding the font into the pdf forces me to escape the function call, because nothing happens
Sys.setenv(R_GSCMD = "D:/Programme/gs/gs9.02/bin/gswin64.exe")
embedFonts("test.pdf")

enter image description here

1 个答案:

答案 0 :(得分:1)

对我来说,使用cairo_pdf代替pdf是可行的。试试:

cairo_pdf(file = "test.pdf", family = "Roboto Condensed")