导入后R中的字体不可用

导入后R中的字体不可用,r,ggplot2,fonts,R,Ggplot2,Fonts,在R环境中导入字体时遇到问题。我的最终目标是包括我公司的自定义字体(.ttf文件)R,以便在ggplot和RMarkdown中使用。我尝试过解决这个问题,并注意到导入常规Windows字体时也会出现同样的问题。导入不会引发任何错误,但字体不可用于打印。我使用的是运行在Windows10Pro 1803上的R版本3.5.1 我尝试过使用extrafont软件包和showtext软件包导入Windows字体。我还尝试将所有Windows ttf文件从C:\Windows\Fonts手动复制到C:\U

在R环境中导入字体时遇到问题。我的最终目标是包括我公司的自定义字体(.ttf文件)R,以便在ggplot和RMarkdown中使用。我尝试过解决这个问题,并注意到导入常规Windows字体时也会出现同样的问题。导入不会引发任何错误,但字体不可用于打印。我使用的是运行在Windows10Pro 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

更新:我也尝试过使用PackageShowText而不是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或自定义字体),字体无法正确导入。这确实有效:


您是否只为
R
安装了
Calibri
?当您键入
loadfonts();windowsFonts()
时,是否看到
Calibri
?Hi Tung,谢谢您的回答!我发现了这个问题,这对我也不起作用:
font\u import(pattern=“Calibri”,prompt=FALSE)
。正在运行
loadfonts();windowsFonts()
仅返回“TT Times New Roman”“TT Arial”和“TT Courier New”,因此似乎未导入任何内容..?如果一切都失败,请尝试一下showtext,设法修复它!我认为使用
loadfonts(device=“win”)加载工作不正常
我访问了所有的windows字体,它们是使用
loadfonts();windowsFonts()
显示的。感谢您的帮助
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) 

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