This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to adjust graphics co-scatter plot using R function "pairs.panels" in library "psych" ?

Hi, everyone !

I am drawing co-scatter plot with correlation coefficient using R function "pairs.panels" in library "psych". It is like this

https://camo.qiitausercontent.com/8c9673bfde6691edde4465c6ff43c7c893709c9b/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f3136353738392f31643432623462312d393266632d326132352d663936332d6664666435633339613236652e6a706567

In pairs.panels function, the font size of correlation coefficient can be changed depending of its value, but I don't want to use this. I want to change color of correlation coefficient and its background depending of its value.

Does anyone know how to change font and background color of correlation coefficient ?

Or is that impossible in function "pairs.panels" ?

Thank you !

r graph rna-seq

1 answer

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 !

Log in to answer this question.