||
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="")
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-10-19 22:55
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社