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

博文

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

已有 12123 次阅读 2016-12-18 22:29 |个人分类: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)


      fitSum <- summary(fit)


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


      pVal <- fitSum$coefficients[2,4]


     


      # 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


      abline(fit)


     


      # Add the legend


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


}


x <- rnorm(n=20, mean=7, sd=3)

y <- x+rnorm(20)

plotAnnotatedScatter(x, y,Xlab="",Ylab="") #直接输入你的X和Y就可以了





https://blog.sciencenet.cn/blog-267448-1021594.html

上一篇:“基于R语言统计分析方法”的培训在尼泊尔特里布大学开班
下一篇:图里如何组合paste()和expression()函数完成特殊字符的文本
收藏 IP: 61.149.199.*| 热度|

1 梅卫平

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

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

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

GMT+8, 2024-3-29 06:35

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部