赖江山的博客分享 http://blog.sciencenet.cn/u/laijiangshan 生态、统计与R语言

博文

自动生成加入R方和P值散点二阶线性回归线图的函数

已有 3252 次阅读 2020-2-10 22:56 |个人分类:RDA|系统分类:科研笔记


plotAnnotatedScatter<-function(x,y,pointCol=rgb(0,0,0,0.7),                               legendPos="topleft", legendCex=1,Xlab="",Ylab="", ... )

{

      # Generate a linear model summary

 

      fit <- lm(y~x+I(x^2))

      fitSum <-summary(fit)

      r2 <- round(fitSum$r.squared,4)

      pVal <- 1-pf(fitSum$fstatistic[1],fitSum$fstatistic[2],fitSum$fstatistic[3])

      # Format the legend for r and p values

      rp <- vector('expression',2)

      rp[1] <- substitute(expression(italic(R)^2 == valueA),

      list(valueA = format(r2,dig=3)))[2]

      rp[2] <- substitute(expression(italic(p) == valueB),

      list(valueB = format(pVal, digits = 2)))[2]

# Plot the data

      plot(x, y, pch=19, cex=1.2, xlab=Xlab,ylab=Ylab,col=pointCol,...)

      # Add line for linear model fit

      lines(sort(x), fitted(fit)[order(x)]) 

      # Add the legend

      legend(legendPos, inset=-0.01,legend = rp, bty = 'n', cex=legendCex)

}


x <- runif(n=20, 2,10)


y <- x^2+rnorm(20,2,10)


plotAnnotatedScatter(x, y,Xlab="",Ylab="") 

fig2.jpeg



https://wap.sciencenet.cn/blog-267448-1217942.html

上一篇:关于adespatial包
下一篇:《数量生态学-R语言的应用》中文版第二版已经出版
收藏 IP: 211.97.130.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-29 19:01

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部