I see that there is already code available to add Pearson correlation coefficients and p values to a scatterplot matrix. Now I would simply like to add Spearman correlation coefficients in the upper panels. Currently I can add Pearson correlation coefficients by calling upper.panel=panel.cor
, as in the below.
# the code for creating a scatterplot matrix
pairs(mat, upper.panel=panel.cor)
panel.cor
is a function supplied in a previous Stack Overflow post~
# panel.cor, the code for calculating coefficients and p values
panel.cor <- function(x, y, digits = 2, cex.cor, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
# correlation coefficient
r <- cor(x, y, **method="spearman"**)
txt <- format(c(r, 0.123456789), digits = digits)[1]
txt <- paste("r= ", txt, sep = "")
text(0.5, 0.6, txt)
# p-value calculation
p <- cor.test(x, y, **method="spearman"**)$p.value
txt2 <- format(c(p, 0.123456789), digits = digits)[1]
txt2 <- paste("p= ", txt2, sep = "")
if(p<0.01) txt2 <- paste("p= ", "<0.01", sep = "")
text(0.5, 0.4, txt2)
}
In an attempt to modify this for Spearman's coefficients and p values I added "method="spearman" in the two places above indicated in bold. When I ran panel.cor
there were no error messages, but the upper panels from the code above came out blank.
I have also tried replacing cor with rcor
from the ltm
package and that also did not work. If I run rcor
in place of cor
I get the message:
Error in lower.panel(...) : could not find function "rcor"
When I run the above line of code using rcor.test
in place of both cor
and cor.test
I get the message
Error in combn(p, 2) : n < m
Any ideas?