如何在Quarto文件中为ggplot2标题添加Font Awesome图标?
解决ggplot2标题/说明添加Font Awesome图标失败的问题
问题根源
你遇到的核心问题是systemfonts::font_info无法正确识别系统中安装的Font Awesome Brands字体,错误返回了Arial的路径,导致后续font_add加载的不是目标字体。这通常是因为字体名称在系统中的注册名和你使用的查询名不匹配,或者systemfonts的字体缓存未更新。
可行解决方案
1. 手动指定字体文件路径(最稳定)
跳过自动识别步骤,直接找到Font Awesome Brands字体的本地文件路径,传入font_add:
library(showtext) # Windows系统下的典型路径(根据你安装的字体文件调整) # TTF版本路径 fa_brands_path <- "C:/Windows/Fonts/fa-brands-400.ttf" # 或者OTF版本路径 # fa_brands_path <- "C:/Windows/Fonts/Font Awesome 6 Brands-Regular-400.otf" font_add(family = "fa-brands", regular = fa_brands_path) showtext_auto()
提示:如果找不到路径,可在Windows字体文件夹中搜索"Font Awesome Brands",右键字体文件选「属性」查看完整路径。
2. 用模糊匹配加载字体
利用sysfonts::font_add的pattern参数,通过字体文件名的关键词模糊匹配,避免因名称不一致导致的识别失败:
library(sysfonts) font_add( family = "fa-brands", pattern = "Font Awesome 6 Brands" # 或直接用"fa-brands"匹配TTF文件名 ) showtext_auto()
3. 刷新字体缓存
如果是字体安装后systemfonts未更新缓存导致的识别问题,执行以下命令刷新:
systemfonts::clear_font_cache() systemfonts::register_fonts() # 重新尝试获取路径 fa_path <- systemfonts::font_info(family = "Font Awesome 6 Brands Regular")[[1, "path"]]
额外验证步骤
- 确认Font Awesome字体已正确安装:打开Windows「字体」设置,搜索"Font Awesome"查看是否存在目标字体。
- 检查caption的HTML标签:你的代码中
&#xf09b;是GitHub图标的正确转义,只要字体加载正常,就能渲染出图标。 - 确保
plot.caption已设置为element_markdown(你的代码已正确配置)。
内容的提问来源于stack exchange,提问作者seansteele
相关产品推荐
相关产品推荐

