导入后在 R 中不可用的字体

Hil*_*lde 6 fonts r ggplot2

我在 R 环境中导入字体时遇到了一些问题。我的最终目标是将我公司的自定义字体(.ttf 文件)R 包含在 ggplot 和 RMarkdown 中。我尝试分解问题并注意到导入常规 Windows 字体时出现同样的问题。导入不会引发任何错误,但字体不可用于绘图。我正在使用在 Windows 10 Pro 1803 上运行的 R 版本 3.5.1。

我已经尝试使用 extrafont 包以及使用 showtext 包导入 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

Run Code Online (Sandbox Code Playgroud)

更新:我还尝试使用包 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

Run Code Online (Sandbox Code Playgroud)

更新我发现使用 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) 

Run Code Online (Sandbox Code Playgroud)

我在 Windows 系统方面的经验有限,所以我真的不知道从哪里开始。如果此消息是重复的,我深表歉意 - 我无法在 Windows 中找到任何类似的问题。任何帮助将不胜感激!

解决了:

该命令loadfonts(device = "win")对于正确准备要使用的字体是必要的。加载字体后添加此项。似乎 font_import 是“install_packages()”提示,而 loadfonts() 是“库”提示......这应该有效:

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

Run Code Online (Sandbox Code Playgroud)

Bru*_*oto 5

您可以将 RStudio 的图形设备修改为 AGG 并以无缝方式使用字体。就像默认的一样(仅更改“family” theme())。只需安装该ragg软件包并按照以下步骤操作:

Tools > Global options > General > Graphics > Backend: AGG

文档: https: //ragg.r-lib.org/

  • 就是这个 !!!我已经寻找解决方案有一段时间了,因为 `loadfonts(device = "win")` 在我的情况下不起作用!太感谢了 (2认同)

Art*_*tem 2

由OP发布。

该命令loadfonts(device = "win")对于正确准备要使用的字体是必要的。加载字体后添加此内容。似乎font_importinstall_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"))
Run Code Online (Sandbox Code Playgroud)