如何在从r中的循环获取的绘图中使用注释\自定义或其他函数打印上标

如何在从r中的循环获取的绘图中使用注释\自定义或其他函数打印上标,r,loops,ggplot2,plot,ggsave,R,Loops,Ggplot2,Plot,Ggsave,我想打印表达式和r平方;然而,获得r平方的文本的方法是不合适的。我正在使用一个列表或多个线性模型,使用注释自定义,我可以毫无问题地找到文本 下面是我的代码示例: j=c("ggplot2","grid","stringi","data.table") lapply(j,require,character.only=TRUE) set.seed(4321) V=sample(1:1800,100,replace=FA

我想打印表达式和r平方;然而,获得r平方的文本的方法是不合适的。我正在使用一个列表或多个线性模型,使用注释自定义,我可以毫无问题地找到文本

下面是我的代码示例:

j=c("ggplot2","grid","stringi","data.table")
lapply(j,require,character.only=TRUE)

set.seed(4321)
V=sample(1:1800,100,replace=FALSE);sort(V)
e=runif(15,0.66,0.8)
s=runif(15,0.77,0.92)
Dabase=list()

for (i in 1:5){
Dabase[[i]]=data.frame(x=round(V*e[i]),y=round(V*s[i]))
}

lms=list();s=list();NCoef=list();Coef=list()
for(i in 1:5){ 
lms[[i]]=(lm(y~x,Dabase[[i]]))   
s[[i]]=summary(lms[[i]])   
NCoef[[i]]=names(lms[[i]]$coefficients)
Coef[[i]]=signif(as.numeric(lms[[i]]$coefficients),digits=3) 
}

Dvcoe <- as.data.frame(t(stri_list2matrix(Coef)))
colnames(Dvcoe)=c("Intercept",paste(rep(("VarCoef"),(dim(Dvcoe)[2]-1)),
seq(1:(dim(Dvcoe)[2]-1)),sep="_"))
Dvcoe$Formula=rep("y~x",5)

label=list();grob1=list();grob2=list()
for (i in seq_along(Dabase)){ 
grob1[[i]]=grobTree(textGrob(paste(substr(Dvcoe$Formula[i],1,1),"=",Dvcoe[i,1],"+",Dvcoe[i,2],"*",substr(Dvcoe$Formula[i],3,3)), x=0.01,  y=0.95, hjust=0,gp=gpar(col="black", fontsize=5,colour="black",fontface="plain",family = "Times New Roman")))
}

for (i in seq_along(s)){
label[[i]]=paste("r^2==", s[[i]]$r.squared)
grob2[[i]]=grobTree(textGrob(label[[i]], x=0.01,  y=0.91, hjust=0,
                           gp=gpar(col="black", fontsize=5,colour="black",fontface="plain",family = "Times New Roman")))
}

p=list()
for (i in 1:5) {
p[[i]]=ggplot(Dabase[[i]],aes(Dabase[[i]][,2],Dabase[[i]][,1]))+
geom_point(size=1,color="firebrick")+
geom_abline(intercept=Dvcoe[i,1], slope=Dvcoe[i,2],colour="dodgerblue",lwd=1)+
theme_classic()+geom_smooth(method="lm",colour="dodgerblue",lwd=1)+
scale_x_continuous(expand = c(0,0.015))+
labs(x=paste(substr(Dvcoe$Formula[i],3,3)), y=paste(substr(Dvcoe$Formula[i],1,1)))+theme(text= element_text(size=7, family = "Times New Roman", color="black"),
                                                   axis.text.x = element_text(angle = 0,hjust=0.5, vjust = 0.5, face="plain", colour="black",size=7),
                                                   axis.text.y = element_text(angle = 0,hjust=0.5, vjust = 0.5, face="plain",colour="black",size=7),
                                                   axis.title.y = element_text(angle = 0,hjust=0.5, vjust = 0.5, face="plain",colour="black"))
p[[i]] + annotation_custom(grob1[[i]])+
annotation_custom(grob2[[i]])
ggsave(filename=paste0("C:/Users/Desktop/Trying/lm",rownames(Dvcoe)[i],".tiff"), 
     last_plot(),device="tiff",width= 10, height= 7,units="cm",dpi=600)
}
j=c(“ggplot2”、“grid”、“stringi”、“data.table”)
lappy(j,require,character.only=TRUE)
种子集(4321)
V=样品(1:1800100,替换=假);分类(五)
e=runif(15,0.66,0.8)
s=runif(15,0.77,0.92)
Dabase=list()
(我在1:5中){
Dabase[[i]]=data.frame(x=round(V*e[i]),y=round(V*s[i]))
}
lms=列表();s=列表();NCoef=list();Coef=列表()
因为(我在1:5中){
lms[[i]]=(lm(y~x,Dabase[[i]]))
s[[i]]=摘要(lms[[i]])
NCoef[[i]]=名称(lms[[i]]$系数)
Coef[[i]]=signif(作为数字(lms[[i]]$系数),数字=3)
}

Dvcoe我无法使用annotation_custom()实现此功能,但它可以使用annotate()
label1=paste(“y==”,lms$系数[1],“+”,lms$系数[2],“*x”)
label2=paste(“r^2=”,圆形(s$r.squared,2))
label=c(粘贴(label1),粘贴(label2))
p+注释(“文本”,x=c(3000000000010000000),y=c(最大值)(df$y),0.95*df$y)),label=TRUE
返回数据帧中的错误
错误(x=x*y[i],y=x[i]*y):参数表示行数不同:500行,44行
由于代码中的错误,我表示歉意。这是经过编辑的,我很感谢你的回答,但问题还在继续。