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

R语言绘制qqline时出现plot.new has not been called yet错误求助

解决qqline报错:plot.new has not been called yet

问题根源

你调用的dotplot(ranef(...))属于lattice绘图系统,而qqnorm/qqline是base R绘图系统的函数。lattice绘图会占用当前绘图设备,覆盖后续base绘图的上下文,导致qqline找不到qqnorm创建的绘图对象。

修复方案

方案1:强制开启新绘图设备

在dotplot和qqnorm之间添加dev.new(),为base绘图单独分配窗口:

plot(allEffects(model1))
dotplot(ranef(model1, condVar = TRUE))
dev.new() # 新增该行,启动新绘图窗口
r_int <- ranef(model1)$ElkID$`(Intercept)`
qqnorm(r_int)
qqline(r_int)

方案2:分批次执行绘图代码

先跑完所有lattice绘图,关闭对应设备后再执行base绘图:

# 执行lattice绘图
plot(allEffects(model1))
dotplot(ranef(model1, condVar = TRUE))
dev.off() # 关闭当前lattice绘图设备

# 执行base绘图
r_int <- ranef(model1)$ElkID$`(Intercept)`
qqnorm(r_int)
qqline(r_int)

方案3:替换为base R绘图函数

如果不需要lattice的高级功能,用base R的dotchart替代dotplot,全程使用同一绘图系统:

plot(allEffects(model1))
# 替换lattice的dotplot为base的dotchart
dotchart(ranef(model1)$ElkID$`(Intercept)`)
r_int <- ranef(model1)$ElkID$`(Intercept)`
qqnorm(r_int)
qqline(r_int)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 03:03:09