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
相关产品推荐
相关产品推荐

