2

I recently learned that I need to keep the font of my thesis consistent with that of my embedded figures. Since I've decided on using Times New Roman for the body of my text, I need to revisit my figures and code them with the same font. Unfortunately, what I was thinking would be a simple function has turned into a HUUGE headache. Following the suggestions of others, I went ahead and installed the "extrafont" package and loaded all of the fonts available from my device (I'm running on a Macbook fyi). Upon using all the necessary code I could scrounge from other stack overflow inquiries, I was greeted with a preview of my figure showing the correct font (again, Times New Roman). However, the headache began upon attempting to export it using the ggsave function. Unfortunately, I received the following error:

There were 50 or more warnings (use warnings() to see the first 50)

Upon running the suggested function to learn more about these warnings, I was greeted by a long list of the same repeated error:

> warnings()
Warning messages:
1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family 'Times New Roman' not found in PostScript font database
2: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
 font family 'Times New Roman' not found in PostScript font database
3: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family 'Times New Roman' not found in PostScript font database
4: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family 'Times New Roman' not found in PostScript font database
5: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family 'Times New Roman' not found in PostScript font database
6: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :

And so on. What's going on here and how do I fix it in my code? The code I used is provided below. And yes, upon running the loadfonts(device) function I was appropriately prompted to accept the function (y/n) and I waited until it had fully loaded before moving forward with my code. And yes, I ran the fonts() command immediately after to double check that Times New Roman was on the list. Here's my code:

install.packages("extrafont")
library(extrafont)
font_import()
loadfonts(device)
fonts()

ggplot(plot1, aes(x=ecotype, y=mean, fill=treatment))+
  geom_bar(stat="identity", position="dodge")+
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), position=position_dodge(.9), 
width=.3)+
  scale_fill_manual(values=c("#CA3542","#37AFA9"), 
labels=c("Control","Exclusion"), name="Treatment")+
  labs(x="Ecotype", y="Expected Mean Dry Aboveground Biomass (g)")+
  geom_hline(aes(yintercept=0), size=.3)+
  facet_grid(site~., scales="free")+
  theme_bw()+
  theme(axis.title.x=element_text(size=16))+
  theme(axis.title.y=element_text(size=16))+
  theme(axis.text.x=element_text(size=16))+
  theme(axis.text.y=element_text(size=16))+
  theme(strip.text.y=element_text(size=16))+
  theme(legend.title=element_text(size=12))+
  theme(legend.text=element_text(size=12))+
  theme(text=element_text(family="Times New Roman"))+
  theme(legend.justification = c(0.9,1.1), legend.position=c(0.95,1), 
panel.grid.major = element_blank(), panel.grid.minor = element_blank())
ggsave("plot1.eps", height = 8, width = 5)

A disclaimer: I'm still a relative novice at R, so I would appreciate any advice to be clear and generally within the context of the above code. Thanks in advance!

atsyplenkov
  • 1,158
  • 13
  • 25
  • 1
    Maybe this question will help, do you need `loadfonts(device='win')` https://stackoverflow.com/questions/27689222/changing-fonts-for-graphs-in-r – NColl Dec 07 '18 at 05:38
  • Thanks NColl. I'll take a look at this in a moment. I believe I tried the suggested code, but my understanding is that it's simply a method to specify that you're working with Windows fonts (I'm on a Mac). Correct me if I'm wrong though, I'll look into this further. – Damian Popovic Dec 07 '18 at 06:19
  • You can use `showtext` package as well https://stackoverflow.com/a/51906008/786542 – Tung Dec 07 '18 at 07:50
  • @DamianPopovic what is the output of `fonts()` function? Is there a warning when you export to `png` or another raster format? Otherwise, try to embed fonts with `embed_fonts()` when you exporting plot to .eps – atsyplenkov Dec 07 '18 at 08:52

0 Answers0