在Ggplot2中使用自定义OTF字体

Rod*_*phe 9 macos fonts r ggplot2 typeface

我需要在R中使用自定义字体,即"Archer"和ggplot2.Archer是我系统上安装的otf字体(Mac OSX Yosemite).

这个脚本(在这里找到:在ggplot2中修改字体)不适用于Archer,但适用于其他字体,如Arial.

install.packages("extrafont");library(extrafont)
font_import("Archer")
library(ggplot2)
qplot(1:10)+theme(text=element_text(family="Archer"))
Run Code Online (Sandbox Code Playgroud)

otf字体有什么问题吗?

yix*_*uan 14

您可以尝试showtext包,它直接适用于OTF字体.

library(showtext)
font.add("Archer", "Archer.otf")
showtext.auto()
library(ggplot2)
qplot(1:10)+theme(text=element_text(family="Archer"))
Run Code Online (Sandbox Code Playgroud)

请用系统中Archer字体的真实文件名替换"Archer.otf" .

使用showtext不需要嵌入字体.


Gre*_*gor 11

你需要将Archer从OTF转换为TTF.从extrafontgithub上的自述:

目前它允许使用带有R的TrueType字体

我很开心 - 我必须这样做,因为我的组织也使用Archer.这个搜索的第一个打击是紫色,所以可能是我使用的,它工作得很好.

如果遇到问题,extrafont查看可用选项也很有用fonts().然后,您可以验证导入是否成功.

如果将绘图保存为PDF,请确保也使用grDevices::embedFonts或嵌入字体extrafont::embed_fonts.