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

Mac中ggplot无法为Google字体Raleway设置加粗/斜体的技术求助

问题描述

在Mac的Font Book中下载并保存了Google字体Raleway,但无法在ggplot中为其设置加粗、斜体样式。示例代码如下:

ggplot() +
  labs(title = "Title in Bold",
       subtitle = "Subtitle in Italics") +
  xlab("Y Label") +
  ylab("X Label") +
  theme(text = element_text(family = "Raleway"),
        plot.title = element_text(face = 'bold', size = 18),
        plot.subtitle = element_text(face = 'italic')
        )

运行后字体显示为Raleway常规样式,但标题的加粗、副标题的斜体均未生效。

尝试使用extrafont包的font_import()时,返回提示:

"/Users/username/Library/Fonts/Raleway-Italic-VariableFont_wght.ttf : Raleway-ThinItalic already registered in fonts database. Skipping.
/Users/username/Library/Fonts/Raleway-VariableFont_wght.ttf : Raleway-Thin already registered in fonts database. Skipping."

切换至AGG图形后端或使用showtext::showtext_auto()时,均出现警告:In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), ... : Unable to load font: Raleway。

该字体的加粗、斜体样式可在Microsoft Word等其他应用中正常使用,需解决在R中正确加载并注册这些字体样式的问题。

会话信息:

R version 4.3.3 (2024-02-29)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.4.1
Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
locale:
1 en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: America/Los_Angeles
tzcode source: internal
attached base packages:
1 stats     graphics  grDevices utils     datasets  methods   base
other attached packages:
1 extrafont_0.19  lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4     purrr_1.0.2     readr_2.1.5
[8] tidyr_1.3.1     tibble_3.2.1    ggplot2_3.5.0   tidyverse_2.0.0 RPostgres_1.4.6
loaded via a namespace (and not attached):
1 cfbfastR_1.9.0     DBI_1.2.2          remotes_2.5.0      writexl_1.5.0      rlang_1.1.3        magrittr_2.0.3
[7] snakecase_0.11.1   furrr_0.3.1        compiler_4.3.3     mgcv_1.9-1         png_0.1-8          systemfonts_1.0.6
[13] vctrs_0.6.5        reshape2_1.4.4     sysfonts_0.8.9     rvest_1.0.4        profvis_0.3.8      crayon_1.5.2
[19] pkgconfig_2.0.3    fastmap_1.1.1      backports_1.4.1    ellipsis_0.3.2     labeling_0.4.3     utf8_1.2.4
[25] promises_1.2.1     rmarkdown_2.26     sessioninfo_1.2.2  tzdb_0.4.0         ps_1.7.6           bit_4.0.5
[31] xfun_0.43          showtext_0.9-7     cachem_1.0.8       jsonlite_1.8.8     blob_1.2.4         later_1.3.2
[37] emoji_15.0         jpeg_0.1-10        broom_1.0.5        parallel_4.3.3     R6_2.5.1           stringi_1.8.3
[43] espnscrapeR_0.8.0  extrafontdb_1.0    parallelly_1.37.1  pkgload_1.3.4      assertthat_0.2.1   Rcpp_1.0.12
[49] knitr_1.45         usethis_2.2.3      pacman_0.5.1       nnet_7.3-19        Matrix_1.6-5       httpuv_1.6.15
[55] splines_4.3.3      timechange_0.3.0   tidyselect_1.2.1   rstudioapi_0.16.0  yaml_2.3.8         codetools_0.2-19
[61] miniUI_0.1.1.1     websocket_1.4.1    curl_5.2.1         processx_3.8.4     listenv_0.9.1      pkgbuild_1.4.4
[67] lattice_0.22-6     plyr_1.8.9         withr_3.0.0        shiny_1.8.1        evaluate_0.23      future_1.33.1
[73] RcppParallel_5.1.7 urlchecker_1.0.1   xml2_1.3.6         pillar_1.9.0       generics_0.1.3     chromote_0.2.0
[79] hms_1.1.3          munsell_0.5.0      scales_1.3.0       globals_0.16.3     xtable_1.8-4       emo_0.0.0.9000
[85] glue_1.7.0         janitor_2.2.0      mlbplotR_1.1.0     tools_4.3.3        data.table_1.15.2  fs_1.6.3
[91] grid_4.3.3         Rttf2pt1_1.3.12    devtools_2.4.5     colorspace_2.1-0   nlme_3.1-164       patchwork_1.2.0
[97] showtextdb_3.0     proto_1.0.0        baseballr_1.6.0    cli_3.6.2          fansi_1.0.6        gt_0.10.1
[103] V8_4.4.2           emojifont_0.5.5    gtable_0.3.4       digest_0.6.35      farver_2.1.1       htmlwidgets_1.6.4
[109] memoise_2.0.1      htmltools_0.5.8    lifecycle_1.0.4    httr_1.4.7         mime_0.12          bit64_4.0.5
解决方案

核心原因

R的字体系统对**可变字体(Variable Font)**的支持有限,你安装的Raleway是可变字体文件(Raleway-VariableFont_wght.ttf),仅靠指定face = 'bold'无法触发字重变化,需要明确注册或调用对应字重的字体变体。

方法1:直接指定字体变体名称

  • 先查看系统中已安装的Raleway所有变体:
    systemfonts::system_fonts() |> dplyr::filter(grepl("Raleway", family))
    
  • 根据输出的family或fullname,直接在element_text中指定对应变体,比如:
    ggplot() +
      labs(title = "Title in Bold",
           subtitle = "Subtitle in Italics") +
      xlab("Y Label") +
      ylab("X Label") +
      theme(
        text = element_text(family = "Raleway"),
        plot.title = element_text(family = "Raleway Bold", size = 18),
        plot.subtitle = element_text(family = "Raleway Italic")
      )
    

方法2:重新安装单文件变体字体

如果可变字体兼容性问题无法解决,直接从Google Fonts下载Raleway的单个字重/样式文件(比如Raleway-Bold.ttf、Raleway-Italic.ttf等),安装到Font Book后:

  • 清理extrafont缓存:
    extrafont::font_remove("Raleway")
    unlink("~/.fonts", recursive = TRUE)
    
  • 重新导入字体:
    extrafont::font_import(pattern = "Raleway", prompt = FALSE)
    extrafont::loadfonts(device = "pdf") # 根据需要加载对应设备(如"win"或"postscript")
    
  • 重启R后再运行ggplot代码,此时face = 'bold'/'italic'即可生效。

方法3:使用showtext正确加载可变字体

针对可变字体,showtext需要明确指定字重参数:

  • 注册字体:
    showtext::font_add("Raleway", 
                       regular = "/Users/username/Library/Fonts/Raleway-VariableFont_wght.ttf",
                       bold = "/Users/username/Library/Fonts/Raleway-VariableFont_wght.ttf",
                       italic = "/Users/username/Library/Fonts/Raleway-Italic-VariableFont_wght.ttf")
    
  • 启用showtext:
    showtext::showtext_auto()
    
  • 运行ggplot代码时,可通过weight参数指定字重(比如weight = 700对应加粗):
    ggplot() +
      labs(title = "Title in Bold",
           subtitle = "Subtitle in Italics") +
      xlab("Y Label") +
      ylab("X Label") +
      theme(
        text = element_text(family = "Raleway"),
        plot.title = element_text(weight = 700, size = 18),
        plot.subtitle = element_text(face = "italic")
      )
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 18:18:09