Skip to content

Instantly share code, notes, and snippets.

@florianhartig
Last active January 3, 2016 06:29
Show Gist options
  • Save florianhartig/8423115 to your computer and use it in GitHub Desktop.
Save florianhartig/8423115 to your computer and use it in GitHub Desktop.
A code snippet creating a pair correlation plot that works well to visualize correlations in large datasets such as MCMC chains or similar. Marginal distributions on the diagonal, correlation density plots on the lower triangle, spearman correlation in scaled numbers on the upper triangle.
library(IDPmisc)
panel.hist <- function(x, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(usr[1:2], 0, 1.5) )
h <- hist(x, plot = FALSE)
breaks <- h$breaks; nB <- length(breaks)
y <- h$counts; y <- y/max(y)
rect(breaks[-nB], 0, breaks[-1], y, col="blue4", ...)
}
panel.cor <- function(x, y, digits=2, prefix="", cex.cor)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- abs(cor(x, y, method = "spearman"))
txt <- format(c(r, 0.123456789), digits=digits)[1]
txt <- paste(prefix, txt, sep="")
if(missing(cex.cor)) cex <- 0.8/strwidth(txt)
text(0.5, 0.5, txt, cex = cex * r)
}
betterPairs <- function(YourData){
return(pairs(YourData, lower.panel=function(...) {par(new=TRUE);ipanel.smooth(...)}, diag.panel=panel.hist, upper.panel=panel.cor))
}
# Example
x = rnorm(10000)
betterPairs(data.frame(A = x, B = 0.6 * x + 0.3 * rnorm(10000), C = rnorm(10000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment