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

R语言diagram包绘图提示plot.new()尚未调用报错如何解决

问题复现

使用最新版diagram包运行下述长期正常执行的代码时,会在调用straightarrow()环节抛出plot.new() has not yet been called错误:

suppressMessages(library(diagram))

## open plot 
par(mar = c(0.5, 0.5, 0.5, 0.5))
openplotmat()
## define number of boxes at each level
elpos <- coordinates (c(3, 1, 1, 2, 2, 1, 1, 1))
## generate matrix of from-to arrow coordinates
fromto <- matrix(ncol = 2, byrow = TRUE,
                 data = c(1, 6,
                          2, 4,
                          3, 7,
                          4, 5,
                          5, 6,
                          5, 7,
                          6, 8,
                          7, 9,
                          8, 10, 
                          9, 10,
                          10, 11,
                          11, 12))
nr <- nrow(fromto)
arrpos <- matrix(ncol = 2, nrow = nr)

# 错误触发位置
i = 1
arrpos[i, ] <- straightarrow(to = elpos[fromto[i, 2], ],
                                from = elpos[fromto[i, 1], ],
                                lwd = 1, arr.pos = 0.6, arr.length = 0.2)
故障原因

diagram包版本更新后,openplotmat()函数移除了旧版本内置的隐式plot.new()调用,不再自动完成绘图设备初始化,后续底层绘图函数检测不到激活的画布就会抛出该错误。

修复方法

在绘图初始化环节最前面显式调用plot.new()创建空白画布,注意par()边距设置要放在plot.new()之后才能生效,修改后的初始化代码如下,其余原有业务代码无需改动即可正常运行:

suppressMessages(library(diagram))

# 显式初始化绘图画布
plot.new()
par(mar = c(0.5, 0.5, 0.5, 0.5))
openplotmat()

elpos <- coordinates (c(3, 1, 1, 2, 2, 1, 1, 1))
fromto <- matrix(ncol = 2, byrow = TRUE,
                 data = c(1, 6,
                          2, 4,
                          3, 7,
                          4, 5,
                          5, 6,
                          5, 7,
                          6, 8,
                          7, 9,
                          8, 10, 
                          9, 10,
                          10, 11,
                          11, 12))
nr <- nrow(fromto)
arrpos <- matrix(ncol = 2, nrow = nr)

i = 1
arrpos[i, ] <- straightarrow(to = elpos[fromto[i, 2], ],
                                from = elpos[fromto[i, 1], ],
                                lwd = 1, arr.pos = 0.6, arr.length = 0.2)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 21:48:19