21

I have been using the following guide to export plots made with ggplotto pdf: plot fonts guide

It raises the issue at the bottom of the post of some fonts not appearing as they should, which happens in my example below. The text in font Bauhaus 93 appears correctly whilst the text in Calibri is displayed incorrectly.

Has anyone found a way to resolve this issue?

library(ggplot2)
library(plyr)
library(grid)
library(gridExtra)
library(extrafont)

data1<-as.data.frame(1:5)
data1[,2]<-as.data.frame(c(1,3,5,7,9))
data1[,3]<-as.data.frame(c(2,4,6,8,10))
colnames(data1)<-c("x","y1","y2")

ggplot(data1, aes(x=x)) + 
  geom_line(aes(y = y1, colour = "Taux selon DEF"), size=0.61, colour="black") +  
  geom_line(aes(y = y2, colour = "Taux selon EC"), size=0.61, colour="black", linetype="dashed") + 
  xlab("X axis lab") + ylab("Y axis lab)") +
  annotate("text", x=1, y=4, label="Some text here", size=2, family="Bauhaus 93") +
  annotate("text", x=4, y=1, label="More text here", size=2, family="Calibri") +
  theme_bw() + theme(legend.title = element_blank(),
                     legend.key = element_rect(fill=NA),
                     panel.border = element_blank(), 
                     axis.line = element_line(colour="black", size=0.25),
                     axis.ticks = element_line(size=0.25),
                     panel.grid.major = element_line(colour = "grey80", size=0.25),
                     panel.grid.minor = element_line(colour = "grey80", size=0.25),
                     axis.text.x = element_text(size=5.5 , lineheight=0.9, hjust=0.5, family="Bauhaus 93"),
                     axis.text.y = element_text(size=5.5 , lineheight=0.9, vjust=0.5, family="Calibri"),
                     axis.title.y = element_text(size=6.1, angle=0, vjust=0.975, face="bold", family="Calibri"),
                     axis.title.x = element_text(size=6.1, angle=0, vjust=-0.20, face="bold", family="Calibri")) +
  scale_x_continuous(expand = c(0, 0), limits=c(0,5)) + 
  scale_y_continuous(expand = c(0, 0), limits=c(0,10)) +
  ggtitle("Title") +
  ggsave("Test.pdf", width=7, height=5)
Sys.setenv(R_GSCMD = "C:/Program Files (x86)/PDF24/gs/bin/gswin32.exe")
embed_fonts("Test.pdf")
user2568648
  • 3,001
  • 8
  • 35
  • 52
  • Initially I had the same error, but now I cannot reproduce it! What does the output of `fonttable()` look like? – Nicholas Jan 09 '15 at 15:53
  • The folowing output is given for the calibri family `65 Calibri Calibri FALSE FALSE FALSE NA 66 Calibri Calibri-Bold TRUE FALSE FALSE NA 67 Calibri Calibri-BoldItalic TRUE TRUE FALSE NA 68 Calibri Calibri-Italic FALSE TRUE FALSE NA 69 Calibri Light Calibri-Light FALSE FALSE FALSE NA 70 Calibri Light Calibri-LightItalic FALSE TRUE FALSE NA` – user2568648 Jan 12 '15 at 08:20
  • 1
    OK, I've been able to reproduce the problem on Windows: it produces a load of "font width unknown" warnings and all the Calibri characters are jumbled on top of each other. There are a number of related extrafont issues here: https://github.com/wch/extrafont/issues Apart from using a different font, I don't know what to suggest! – Nicholas Jan 19 '15 at 17:56
  • Ok, thanks for your efforts. I'll stick to using another font. – user2568648 Jan 20 '15 at 08:06

1 Answers1

38

Try adding device=cairo_pdf to the ggsave() call. This appears to solve the problem for me. This way, it's no longer necessary to use embed_fonts().

See mgaudet's comment here: https://github.com/wch/extrafont/issues/8

tonytonov
  • 25,060
  • 16
  • 82
  • 98
Nicholas
  • 1,416
  • 1
  • 15
  • 15
  • 2
    I also found that using `device=cairo_pdf` embeds the font, but this results in worse kerning – anyone know of a better solution? – rvrvrv Apr 23 '15 at 13:53
  • @user1092247 Perhaps the font you've requested doesn't have kerning support. Remember that kerning is done by the font designer, though some systems ignore this. – Hugh May 13 '16 at 02:25
  • 1
    Great answer. I found I had to increase the ``width`` and ``height`` arguments inside ``ggsave`` to get a result similar to what I used to get without ``device=cairo_pdf`` on my system before a clean-update to ``MacOS Sierra``. – PatrickT Jul 02 '17 at 17:44
  • 2
    Newer versions of R don't support cairo anymore, which gets you `failed to load cairo DLL`. Any new ideas how to solve this? – cw' Mar 25 '19 at 22:37
  • @cw' Try installing the Cairo package and then use `device = CarioPDF` in your `ggsave()` call. On Windows it was straightforward. On Ubuntu it was necessary to install `libharfbuzz-dev` and `libxt-dev` before Cairo would compile. – Nicholas Jul 28 '23 at 15:18