在Windows和Mac上更改ggplot2中的字体

16

我在我的Mac电脑上使用ggplot2创建了一张图表。我将字体更改为Times New Roman,效果很好。

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))))

这是Mac上的结果:

在此输入图像描述

我需要将图表导出为WMF格式,因此我在Windows上使用了R Studio,但无法更改字体为Times New Roman。除了上面的代码之外,我还尝试了以下操作,但没有成功。

library(extrafont)
font_import()
loadfonts()

我收到这样的警告(用英文表示:“在Windows字体数据库中找不到字体系列”)

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

这是在Windows上的结果:

输入图像描述

还有:为什么图形的线在Mac上看起来比在Windows上更加平滑?

有人可以帮忙吗?谢谢!


3
相关:https://dev59.com/V1sW5IYBdhLWcg3w2aOK - Richie Cotton
1个回答

9

在渲染图表之前,使用loadfonts(device="win")而不是只使用loadfonts()对我有用。我不确定为什么后者(或根本不调用)在OS X上已经足够了;我只能假设存在某种默认设备的情况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接