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

如何在R Markdown PDF的ggplot2图中嵌入Ubuntu Condensed字体?

Ubuntu Condensed字体无法嵌入R Markdown PDF输出的ggplot2图表(R 4.4.x版本问题)
  • 已在Windows机器为所有用户安装Ubuntu和Ubuntu Condensed字体,systemfonts::system_fonts()和extrafont::fonttable()均可查询到这两种字体
  • 已将两种字体注册到Windows位图设备,使用AGG作为后端图形设备
  • RStudio查看器面板中图表能正常显示两种字体,但生成R Markdown PDF时,Ubuntu字体正常,Ubuntu Condensed完全无法嵌入
  • 使用showtext可解决该问题,但希望尽量避免使用它

当前配置代码

knitr::opts_chunk$set(
  dev = "cairo_pdf",
    echo = FALSE,
    message = FALSE,
    warning = FALSE
)

library(ggplot2)
library(ggtext)
library(extrafont)
library(systemfonts)

loadfonts()

grDevices::windowsFont("Ubuntu")
grDevices::windowsFont("Ubuntu Condensed")

绘图代码

ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point() +
  labs(title = "This is Ubuntu", x = "This should be Ubuntu Condensed", 
       y = "This should be Ubuntu Condensed") +
  theme(plot.title=element_textbox_simple(family="Ubuntu"),
        axis.title = element_text(family = "Ubuntu Condensed"),
        axis.text = element_text(family = "Ubuntu Condensed"))

会话信息

R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default


locale:
[1] LC_COLLATE=Czech_Czechia.utf8  LC_CTYPE=Czech_Czechia.utf8    LC_MONETARY=Czech_Czechia.utf8
[4] LC_NUMERIC=C                   LC_TIME=Czech_Czechia.utf8    

time zone: Europe/Prague
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] systemfonts_1.2.1 extrafont_0.19    ggtext_0.1.2      ggplot2_3.5.1    

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       cli_3.6.3         knitr_1.49        rlang_1.1.5       xfun_0.50        
 [6] stringi_1.8.4     Rttf2pt1_1.3.8    generics_0.1.3    textshaping_1.0.0 labeling_0.4.3   
[11] glue_1.8.0        colorspace_2.1-1  markdown_1.13     extrafontdb_1.0   ragg_1.3.3.9000  
[16] gridtext_0.1.5    scales_1.3.0      grid_4.4.2        munsell_0.5.1     evaluate_1.0.3   
[21] tibble_3.2.1      lifecycle_1.0.4   stringr_1.5.1     compiler_4.4.2    dplyr_1.1.4      
[26] Rcpp_1.0.14       pkgconfig_2.0.3   rstudioapi_0.17.1 farver_2.1.2      R6_2.5.1         
[31] tidyselect_1.2.1  pillar_1.10.1     commonmark_1.9.2  magrittr_2.0.3    tools_4.4.2      
[36] withr_3.0.2       gtable_0.3.6      xml2_1.3.6       

更新

在R 4.3.3版本中两种字体均能正确嵌入,推测问题源于R 4.3.3到R 4.4.0的版本变更。


内容的提问来源于stack exchange,提问作者Kateřina Hudáčová

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 13:45:11