【问题标题】:Import font for female and male symbols in R using extrafont or showtext for ggplot2使用 ggplot2 的 extrafont 或 showtext 在 R 中导入女性和男性符号的字体
【发布时间】:2019-08-24 07:34:59
【问题描述】:

我正在尝试在ggplot 图中使用女性 ♀ 和男性 ♂ 符号。当我加载extrafont 包并运行所需代码时,它不起作用(类似于此post)。

我使用的是 Mac OS X,版本 10.11.6,使用 R for Mac OS X,版本 3.5.2。

install.packages("extrafont")
library(extrafont)
extrafont::loadfonts(device="pdf")
extrafont::font_import(pattern="CALIBRI") #pattern that has the ♀ and ♂ symbols
#when I run this as font_import() alone fonts() is still empty

错误信息:

扫描 /Library/Fonts/、/System/Library/Fonts、~/Library/Fonts/ 中的 ttf 文件... 从 .ttf 文件中提取 .afm 文件... data.frame 中的错误(fontfile = ttfiles,FontName = "",stringsAsFactors = FALSE): 参数暗示不同的行数:0, 1

并仔细检查:

> fonts() #empty
 NULL
> fonttable() #empty
 data frame with 0 columns and 0 rows

有谁知道为什么会发生这种情况以及如何让它正常运行?

更新:

或者,我可以让 Calibri 使用不同的包加载(请参阅 OP here)。但是,我仍然无法在我的ggplot 上显示 ♀ 和 ♂ 符号。 建议?

install.packages('showtext', dependencies = TRUE)
library(showtext)
font_add_google("Montserrat", "Montserrat")
font_add_google("Roboto", "Roboto")
font_paths() 
font_files()

# syntax: font_add(family = "<family_name>", regular = "/path/to/font/file")
font_add("Calibri", "calibri.ttf")
font_families()
showtext_auto() 

【问题讨论】:

  • @Tung 我试了一下,但在我弄清楚字体在我的硬盘上的存储位置之后,他们的解决方案仍然不起作用。

标签: r ggplot2 extrafont showtext


【解决方案1】:

showtext 应该可以胜任。

library(ggplot2)
library(showtext)
showtext_auto()

female = intToUtf8(9792)
male = intToUtf8(9794)

p = ggplot() +
    annotate("text", x = 1, y = 1, label = female, size = 20) +
    annotate("text", x = 2, y = 1, label = male, size = 20) +
    theme_bw(base_family = "sans")

## On screen
x11()
print(p)

## Save to PDF
ggsave("symbol.pdf", p, width = 9, height = 6)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-23
    • 2016-09-02
    • 1970-01-01
    • 2016-11-16
    • 2017-10-08
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多