Change font in ggplot2 on Windows vs Mac

10,753

Using loadfonts(device="win")before rendering the plot, rather than just loadfonts(), worked for me. I'm not sure why the latter (or no call at all) is sufficient on OS X; I can only assume there's some sort of default device thing going on.

Share:
10,753
FreshF
Author by

FreshF

Updated on July 20, 2022

Comments

  • FreshF
    FreshF almost 2 years

    I created a plot using ggplot2 on my mac. I changed the fonts to Times New Roman, which works fine.

    library(extrafont)
    
    ggplot(data=df)+
      stat_density(aes(x=R1, colour="rho = -0,6"), 
                   adjust=4, lwd=0.65, geom="line", position="identity")+
      stat_density(aes(x=R2, colour="rho = 0,6"), 
                   adjust=4, lwd=0.65, geom="line", position="identity")+
      stat_density(aes(x=R3, colour="rho = 0"), 
                   adjust=4, lwd=0.65, linetype=2, geom="line", position="identity")+
      xlim(-1, 1)+
      xlab("Renditen")+
      ylab("Dichte")+
      ggtitle("Renditeverteilung im Heston-Modell")+
      theme(plot.title=element_text(face="bold", size=16, vjust=2, family="Times New Roman"),  
            axis.title.x=element_text(vjust=-1, size=14, family="Times New Roman"),
            axis.title.y=element_text(vjust=-0.25, size=14, family="Times New Roman"), 
            legend.text=element_text(size=14, family="Times New Roman"), legend.title=element_blank(),
            legend.margin=unit(1, "cm"),
            legend.key.height=unit(1, "line"), 
            legend.key.size=unit(0.8, "cm"), 
            legend.key=element_rect(fill=NA), 
            legend.background=element_blank(),
            plot.margin=unit(c(1,1,1,1), "cm"))+
      scale_colour_manual(values=c("red","black", "blue"), labels=greeks_rho)+
      guides(colour=guide_legend(override.aes=list(linetype=c(1,3,1))))
    

    This is the result on Mac:

    enter image description here

    I need to export the plot in the WMF format so I used R Studio on Windows, where I cannot change the font to Times New Roman. I did the following in addition to the above code without success.

    library(extrafont)
    font_import()
    loadfonts()
    

    I get warnings like this (in English: ~ "Font Family not found in Windows Font Database")

    47: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label),  ... :
      Zeichensatzfamilie in der Windows Zeichensatzdatenbank nicht gefunden
    

    This is the result on Windows:

    enter image description here

    And: Why do the lines of the graphs look a lot smoother on Mac than on Windows?

    Can somebody help here? Thank You!