Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent cex from scaling with the correlation coefficient in chart.Correlation()

Tags:

r

correlation

I'm trying to plot a huge matrix of correlation coefficients, and currently, my plot looks like this: Notice that some cells are missing correlation coefficients

Notice that some cells are missing correlation coefficients (ignore for now that lack of symmetry of the plot, unless you happen know why that's the case, too). I believe that the values are not, in fact, missing, but simply too small to appear, because they are scaled by the value of their correlation coefficient.

Looking at the documentation for chart.Correlation(), I was able to find a function from which much of the content of chart.Correlation() was modeled:

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 <- 0.8/strwidth(txt)    
    test <- cor.test(x,y)
    # borrowed from printCoefmat
    Signif <- symnum(test$p.value, corr = FALSE, na = FALSE,
                  cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
                  symbols = c("***", "**", "*", ".", " "))

    text(0.5, 0.5, txt, cex = cex * r)
    text(.8, .8, Signif, cex=cex, col=2)
}
pairs(USJudgeRatings[,c(2:3,6,1,7)], lower.panel=panel.smooth, upper.panel=panel.cor)

If I change:

text(0.5, 0.5, txt, cex = cex * r)

To:

text(0.5, 0.5, txt, cex = 0.8)

I get roughly the effect I'm looking for. The problem is that I have no idea how to change this parameter using chart.Correlation() itself. Does issue this make sense?

like image 503
Atticus29 Avatar asked Jan 19 '26 09:01

Atticus29


1 Answers

Here's a hack that modifies the function to allow what you want. This adds a user-settable exponent argument to the function that allows you to change the cex value to cex*[correlation]^cex.cor.scale.

It spits out warnings because of the way ... is handled; they're annoying but harmless.

It would probably be best to contact the maintainer and ask them if they'd be willing to enhance the function, or start creating your own modified version of the package.

edit: slightly more robust changes to the relevant lines

library("PerformanceAnalytics")
## turn the function into a character string
tmpstr <- deparse(chart.Correlation)
## modify the relevant lines
panelcorline <- grep("^ *panel.cor",tmpstr)
tmpstr[panelcorline] <- paste(tmpstr[panelcorline],"cex.cor.scale=1,")
rscaleline <- grep("^ *text\\(0.5",tmpstr)
tmpstr[rscaleline] <- gsub("cex \\* r","cex*r^cex.cor.scale",tmpstr[rscaleline])
## convert back to a function (don't mask the original function)
my.chart.Correlation <- eval(parse(text=tmpstr))

Test it out:

data(managers)
chart.Correlation(managers[,1:8], histogram=TRUE, pch="+")
## no scaling
my.chart.Correlation(managers[,1:8], histogram=TRUE, pch="+",cex.cor.scale=0)
## enhanced scaling
my.chart.Correlation(managers[,1:8], histogram=TRUE, pch="+",cex.cor.scale=2)
like image 54
Ben Bolker Avatar answered Jan 20 '26 21:01

Ben Bolker