在R环境中导入字体时遇到一些麻烦。我的最终目标是包括在ggplot和RMarkdown中使用我公司的自定义字体(.ttf文件)R。我尝试解决此问题,并注意到导入常规Windows字体时也会发生相同的问题。导入不会引发任何错误,但是字体不可用于绘图。我正在Windows 10 Pro 1803上运行R版本3.5.1。
我尝试使用extrafont包以及showtext packakge导入Windows字体。我还尝试将所有Windows ttf文件从C:\ WINDOWS \ Fonts手动复制到C:\ Users ... \ Documents \ R \ R-3.5.1 \ library \ extrafontdb \ metrics,此问题仍然存在。 >
有些带有R基本数据集的代码块会引发错误:
library(ggplot2)
library(extrafont)
font_import()
# Only three fonts seem to have been imported...
loadfonts(); windowsFonts()
#$`serif`
#[1] "TT Times New Roman"
#$sans
#[1] "TT Arial"
#$mono
#[1] "TT Courier New"
ggplot(data = esoph) +
aes(x = agegp, weight = ncases) +
geom_bar() +
ggtitle("This is a title") +
theme(plot.title = element_text(size = 14, family = "Calibri"))
#Warning messages:
#1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
# font family not found in Windows font database
#2: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
# font family not found in Windows font database
#3: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
# font family not found in Windows font database
更新:我也尝试使用showtext包而不是extrafont进行导入。
library(ggplot2)
library(showtext)
# Check the current search path for fonts
font_paths()
# [1] "C:\\WINDOWS\\Fonts"
# List available font files in the search path
font_files()
# path file family face
# 1 C:/WINDOWS/Fonts AGENCYB.TTF Agency FB Bold
# 2 C:/WINDOWS/Fonts AGENCYR.TTF Agency FB Regular
# 3 C:/WINDOWS/Fonts ALGER.TTF Algerian Regular
# 166 C:/WINDOWS/Fonts GeorgiaPro-SemiBold.ttf Georgia Pro Semibold
# Add one of these fonts
font_add("Agency FB", "AGENCYB.ttf")
font_families()
# [1] "sans" "serif" "mono" "wqy-microhei" "Calibri" "CalistoMT" "Agency FB"
showtext_auto()
ggplot(data = esoph) +
aes(x = agegp, weight = ncases) +
geom_bar() +
ggtitle("This is a title") +
theme(plot.title = element_text(size = 14, family = "Agency FB"))
#Warning messages:
#1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
# font family not found in Windows font database
#2: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
# font family not found in Windows font database
#3: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
# font family not found in Windows font database
#4: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
# font family not found in Windows font database
#5: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
# font family not found in Windows font database
#6: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
# font family not found in Windows font database
#7: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
# font family not found in Windows font database
#8: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
# font family not found in Windows font database
#9: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
# font family not found in Windows font database
#10: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
# font family not found in Windows font database
#11: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
# font family not found in Windows font database
#12: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
# font family not found in Windows font database
更新我发现,使用showtext,可以从 Google导入字体。但是,当我尝试在本地工作时(Windows或自定义字体,字体无法正确导入。这可以起作用:
library(showtext)
library(ggplot2)
font_add_google("Quattrocento Sans", "Quattrocento Sans")
showtext_auto()
windows()
a <- ggplot(data = esoph) +
aes(x = agegp, weight = ncases) +
geom_bar() +
ggtitle("This is a title") +
theme(plot.title = element_text(size = 14, family = "Quattrocento Sans"))
print(a)
我在Windows系统上的经验有限,所以我真的不知道从哪里开始。如果此消息重复,我深表歉意。我无法在Windows上找到类似的问题。任何帮助将不胜感激!
已解决:
必须使用命令loadfonts(device = "win")
来正确准备要使用的字体。加载字体后添加它。好像font_import是“ install_packages()”提示,而loadfonts()是“ library”提示...这应该起作用:
library(ggplot2)
library(extrafont)
font_import()
loadfonts(device = "win")
ggplot(data = esoph) +
aes(x = agegp, weight = ncases) +
geom_bar() +
ggtitle("This is a title") +
theme(plot.title = element_text(size = 14, family = "Calibri"))