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

将Rmarkdown编织为PDF时出现grid.Call.graphics错误求助

解决Rmarkdown编织PDF时grid.Call.graphics的字符集错误

问题本质

这个错误是因为PDF渲染设备无法识别绘图中使用的字符集,尤其是当节点标签包含非ASCII字符(如中文、特殊符号)时,ggplot2/ggraph默认字体在PDF设备下的字符集支持不足。而HTML或普通R环境的绘图设备对字符兼容性更强,所以不会触发错误。

具体解决方法

  • 更换PDF渲染设备
    在Rmarkdown的YAML头部指定cairo_pdf设备,它对多字符集的支持更完善:

    output:
      pdf_document:
        dev: cairo_pdf
    

    或者在单个代码块中指定设备:

    ```{r, dev='cairo_pdf'}
    # 你的绘图代码
    
  • 显式指定支持目标字符的字体
    根据操作系统选择对应字体,比如Windows用"SimHei",Mac用"PingFang SC",Linux用"Noto Sans CJK SC",修改绘图代码中的文本元素:

    tidyclassroom %>%
      activate("nodes") %>%
      ggraph(layout=comparison) +
      geom_edge_link(
        end_cap = circle(12, "pt"),
        color = "grey", 
        arrow = arrow(length=unit(5, "pt"))) +
      geom_node_point(size=10, shape=21, fill=ifelse(V(graph)$sex == 2, "pink", "lightblue")) +
      geom_node_text(aes(label = name), colour = 'black', vjust = 0.4, family = "SimHei") +
      ggtitle('August 2023') +
      theme_graph() +
      theme(plot.title = element_text(hjust = 0.5, family = "SimHei"))
    
  • 统一字符编码
    强制将节点标签的编码设为UTF-8,避免编码混乱:

    Encoding(V(graph)$name) <- "UTF-8"
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 08:11:10