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

Quarto导出PDF时ggplot文本渲染异常(Calibri Light字体)

问题:Quarto导出PDF时ggplot图表文本渲染异常(Calibri Light字体)

现象

使用Calibri Light字体将Quarto文档导出为PDF时,ggplot图表文本渲染异常;导出为HTML时显示正常,换用Helvetica等字体则PDF显示正常。

文档配置

---
title: "Some title here"
author: "Marc Bosch"
execute:
  warning: false
  error: false
  message: false
  echo: false
format:
  html: 
    code-fold: true
    code-summary: "Show the code"
    fig-width: 8
    embed-resources: true
    css: styles.css
  pdf:
    mainfont: Calibri Light
---

自定义ggplot主题代码

通过带plot_font参数的自定义主题设置字体:

theme(
plot.title = element_text(size = title_size, face = "bold", family = plot_font), 
plot.subtitle = element_text(size = subtitle_size, family = plot_font),
axis.title.y = element_text(size = x_size), 
axis.title.x = element_text(size = y_size),
axis.text.x = element_text(size = axis_x_size, vjust = x_vjust),
axis.text.y = element_text(size = axis_y_size), 
plot.caption = element_text(size = 8.5, hjust = 0, 
                            colour = rgb(0, 0, 0, 0.65), 
                            family = plot_font), 
strip.text = element_text(size = 13),
legend.position = legend_position, 
legend.text = element_text(size = 13),
title = element_text(family = plot_font), 
text = element_text(family = plot_font),
panel.grid.minor = element_blank()
)

已尝试操作

  • 测试过多个PDF引擎
  • 执行以下代码注册字体:
library(extrafont)
loadfonts(quiet = TRUE)

绘图代码示例

ggplot(df, aes(x = variable_x) +
       geom_bar(fill = my_colour) +
       labs(title = "something") +
       function_corporate_theme(plot_font = "Calibri Light")

解决方案

  1. 核对字体注册名称:执行extrafont::fonts()查看已注册字体的准确名称,确保plot_font使用的名称与注册名称完全匹配。
  2. 替换为showtext加载字体:放弃extrafont,改用showtext加载系统字体,示例代码:
    library(showtext)
    font_add("Calibri Light", regular = "calibril.ttf") # 根据系统字体文件路径调整
    showtext_auto()
    
    确保Quarto PDF配置的mainfont与font_add的第一个参数一致,ggplot主题中也使用该名称。
  3. 强制嵌入字体:在Quarto的PDF格式配置中添加embed-fonts: true,确保字体被正确嵌入PDF:
    format:
      pdf:
        mainfont: Calibri Light
        embed-fonts: true
    
  4. 消除字体粗细冲突:自定义主题中plot.title设置了face = "bold",但Calibri Light无对应粗体变体,可移除该参数,或改用常规Calibri字体并设置粗体。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 19:45:48