Skip to content

Instantly share code, notes, and snippets.

@jlehtoma
Created March 27, 2017 15:51
Show Gist options
  • Save jlehtoma/bbb9cc0cac1bc0e81fbe809d415a67cf to your computer and use it in GitHub Desktop.
Save jlehtoma/bbb9cc0cac1bc0e81fbe809d415a67cf to your computer and use it in GitHub Desktop.
Plot correaltion pairs
# panel.smooth function is built in.
# panel_cor puts correlation in upper panels, size proportional to correlation
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))
txt <- format(c(r, 0.123456789), digits = digits)[1]
txt <- paste(prefix, txt, sep = "")
if (missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
text(0.5, 0.5, txt, cex = cex.cor * r)
}
plot_pairs <- function(dat) {
pairs(dat,lower.panel = panel.smooth, upper.panel = panel_cor,
pch = 20)
}
dat <- data.frame(X1 = rnorm(100, 0, 1),
X2 = rnorm(100, 0, 1))
plot_pairs(dat)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment