Gilean0709
Gilean0709

Reputation: 1098

extrafonts-package: problems saving plot as pdf with missing text

I am using the extrafonts-package in R to import the font 'roboto condensed' and use it for my plot. It works as long as I only generate the plot in RStudio, but fails if I want to export the plot as a pdf. I receive numerous warnings like font width unknown for character 0x41, but R still generates a pdf, but without any text. I tried to embed the font aswell, but if I call embed_fonts() nothing happens and R gets stuck in the function-call until I escape it.

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

Upvotes: 4

Views: 1423

Answers (1)

jarauh
jarauh

Reputation: 2006

For me, it worked to use cairo_pdf instead of pdf. Try:

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

Upvotes: 3

Related Questions