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

R语言grid.Call报错:polygon edge not found及Arial字体缺失求助

解决R绘图时grid.Call(C_textBounds)报错与Arial字体缺失问题

问题重现

运行以下代码时触发错误:

library(ggplot2)
qplot(mpg, wt, data=mtcars)

# 或
pheatmap(sampleDistMatrix,
         clustering_distance_rows=sampleDists,
         clustering_distance_cols=sampleDists,
         col=colors)

错误信息

Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
  polygon edge not found

警告信息

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

已尝试的无效方案

  • 重装RStudio
  • 在Apple字体册中关闭(无法删除)重复的Arial字体
  • 强制ggplot2使用其他字体(仅解决单个包问题,无法覆盖pheatmap等其他包)

系统相关信息

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"


$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检测结果

> 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  max_a…⁶ max_d…⁷ max_a…⁸
  <chr>        <int> <chr>  <chr> <lgl>  <lgl> <lgl>   <ord>  <ord> <lgl>   <lgl> <lgl>   <lgl>     <int>   <int>   <int> <lis>   <dbl>   <dbl>   <dbl>
1 /System/Lib…     0 Helve… Regu… FALSE  FALSE FALSE   normal norm… FALSE   FALSE TRUE    FALSE      2252       0      10 <dbl>    9.23   -2.77      18
# … with 4 more variables: max_advance_height <dbl>, lineheight <dbl>, underline_pos <dbl>, underline_size <dbl>, and abbreviated variable names
#   ¹​monospace, ²​scalable, ³​vertical, ⁴​n_glyphs, ⁵​n_charmaps, ⁶​max_ascend, ⁷​max_descend, ⁸​max_advance_width
# ℹ Use `colnames()` to see all variable names

解决方案

1. 修复系统Arial字体安装

在macOS上,Arial属于系统字体但可能出现损坏或索引异常:

  • 打开「字体册」,搜索Arial,勾选所有已关闭的Arial字体变体(Regular/Bold/Italic等)
  • 如果字体仍异常,执行以下命令重建字体缓存:
atsutil databases -remove
atsutil server -shutdown
atsutil server -start

重启RStudio后测试绘图。

2. 全局替换默认字体为系统可用字体

修改R的全局字体配置,让所有依赖grid的绘图包(包括ggplot2、pheatmap)自动使用系统已有的Helvetica字体:

# 查看系统可用字体
systemfonts::system_fonts()

# 设置全局sans-serif字体为Helvetica
options(device = function(...) {
  grDevices::png(..., type = "cairo")
})
pdf.options(family = "Helvetica")

# 同时修改ggplot2的全局默认字体
ggplot2::theme_update(text = ggplot2::element_text(family = "Helvetica"))

3. 重置R的字体配置文件

如果上述方法无效,删除R的字体配置文件并重新生成:

# 删除旧的字体配置
file.remove(file.path(Sys.getenv("HOME"), ".R", "fonts", "fontconfig.cache-3"))

# 重新生成字体配置
systemfonts::register_fonts()

重启R后验证字体是否被正确识别。


内容的提问来源于stack exchange,提问作者la.poubelle

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 02:15:41