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

如何在ggplot中为欧元符号单独设置字体以解决显示空白问题

解决ggplot自定义字体缺失欧元符号的问题

最优简便方法:利用showtext的字体回退机制

直接在加载Gulliver字体时,指定一个包含欧元符号的系统字体作为回退字体,这样Gulliver没有的字符会自动用回退字体显示,无需修改任何文本内容:

library(tidyverse)
library(showtext)

# 加载字体时添加fallback参数,指定有欧元符号的字体(比如Arial、Segoe UI等)
font_add("Gulliver", 
         regular = ".../Windows/Fonts/Gulliver Regular.otf",
         fallback = "Arial")
showtext_auto()

mtcars %>% 
  ggplot() +
  geom_point(aes(x = disp, y = mpg)) +
  theme(text = element_text(family = "Gulliver")) +
  labs(y = bquote("mpg - (€ & " ~m^2 ~")"),
       x = "€€€")

灵活定制方法:用ggtext针对欧元符号单独设字体

如果需要更精细控制文本样式,可以用ggtext包解析markdown格式,给欧元符号单独指定字体:

  1. 先安装并加载ggtext包
  2. 在labs的文本中用<span>标签给欧元符号指定字体
  3. 在theme中把轴标题设为element_markdown(),默认用Gulliver字体,标签内的样式会覆盖欧元部分
library(tidyverse)
library(showtext)
library(ggtext)

font_add("Gulliver", ".../Windows/Fonts/Gulliver Regular.otf")
showtext_auto()

mtcars %>% 
  ggplot() +
  geom_point(aes(x = disp, y = mpg)) +
  theme(
    text = element_text(family = "Gulliver"),
    axis.title.x = element_markdown(),
    axis.title.y = element_markdown()
  ) +
  labs(
    y = "mpg - (<span style='font-family: Arial'>€</span> & m²)",
    x = "<span style='font-family: Arial'>€€€</span>"
  )

注意:回退字体要选你系统里确实存在的、包含欧元符号的字体,比如Windows里的Segoe UI、Arial,Mac里的Helvetica等。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 18:52:30