首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将不在extrafonts库中的字体添加到R

将不在extrafonts库中的字体添加到R
EN

Stack Overflow用户
提问于 2018-09-10 12:53:25
回答 2查看 11.9K关注 0票数 12

在安装了R的extrafonts库,并检查了它必须提供的字体之后,它列出了这个列表:

代码语言:javascript
运行
复制
[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"    

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

有没有办法下载这些自定义字体供R在ggplot中使用,即使它们不包括在extrafonts中?如果不是,有没有人知道这些字体中是否有任何字体与National2 Condensed和National2特别相似?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-10 14:32:17

首先,你得到你想要的字体并安装在你的系统上。与R无关。通过检查任何常规程序,如MS Word或其他程序,测试字体是否正常工作。

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

代码语言:javascript
运行
复制
library(extrafont)
font_import(pattern="Roboto")

如果此操作有效,则此步骤会将这些字体添加到extrafontdb中。你会看到类似这样的东西。

代码语言:javascript
运行
复制
> 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中使用。你所要做的就是在下面运行。

代码语言:javascript
运行
复制
library(extrafont)
# for windows
windowsFonts(sans="Roboto")
loadfonts(device="win")
loadfonts(device="postscript")

现在,默认值应该已经更改了。

代码语言:javascript
运行
复制
plot(x=1:5,y=1:5)

ggplot有需要更改的base_family和文本geom的family参数。

代码语言:javascript
运行
复制
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

导出光栅图像也应该可以。

代码语言:javascript
运行
复制
ggsave("plot.png",p)

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

代码语言:javascript
运行
复制
ggsave("plot.pdf",p,family="Roboto")

你需要的所有信息都是here

票数 13
EN

Stack Overflow用户

发布于 2018-10-13 02:41:35

使用showtext包的解决方案:

代码语言:javascript
运行
复制
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)

下面是生成的图:

我使用的是字体文件的测试版本,在本例中,只需将regularbold参数更改为文件的实际路径即可。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52251290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档