0

I'm trying to save multiple plots to one pdf, typically I use:

# creating pdf from four panels
# these aren't reproducible but any ggplot will recreate error essentially
pdf(file = "Fig.1", height = 14, width = 14)
Fig.1<- grid.arrange(p1,p2,p3,p4)

print(Fig.1)
dev.off()

However, I want to use a specific font in the plots.

I have thus downloaded and installed the fontextrapackage, intalled and loaded the fonts with loadfonts(device = "win"). I then use theme(text=element_text(family="Calibri")) within the ggplot call. When I re-run code individual plots will look fine in R itself. I can also successfully use grid.arrange using gridExtra to arrange them and check them within R itself. However, when I use:

pdf(file = "Fig.4.trw.pdf", height = 14, width = 14)
Fig.4.trw<- grid.arrange(p1,p2,p3,p4)

I get a bunch of errors associated to fonts:

In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
font width unknown for character 0x30

I've also tried to use ggsave but that isn't working either, please help!

Thanks

Dasr
  • 777
  • 6
  • 16
  • You probably need `loadfonts(device = "all")` or `"pdf", "postscript"` – Tung Jul 09 '19 at 14:20
  • See this one https://stackoverflow.com/a/51888677/786542 or try [showtext](https://stackoverflow.com/a/51906008/786542) – Tung Jul 09 '19 at 14:21
  • Have tried, it is throwing exactly same errors. – Dasr Jul 09 '19 at 14:25
  • You probably did something wrong. Did the font show up when you run either `font_families()` or `windowsFonts()`? – Tung Jul 09 '19 at 14:31
  • 1
    Yeah it comes up. Weird thing is the plot looks fine before I actually try to save it to PDF. Ok I have managed a workaround that seems to function, I found it scouring the internet. If you use this: `ggsave("Fig1.pdf", Fig.1, width=14, height=14,device=cairo_pdf)` it seems to work. – Dasr Jul 09 '19 at 14:49
  • The syntax you used is in one of the link I posted – Tung Jul 09 '19 at 15:58

0 Answers0