简体   繁体   English

如何在ggplot中使用FontAwesome Free Font

[英]How to use FontAwesome Free Font in ggplot

I try to plot a certain glyph using extrafont (on Windows 10):我尝试使用extrafont plot 某个字形(在 Windows 10 上):

library(extrafont)
library(ggplot2)
loadfonts()

ft <- fonttable()

ft[grepl("Awesome", ft$FontName), c("FullName", "FamilyName", "FontName")]

#                          FullName                    FamilyName                   FontName
# 367 Font Awesome 5 Brands Regular Font Awesome 5 Brands Regular FontAwesome5Brands-Regular
# 368     Font Awesome 5 Free Solid     Font Awesome 5 Free Solid     FontAwesome5Free-Solid

ggplot() +
 geom_label(aes(x = 1, y = 1,  label = "\uf0f3"), 
 family = "Font Awesome 5 Free Solid", size = 16) +
 theme_minimal()

This works as expected:这按预期工作:

带有 FontAwesome Bell 符号的 ggplot

However, if I try to use another Unicode I just get an empty box:但是,如果我尝试使用另一个 Unicode,我只会得到一个空框:

ggplot() +
 geom_label(aes(x = 1, y = 1,  label = "\uf1fd"), 
 family = "Font Awesome 5 Free Solid", size = 16) +
 theme_minimal()

带有空框而不是 FontAwesome 符号的 ggplot

For all it is worth, I am using this FontAwesome CheatSheet to get the Unicode hex values.尽管值得,我正在使用这个FontAwesome CheatSheet来获取 Unicode 十六进制值。

Why is it not working?为什么它不起作用?

Update更新

@Pedro was suggetsing in the comments that it may have something to do with the Fontawesome version, so I looked up the v4 Unicodes, here's what I got: @Pedro 在评论中暗示它可能与 Fontawesome 版本有关,所以我查找了v4 Unicodes,这是我得到的:

library(rvest)
library(stringr)
library(purrr)
library(tibble)
library(dplyr)
library(ggplot2)
library(extrafont)

doc <- "https://fontawesome.com/v4/cheatsheet/" %>% 
  read_html()

unicodes <- doc %>% 
  html_elements(".row .fa+span") %>% 
  html_text2() 

fa <- unicodes %>% 
  str_subset(fixed("(alias)"), TRUE) %>% 
  str_extract("[0-9a-f]+") %>% 
  paste0("0x", .) %>% 
  map_chr(intToUtf8) %>% 
  tibble(label = .) %>% 
  mutate(x = (seq_along(label) - 1) %/% ceiling(sqrt(length(label)))) %>% 
  group_by(x) %>% 
  mutate(y = seq_along(label) - 1)

ggplot(fa) +
  geom_text(aes(x, y,  label = label), 
             family = "Font Awesome 5 Free Solid", size = 3) +
  geom_text(aes(x, y,  label = label), 
            family = "Font Awesome 5 Brands Regular", size = 3, color = "red") +
  
  theme_void()

在备忘单上找到的所有 fontawesome glpyhs 的情节。我们看到有几个字形没有渲染

Session Info Session 资料

R version 4.2.2 (2022-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363) Matrix products: default locale: [1] LC_COLLATE=German_Germany.utf8 LC_CTYPE=German_Germany.utf8 [3] LC_M.NETARY=German_Germany.utf8 LC_NUMERIC=C [5] LC_TIME=German_Germany.utf8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] ggplot2_3.4.0 extrafont_0.18 loaded via a namespace (and not attached): [1] Rcpp_1.0.9 bslib_0.4.2 compiler_4.2.2 pillar_1.8.1 [5] later_1.3.0 jquerylib_0.1.4 tools_4.2.2 digest_0.6.31 [9] jsonlite_1.8.4 lifecycle_1.0.3 tibble_3.1.8 gtable_0.3.1 [13] pkgconfig_2.0.3 rlang_1.0.6 DBI_1.1.3 shiny_1.7.3 [17] cli_3.5.0 fastmap_1.1.0 Rttf2pt1_1.3.8 withr_2.5.0 [21] dplyr_1.0.10 generics_0.1.3 sass_0.4.4 vctrs_0.5.1 [25] tidyselect_1.2.0 grid_4.2.2 glue_1.6.2 R6_2.5.1 [29] fansi_1.0.3 extrafontdb_1.0 magrittr_2.0.3 scales_1.2.1 [33] promises_1.2.0.1 ellipsis_0.3.2 htmltools_0.5.4 assertthat_0.2.1 [37] mime_0.12 xtable_1.8-4 colorspace_2.0-3 httpuv_1.6.6 [41] utf8_1.2.2 munsell_0.5.0 cachem_1.0.6

After uninstalling all Font Awesome Fonts from my machine and re-installing them from the source , all glyphs could be rendered properly.从我的机器上卸载所有 Font Awesome Fonts 并从源代码重新安装它们后,所有字形都可以正确呈现。

I just needed to install the webfonts (not the desktop version), because only the webfont comes in ttf format, while the desktop only ships the otf which cannot be read by extrafont::font_import .我只需要安装 webfonts(不是桌面版),因为只有 webfont 是ttf格式,而桌面版只提供 extrafont:: otf无法读取的extrafont::font_import

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM