将字体添加到Extrafonts库中没有的R中 [英] Add font to R that is not in extrafonts library

查看:516
本文介绍了将字体添加到Extrafonts库中没有的R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在安装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"    

但是我需要使用National 2 Condensed和National( https: //klim.co.nz/retail-fonts/national-2-condensed/).

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

解决方案

首先,获得所需的字体并将其安装在系统上.与R无关.通过检入任何常规程序(如MS Word或类似程序)来测试字体是否正常工作.

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

library(extrafont)
font_import(pattern="Roboto")

如果这可行,那么此步骤会将这些字体添加到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...

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

library(extrafont)
# for windows
windowsFonts(sans="Roboto")
loadfonts(device="win")
loadfonts(device="postscript")

现在默认值应该已经更改.

plot(x=1:5,y=1:5)

ggplot具有需要更改的base_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

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

ggsave("plot.png",p)

PDF是一种痛苦.他们有一个额外的family参数.还有一些关于嵌入和填充的东西.请参阅下面的链接.

ggsave("plot.pdf",p,family="Roboto")

您需要的所有信息都在此处.

After installing R's extrafonts library, and checking what fonts it had to offer, it came up with this list:

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

However I need to use National 2 Condensed, and National (https://klim.co.nz/retail-fonts/national-2-condensed/).

Is there a way to download these custom fonts for R to use in ggplot, even though they aren't included in extrafonts? And if not, does anybody know if any of these fonts are particularly similar to National 2 Condensed and National 2?

解决方案

First you get the font you want and install it on your system. Nothing to do with R. Test if the font works by checking in any regular program like MS Word or something.

Then open R, load extrafont package and import the font that you installed. I think it only works with .ttf fonts for now.

library(extrafont)
font_import(pattern="Roboto")

If this works, then this step will add those fonts to the extrafontdb. You will see something like this...

> 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...

This is a one time thing. Once imported, it is available within R from then on. All you have to do is run below.

library(extrafont)
# for windows
windowsFonts(sans="Roboto")
loadfonts(device="win")
loadfonts(device="postscript")

Now the defaults should have changed.

plot(x=1:5,y=1:5)

ggplot has base_family which needs to be changed and family argument for text geoms.

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

Exporting raster images should work too.

ggsave("plot.png",p)

PDFs are a pain. They have an extra family argument. There is also something about embedding and stuff. See link below.

ggsave("plot.pdf",p,family="Roboto")

All the info you need is here.

这篇关于将字体添加到Extrafonts库中没有的R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆