You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ggplot2中Font Awesome图标仅显示空矩形问题求助

问题描述

在ggplot2绘图中尝试使用Font Awesome的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;'>&amp;#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    
解决方案

空矩形问题的核心是系统未正确加载Font Awesome Solid字体,或HTML标签的编码/字体引用格式错误。以下是两种可行修复方案:

方案1:使用fontawesome包直接调用图标

fontawesome包内置Font Awesome字体,无需手动处理编码和字体加载,是最简便的方式:

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

df <- data.frame(
  x = 2,
  y = 1.5,
  h = 4.25,
  w = 6.25,
  value = c("97%"),
  info = c("consensus about climate change")
)

df %>% ggplot(aes(x, y)) +
  geom_richtext(
    aes(label = fa("temperature-arrow-up", fill = "dodgerblue4", size = 48)), 
    label.colour = NA, 
    fill = NA
  ) +
  theme_minimal() +
  labs(title = "图标已正常显示")

方案2:修正ggtext的HTML标签与字体加载

若坚持手动使用HTML标签,需确保:

  1. 系统已安装Font Awesome Solid字体(可从官方渠道下载安装)
  2. 修正HTML实体编码写法(直接用&#xe040;,无需转义&)
  3. 通过sysfonts和showtext正确加载字体

修改后的代码:

library(tidyverse)
library(ggtext)
library(sysfonts)
library(showtext)

# 加载系统中的Font Awesome Solid字体(需匹配实际字体文件名)
font_add(family = "fa-solid", regular = "Font Awesome 6 Free-Solid-900.otf")
showtext_auto()

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("&#xe040;") # 正确的HTML实体编码
) %>% 
  mutate(icon = glue::glue("<span style='font-family:fa-solid; color:dodgerblue4; font-size:48px;'>{icon}</span>"))

df %>% ggplot(aes(x, y)) +
  geom_richtext(aes(label = icon), 
                label.colour = NA, 
                fill = NA) +
  theme_minimal() +
  labs(title = "图标已正常显示")

关键注意点

  • fontawesome包的fa()函数会自动处理字体和编码,推荐优先使用
  • 手动加载字体时,需确认字体文件路径和名称与系统一致(Windows下字体名称可能显示为Font Awesome 6 Free Solid,需对应调整font_add参数)
  • ggtext的geom_richtext支持HTML/CSS样式,可直接在span标签中设置颜色、大小等属性

内容的提问来源于stack exchange,提问作者SEMson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 17:43:11