使用sysfonts 包直接安装外部获取一直还是主要使用 showtext 包。发现它除了font_add_google 下字体以外,还可以从外部导入字体,这真是太好了。

毕竟如果不挂梯子,有时候字体根本获取不到,非常头疼。

使用sysfonts 包主要依赖包sysfonts,我们可以先看看自己电脑里的字体:

> head(font_files()) path file family face version ps_name1 /Library/Fonts Al Nile.ttc Al Nile Regular 13.0d2e2 AlNile2 /Library/Fonts Al Tarikh.ttc Al Tarikh Regular 13.0d2e1 AlTarikh3 /Library/Fonts AlBayan.ttc Al Bayan Plain 13.0d1e6 AlBayan4 /Library/Fonts AmericanTypewriter.ttc American Typewriter Regular 13.0d1e4 AmericanTypewriter5 /Library/Fonts Andale Mono.ttf Andale Mono Regular Version 2.00x AndaleMono6 /Library/Fonts Apple Chancery.ttf Apple Chancery Chancery 13.0d1e4 Apple-Chancery

如果需要添加字体文件,可以使用函数font_add(family, regular),其中family 为字体在R 中的名称,regular 为对应的字体文件。

showtext_auto()font_add("gochi", regular = "../../../../01-dir_for_work/3-source/1.font/GochiHand-Regular.ttf")

但这样也有些麻烦,总不至于每次画自己的主题,都要拖着一个文件吧?

据说extrafont 包中的函数font_import 可以很好的扫描tff 等字体文件,并将它们永久保存在R 中,但发现似乎并不适合mac 用户:

直接安装在https://fonts.google.com/specimen/Gochi+Hand?query=gochi#about 下载好ttf 文件后,直接安装即可。

另外我发现,showtext 和默认的字体库存在一定的冲突,如果是使用了showtext 导入的字体后,希望再用回操作系统中的字体,需要手动关闭showtext 的模式:

showtext_auto(enable = F)

或者手动在指定段落间使用showtext 的字体:

showtext_begin()x1 <- ggplot(mtcars, aes(mpg, disp, colour = factor(am))) + geom_point() x1 + labs(x = "mpg", title = "Template fot my ggplot", tag = "Fig. 1", caption = "By: Mug Peng") + scale_color_paletteer_d("ggsci::default_nejm") + theme(text = element_text(family = "gochi"))showtext_end()

外部获取我目前希望的是,如果可以把字体文件放在github 上,如果可以后期获取就好了。

目前的解决方案是,在工作目录下直接下载该字体,再导入到showtext 中,反正文件也不大:

download.file("https://gh-proxy.du33169.workers.dev/https://github.com/mugpeng/3-source/blob/main/1.font/GochiHand-Regular.ttf", destfile = "./data/font.ttf")p_load(showtext)font_add("gochi", regular = "./data/font.ttf")

贵州九大城市排行榜 贵州省各城市排名 贵州综合实力最强的城市
Intel Core i5-6500 @ 3.20GHz