Skip to content

Instantly share code, notes, and snippets.

@SamBuckberry
Last active August 29, 2015 14:23
Show Gist options
  • Save SamBuckberry/a83ddd3535d0de4addf0 to your computer and use it in GitHub Desktop.
Save SamBuckberry/a83ddd3535d0de4addf0 to your computer and use it in GitHub Desktop.
Quick plotting of correlations with P-values
# See examples at http://sambuckberry.github.io/2015/07/15/quickCorPlots/
plotAnnotatedScatter <- function(x, y, pointCol=rgb(0,0,0,0.7),
legendPos="topleft", legendCex=1, ... ){
# Generate a linear model summary
fit <- lm(y ~ x)
fitSum <- summary(fit)
r2 <- fitSum$r.squared
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, col=pointCol,...)
# Add line for linear model fit
abline(fit)
# Add the legend
legend(legendPos, legend = rp, bty = 'n', cex=legendCex)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment