ggplot2 和 fontawesome 只显示一个空的矩形

问题描述 投票:0回答:1

我尝试在 ggplot2 图中使用一些很棒的字体图标。特别是“fa-temperature-arrow-up”对我来说很有趣,但我无法让它发挥作用。我尝试了 emojifont 包,但没有成功。有人能指出我所缺少的吗?

library(tidyverse)
library(ggtext)

df <- data.frame(
  x = 2,
  y = 1.5,
  h = 4.25,
  w = 6.25,
  value = c("97%"),
  info = c("consensus about climate change"),
  icon = c("e040") #temperature-arrow-up
) %>% 
  mutate(icon = glue::glue("<span style='font-family:fa-solid;'>&#x{icon};</span>"))

df %>% ggplot(aes(x, y)) +
  geom_richtext(aes(label = icon), 
                size = 12, 
                label.colour = NA, 
                fill = NA, 
                col = 'dodgerblue4') +
  theme_minimal() +
  labs(title ="I only see an empty rectangle")

sessionInfo()

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)
Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8    LC_MONETARY=German_Germany.utf8
[4] LC_NUMERIC=C                    LC_TIME=German_Germany.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggtext_0.1.2    lubridate_1.9.2 forcats_1.0.0   stringr_1.5.0   dplyr_1.1.0     purrr_1.0.1     readr_2.1.4    
 [8] tidyr_1.3.0     tibble_3.2.0    ggplot2_3.4.1   tidyverse_2.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.10       pillar_1.8.1      compiler_4.2.2    tools_4.2.2       sysfonts_0.8.8    digest_0.6.31    
 [7] timechange_0.2.0  lifecycle_1.0.3   gtable_0.3.2      pkgconfig_2.0.3   rlang_1.1.0       cli_3.6.0        
[13] rstudioapi_0.14   commonmark_1.8.1  xfun_0.37         proto_1.0.0       fastmap_1.1.1     xml2_1.3.3       
[19] withr_2.5.0       showtextdb_3.0    generics_0.1.3    vctrs_0.6.0       hms_1.1.2         gridtext_0.1.5   
[25] emojifont_0.5.5   grid_4.2.2        tidyselect_1.2.0  glue_1.6.2        fontawesome_0.5.0 R6_2.5.1         
[31] fansi_1.0.4       farver_2.1.1      tzdb_0.3.0        magrittr_2.0.3    scales_1.2.1      htmltools_0.5.4  
[37] ellipsis_0.3.2    showtext_0.9-5    colorspace_2.1-0  labeling_0.4.2    utf8_1.2.3        stringi_1.7.12   
[43] munsell_0.5.0     markdown_1.5    
r ggplot2 font-awesome ggtext
1个回答
0
投票

我的解决方案是

  1. 使用 fontawesome 的桌面字体
  2. 添加 showtext_auto()

代码

library(tidyverse)
library(ggtext)
library(fontawesome)
library(showtext)

font_add(family = 'fa-solid', regular = '{PATH_TO}/fontawesome-free-6.3.0-desktop/otfs/Font Awesome 6 Free-Solid-900.otf')


df <- data.frame(
  x = 2,
  y = 1.5,
  h = 4.25,
  w = 6.25,
  value = c("97%"),
  info = c("consensus about climate change"),
  icon = c("e040") #temperature-arrow-up
) %>%
  mutate(icon = glue::glue("<span style='font-family:fa-solid;'>&#x{icon};</span>"))

showtext_auto()

df %>% ggplot(aes(x, y)) +
  geom_richtext(aes(label = icon),
                size = 12,
                label.colour = NA,
                fill = NA,
                col = 'dodgerblue4') +
  theme_minimal() +
  labs(title ="now it works")
© www.soinside.com 2019 - 2024. All rights reserved.