gpt4 book ai didi

r - 使用 ggplot2 的 extrafont 或 showtext 在 R 中导入女性和男性符号的字体

转载 作者:行者123 更新时间:2023-12-04 17:38:50 25 4
gpt4 key购买 nike

我正在尝试在 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

错误信息:

Scanning ttf files in /Library/Fonts/, /System/Library/Fonts, ~/Library/Fonts/ ... Extracting .afm files from .ttf files... Error in data.frame(fontfile = ttfiles, FontName = "", stringsAsFactors = FALSE) : arguments imply differing number of rows: 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()

最佳答案

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)

enter image description here

关于r - 使用 ggplot2 的 extrafont 或 showtext 在 R 中导入女性和男性符号的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55483861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com