||
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就可以了
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-21 16:32
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社