r - 将不在 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 中使用,即使它们不包含在额外字体中?如果没有,有人知道这些字体是否与 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")
您需要的所有信息都在这里。
推荐阅读
- paypal-rest-sdk - 跟踪结束后 Paypal 不向客户收费
- java - 有没有办法进行循环,直到没有运行时错误
- amazon-web-services - 我想从参数部分传递多个存储桶名称,并允许它们一次性进入 iam 资源部分 - 云形成
- groovy - 使用 Terraform 地图变量的 Jenkins 参数化作业
- javascript - 在 Windows 任务栏中(纯 JavaScript)等元素上交换位置
- windows - 如何将记事本的默认unicode设置为UTF8?
- javascript - 在 Amcharts4 range.contents.fill 不适用于 Lineseries.propertiesField.fill
- node.js - 如何防止我的图书馆的消费者安装我的 devDependencies?
- javascript - React.js,在 useEffect 挂钩中,window.location.href 不会停止中断进一步的代码执行
- python - xclip 没有从 pexpect 获取标准输入,或者 pexpect 没有发送?