How to adjust graphics co-scatter plot using R function "pairs.panels" in library "psych" ?
1
0
Entering edit mode
4.6 years ago
takoyaki ▴ 120

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 • 3.8k views
ADD COMMENT
0
Entering edit mode
4.6 years ago

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

ADD COMMENT
1
Entering edit mode

Thank you Kevin. It was a great help and I could solve my problem !

ADD REPLY

Login before adding your answer.

Traffic: 2351 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6