Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change font in ggplot2 on Windows vs Mac

Tags:

r

fonts

ggplot2

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!

like image 746
FreshF Avatar asked Dec 10 '13 13:12

FreshF


People also ask

Can you change the font in ggplot2?

ggplot allows you to change the font of each part of the figure: you just need to know the correct option to modify in the theme. (For a full list of customizable components of the theme, see this documentation.)

Are fonts different for Mac and PC?

The Macintosh components (which includes the . AFM file) and the Windows components (. PFB and . PFM files) are all included in an OpenType font file, which means you can install and use the same font file on both Windows and Macintosh computers.

Why are fonts different on Windows and Mac?

Mac OS X tries to render fonts exactly as they were designed, whereas Windows tries to alter them slightly to make them more readable.


1 Answers

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.

like image 50
jimjamslam Avatar answered Oct 12 '22 03:10

jimjamslam