希望大神指教:换了一台电脑就运行不了的R代码(报错: plot.new has not been called yet)

今天遇到了一个很奇怪的代码,在别的电脑上能运行,结果在自己的电脑上,却一直报错。主要问题就是legend添加图例部分,我的电脑上运行那一行代码,要么运行成功,但是不显示图例,要么运行不成功,并提示我:Error in (function (s, units = “user”, cex = NULL, font = NULL, vfont = NULL, : plot.new has not been called yet

如果有大神知道是什么原因,可以给我指点一下!!!
代码:

library(rio)
library(plyr)
library(ggplot2)
library(ggpmisc)
library(tidyverse)

setwd("D:/File")
df<-import("data.xlsx")
Model1 <- list(formula = y ~ 1.3+a*x^b,start=list(a=2,b=4))
Model2 <- list(formula = y ~ 1.3+a*x*(1+x)^-b,start=list(a=2,b=2))
Model3 <- list(formula = y ~ 1.3+a*exp(-b*x^-1),start=list(a=6,b=4))

ggplot(df,aes(x=D,y=H))+ 
  geom_point(size=1,colour="grey30")+
  theme_bw()+
  theme(panel.grid = element_blank())+
  labs(x="胸径(cm)",
       y="树高(m)")+
  stat_fit_augment(method = "nls",method.args = Model1,color="green")+
  stat_fit_augment(method = "nls",method.args = Model2,color="red")+
  stat_fit_augment(method = "nls",method.args = Model3,color="purple")
legend(x = 50, y = 13,
       title="拟合曲线",
       fill = c("green","red","purple"),
       legend = c("Model1", "Model2","Model3"),
       cex = 0.75)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/amyniez/article/details/129063167
今日推荐