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

ggplot中表情符号无法正常渲染的技术求助

解决方案:在ggplot中正确渲染Emoji(macOS环境)

问题根源

  1. 指定的Trebuchet MS字体不包含Emoji字符,导致无法渲染;系统找不到该字体时会自动 fallback 到默认字体,触发警告。
  2. emojifont包需要配合showtext启用图形设备的字体渲染支持,否则无法加载Emoji字体。

方案1:使用emo包 + 支持Emoji的系统字体

直接替换为macOS原生支持Emoji的字体(如SF Pro Text,系统默认自带),代码如下:

library(tidyverse)
library(emo)

ggplot() +
  xlab(paste0('Mileage', emo::ji("fuel_pump"))) +
  ylab(paste0('Vehicle', emo::ji("car"))) +
  theme(text = element_text(family = "SF Pro Text"))

若想保留Trebuchet MS的字体风格,同时显示Emoji,可通过systemfonts设置字体 fallback:

library(tidyverse)
library(emo)
library(systemfonts)

# 注册自定义字体族:优先用Trebuchet MS,无Emoji字符时自动调用SF Pro Text
register_font(
  "Trebuchet+Emoji",
  regular = "Trebuchet MS",
  fallback = "SF Pro Text"
)

ggplot() +
  xlab(paste0('Mileage', emo::ji("fuel_pump"))) +
  ylab(paste0('Vehicle', emo::ji("car"))) +
  theme(text = element_text(family = "Trebuchet+Emoji"))

方案2:使用emojifont + showtext

需先启用showtext的自动渲染,注册系统Emoji字体,代码如下:

library(tidyverse)
library(emojifont)
library(showtext)

# 启用showtext自动渲染,让ggplot识别特殊字体
showtext_auto()
# 加载macOS自带的Apple Color Emoji字体
load.emojifont("Apple Color Emoji")

ggplot() +
  xlab(paste0('Mileage', emoji("fuel_pump"))) +
  ylab(paste0('Vehicle', emoji("car"))) +
  # 指定字体族:优先用Trebuchet MS,Emoji部分调用Apple Color Emoji
  theme(text = element_text(family = c("Trebuchet MS", "Apple Color Emoji")))

解决字体警告问题

若系统提示找不到Trebuchet MS,先检查字体是否存在:

# 搜索系统中是否有Trebuchet系列字体
systemfonts::system_fonts() |> filter(str_detect(family, "Trebuchet"))

如果无结果,建议安装该字体,或替换为系统已有的字体(如Arial)。


环境依赖检查

确保以下包已安装:

install.packages(c("systemfonts", "showtext"))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 00:44:55