psych's implementation of this is merely a wrapper of the base R function, pairs()
, I believe. In any case, here is where you can become an 'R Surgeon'. I have greatly modified the pairs()
functions in R in the past, but it can take some time.
You can access the code behind psych's implementation of pairs()
by typing
psych::pairs.panels
The panel function in which you'll be interested is:
"panel.cor" <- function(x, y, prefix = "", ...) {
usr <- par("usr")
on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
if (is.null(wt)) {
r <- cor(x, y, use = "pairwise", method = method)
}
else {
r <- cor.wt(data.frame(x, y), w = wt[, c(1:2)])$r[1,
2]
}
txt <- format(c(round(r, digits), 0.123456789), digits = digits)[1]
txt <- paste(prefix, txt, sep = "")
if (stars) {
pval <- r.test(sum(!is.na(x * y)), r)$p
symp <- symnum(pval, corr = FALSE, cutpoints = c(0,
0.001, 0.01, 0.05, 1), symbols = c("***", "**",
"*", " "), legend = FALSE)
txt <- paste0(txt, symp)
}
cex <- cex.cor * 0.8/(max(strwidth("0.12***"), strwidth(txt)))
if (scale) {
cex1 <- cex * abs(r)
if (cex1 < 0.25)
cex1 <- 0.25
text(0.5, 0.5, txt, cex = cex1)
}
else {
text(0.5, 0.5, txt, cex = cex)
}
}
You simply then need to edit those text()
commands to introduce a colour shade scaled ranged between -1 to +1 for correlation.
I did this just out of interest. It allows you to identify a colour based on a correlation value at 0.01 resolution:
mypalette <- colorRampPalette(c('royalblue','red2'))
cols <- mypalette(200)[cut(seq(-1, 1, 0.01), breaks = 200)]
names(cols) <- seq(-1, 1, 0.01)
cols["0.65"]
0.65
"#CF1227"
cols["-0.5"]
-0.5
"#6B4FA9"
Kevin
Thank you Kevin. It was a great help and I could solve my problem !