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

MacOS Monterrey下ggplot2报错polygon edge not found及字体缺失求助

问题背景

在MacOS Monterey v12.6.3系统中,所有ggplot2代码突然无法运行。执行以下简单绘图代码:

library(ggplot2)

x = 1:10
y = 1:10
df = data.frame(x, y)

ggplot(df, aes(x = x, y = y)) + 
  geom_point()

触发报错:

Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
  polygon edge not found
In addition: There were 50 or more warnings (use warnings() to see the first 50)

同时出现核心警告:

> warnings()
Warning messages:
1: In grid.Call(C_stringMetric, as.graphicsAnnot(x$label)) :
  no font could be found for family "Arial"

重新安装ggplot2无法解决问题,补充环境信息如下:

> ggplot2::theme_get()$text
List of 11
 $ family       : chr ""
 $ face         : chr "plain"
 $ colour       : chr "black"
 $ size         : num 11
 $ hjust        : num 0.5
 $ vjust        : num 0.5
 $ angle        : num 0
 $ lineheight   : num 0.9
 $ margin       : 'margin' num [1:4] 0points 0points 0points 0points
  ..- attr(*, "unit")= int 8
 $ debug        : logi FALSE
 $ inherit.blank: logi TRUE
 - attr(*, "class")= chr [1:2] "element_text" "element"

> X11Fonts()
$serif
[1] "-*-times-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$sans
[1] "-*-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$mono
[1] "-*-courier-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$Times
[1] "-adobe-times-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$Helvetica
[1] "-adobe-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$CyrTimes
[1] "-cronyx-times-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$CyrHelvetica
[1] "-cronyx-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$Arial
[1] "-monotype-arial-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$Mincho
[1] "-*-mincho-%s-%s-*-*-%d-*-*-*-*-*-*-*"

> systemfonts::font_info()
# A tibble: 1 × 24
  path    index family style italic bold  monos…¹ weight width kerning color scala…² verti…³ n_gly…⁴ n_sizes n_cha…⁵ bbox 
  <chr>   <int> <chr>  <chr> <lgl>  <lgl> <lgl>   <ord>  <ord> <lgl>   <lgl> <lgl>   <lgl>     <int>   <int>   <int> <lis>
1 /Syste…     0 Helve… Regu… FALSE  FALSE FALSE   normal norm… FALSE   FALSE TRUE    FALSE      2252       0      10 <dbl>
# … with 7 more variables: max_ascend <dbl>, max_descend <dbl>, max_advance_width <dbl>, max_advance_height <dbl>,
#   lineheight <dbl>, underline_pos <dbl>, underline_size <dbl>, and abbreviated variable names ¹​monospace, ²​scalable,
#   ³​vertical, ⁴​n_glyphs, ⁵​n_charmaps
# ℹ Use `colnames()` to see all variable names
原因分析
  1. 字体查找失败:警告明确显示找不到Arial字体,结合systemfonts::font_info()输出,系统仅识别到Helvetica一种字体,说明R的字体检测机制无法正常读取系统字体库,或系统中的Arial字体已损坏/缺失。
  2. 底层绘图错误:ggplot2依赖grid包渲染图形,当文本字体无法加载时,会触发grid.Call的底层错误(polygon edge not found),导致整个绘图流程失败。
  3. 无效字体配置:X11Fonts()中的Arial配置指向-monotype-arial-,但系统中不存在对应字体文件,进一步加剧了字体加载失败的问题。
可行解决方案

临时应急方案(快速恢复绘图)

强制指定ggplot2使用系统已识别的Helvetica字体,避免尝试加载不存在的Arial:

  • 单图设置:
ggplot(df, aes(x = x, y = y)) + 
  geom_point() +
  theme(text = element_text(family = "Helvetica"))
  • 全局默认设置(重启R后生效):
ggplot2::theme_set(ggplot2::theme_grey(base_family = "Helvetica"))

修复字体检测机制

  1. 重置R字体缓存:
systemfonts::clear_cache()

执行后重启R,再运行systemfonts::font_info()检查是否能识别更多系统字体。

  1. 检查并修复系统字体:
  • 打开Mac「字体册」(Font Book),搜索Arial字体:
    • 若缺失:从系统备份或官方渠道恢复Arial字体;
    • 若存在:右键点击Arial字体,选择「验证字体」,修复损坏的字体文件。
  1. 重新导入系统字体到R:
    安装并使用extrafont包重新导入系统字体:
install.packages("extrafont")
library(extrafont)
font_import() # 此过程可能耗时数分钟,耐心等待
loadfonts()

执行后运行fonts(),确认Arial字体已被识别。

修复X11字体配置

若使用X11作为绘图后端,清除无效的Arial配置并重新映射:

# 清除旧配置
X11Fonts(Arial = "")
# 映射到系统可用字体(如苹果系统字体)
X11Fonts(Arial = "-apple-system-%s-%s-*-*-%d-*-*-*-*-*-*-*")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 16:27:54