将字体添加到Extrafonts库中没有的R中

Can*_*ice 5 fonts r ggplot2

安装R的extrafonts库并检查它必须提供的字体后,它提供了以下列表:

[1] ".Keyboard"               "System Font"             "Andale Mono"             "Apple Braille"           "AppleMyungjo"           
[6] "Arial Black"             "Arial"                   "Arial Narrow"            "Arial Rounded MT Bold"   "Arial Unicode MS"       
[11] "Batang"                  "Bodoni Ornaments"        "Bodoni 72 Smallcaps"     "Bookshelf Symbol 7"      ""                       
[16] "Brush Script MT"         "Calibri"                 "Calibri Light"           "Cambria"                 "Cambria Math"           
[21] "Candara"                 "Comic Sans MS"           "Consolas"                "Constantia"              "Corbel"                 
[26] "Courier New"             "DIN Alternate"           "DIN Condensed"           "Franklin Gothic Book"    "Franklin Gothic Medium" 
[31] "Gabriola"                "Georgia"                 "Gill Sans MT"            "Gulim"                   "Impact"                 
[36] "Khmer Sangam MN"         "Lao Sangam MN"           "Lucida Console"          "Lucida Sans Unicode"     "Luminari"               
[41] "Marlett"                 "Meiryo"                  "Microsoft Yi Baiti"      "Microsoft Himalaya"      "Microsoft Sans Serif"   
[46] "Microsoft Tai Le"        "MingLiU_HKSCS-ExtB"      "MingLiU_HKSCS"           "MingLiU"                 "MingLiU-ExtB"           
[51] "Mongolian Baiti"         "MS Gothic"               "MS Mincho"               "MS PGothic"              "MS PMincho"             
[56] "MS Reference Sans Serif" "MS Reference Specialty"  "Palatino Linotype"       "Perpetua"                "PMingLiU"               
[61] "PMingLiU-ExtB"           "SimHei"                  "SimSun"                  "SimSun-ExtB"             "Tahoma"                 
[66] "Times New Roman"         "Trattatello"             "Trebuchet MS"            "Tw Cen MT"               "Verdana"                
[71] "Webdings"                "Wingdings"               "Wingdings 2"             "Wingdings 3"    
Run Code Online (Sandbox Code Playgroud)

但是我的老板告诉我,根据我们公司的样式指南,我们公司用于图形的字体类型是National 2 Condensed和National2。快速的Google搜索就可以找到这些字体:https//klim.co。 nz /零售字体/ national-2-condensed /

有没有办法下载这些自定义字体以供R在ggplot中使用,即使它们没有包含在Extrafonts中?如果不是,是否有人知道这些字体中的任何一种与《 National 2 Condensed》和《 National 2》特别相似吗?

谢谢!

编辑!!(接受一个较早的答案,但现在有一个问题):我现在在计算机上安装了字体(测试它可以在Microsoft Word中运行)。在这里看到它已安装。

在此处输入图片说明

但是,当我运行以下命令时,出现以下错误:

library(extrafont)
extrafont::font_import(pattern="National2Condensed-Bold")

Continue? [y/n] y
Scanning ttf files in /Library/Fonts/, /System/Library/Fonts, ~/Library/Fonts/ ...
Extracting .afm files from .ttf files...
Error in data.frame(fontfile = ttfiles, FontName = "", stringsAsFactors = FALSE) : 
  arguments imply differing number of rows: 0, 1
Run Code Online (Sandbox Code Playgroud)

发生此错误是因为字体类型是.otf而不是.ttf?我该如何解决?

rmf*_*rmf 6

首先,您获得所需的字体并将其安装在系统上。与R无关。通过检入任何常规程序(如MS Word等)来测试字体是否有效。

然后打开R,加载extrafont程序包并导入您安装的字体。我认为目前仅适用于.ttf字体。

library(extrafont)
font_import(pattern="Roboto")
Run Code Online (Sandbox Code Playgroud)

如果可行,那么此步骤会将这些字体添加到extrafontdb中。您会看到类似这样的内容...

> font_import(pattern="Roboto",prompt=FALSE)
Scanning ttf files in C:\windows\Fonts ...
Extracting .afm files from .ttf files...
C:\Windows\Fonts\Roboto-Black.ttf => C:/R/R-3.5.1/library/extrafontdb/metrics/Roboto-Black
C:\Windows\Fonts\Roboto-BlackItalic.ttf => C:/R/R-3.5.1/library/extrafontdb/metrics/Roboto-BlackItalic
...
C:\Windows\Fonts\RobotoCondensed-Regular.ttf => C:/R/R-3.5.1/library/extrafontdb/metrics/RobotoCondensed-Regular
Found FontName for 30 fonts.
Scanning afm files in C:/R/R-3.5.1/library/extrafontdb/metrics
Writing font table in C:/R/R-3.5.1/library/extrafontdb/fontmap/fonttable.csv
Writing Fontmap to C:/R/R-3.5.1/library/extrafontdb/fontmap/Fontmap...
Run Code Online (Sandbox Code Playgroud)

这是一回事。导入后,此后即可在R中使用。您所要做的就是在下面运行。

library(extrafont)
# for windows
windowsFonts(sans="Roboto")
loadfonts(device="win")
loadfonts(device="postscript")
Run Code Online (Sandbox Code Playgroud)

现在默认值应该已更改。

plot(x=1:5,y=1:5)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

ggplotbase_family哪些需要更改以及family文本几何的参数。

library(ggplot2)
p <- ggplot(data.frame(x=1:5,y=1:5),aes(x,y))+
  geom_point()+
  geom_text(aes(label=y),nudge_x=0.5,family="Roboto")+
  theme_bw(base_family="Roboto")
p
Run Code Online (Sandbox Code Playgroud)

导出栅格图像也应该起作用。

ggsave("plot.png",p)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

PDF是一种痛苦。他们有一个额外的family论点。还有一些关于嵌入和填充的内容。请参阅下面的链接。

ggsave("plot.pdf",p,family="Roboto")
Run Code Online (Sandbox Code Playgroud)

您需要的所有信息都在这里


yix*_*uan 6

使用showtext包的解决方案:

library(showtext)
## Add the font with the corresponding font faces
font_add("national2",
    regular = "National2CondensedTest-Regular.otf",
    bold = "National2CondensedTest-Bold.otf")
## Automatically use showtext to render plots
showtext_auto()

library(ggplot2)
p = ggplot(NULL, aes(x = 1, y = 1)) + ylim(0.8, 1.2) +
    annotate("text", 1, 1.1, label = "National 2 Condensed Bold",
             family = "national2", fontface = "bold", size = 15) +
    annotate("text", 1, 0.9, label = "National 2 Condensed Regular",
             family = "national2", size = 12) +
    theme(axis.title = element_blank(),
          axis.ticks = element_blank(),
          axis.text  = element_blank())

ggsave("test.pdf", p, width = 8, height = 4)
Run Code Online (Sandbox Code Playgroud)

下面是生成的图:

使用 showtext 输出绘图

我使用了字体文件的测试版本,在您的情况下,只需将regularbold参数更改为文件的实际路径。