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

Posted

技术标签:

【中文标题】在 Windows 和 Mac 上更改 ggplot2 中的字体【英文标题】:Change font in ggplot2 on Windows vs Mac 【发布时间】:2013-12-28 01:56:07 【问题描述】:

我在我的 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()

我收到这样的警告(英文:~“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

这是在 Windows 上的结果:

还有:为什么图表的线条在 Mac 上看起来比在 Windows 上平滑很多?

有人可以帮忙吗?谢谢!

【问题讨论】:

相关:***.com/questions/34522732/changing-fonts-in-ggplot2 【参考方案1】:

在渲染情节之前使用loadfonts(device="win"),而不仅仅是loadfonts(),对我有用。我不确定为什么后者(或根本没有调用)在 OS X 上就足够了;我只能假设发生了某种默认设备。

【讨论】:

以上是关于在 Windows 和 Mac 上更改 ggplot2 中的字体的主要内容,如果未能解决你的问题,请参考以下文章

怎么在mac上更改移动硬盘的权限

如何以编程方式更改 Windows 可执行文件和 Mac 应用程序的图标?

Android Studio:使用 Mac (OSX) 和 Windows 的同一项目的 SDK 位置更改

电脑网卡MAC地址的更改

ZeroTier One Mac 更改 IP

如何更改苹果mac电脑各个应用的图标