简体   繁体   中英

Plot scatterplot matrix with partial correlation coefficients in R

I use a modified version of the pairs function to produce a scatterplot matrix:

pairs.cor <- function (x,y,smooth=TRUE, digits=2,  ...)
{
  panel.cor <- function(x, y, ...)
  {
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    r.obj = cor.test(x, y,use="pairwise",...)
    r = as.numeric(r.obj$estimate)
    p = r.obj$p.value
    mystars <- ifelse(p < .05, "* ", " ")
    txt <- format(c(r, 0.123456789), digits=digits)[1]
    txt <- paste(txt, mystars, sep="")
    text(0.5, 0.5, txt)
  }
panel.hist <- function(x)
  {
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(usr[1:2], 0, 1.5) )
    h <- hist(x, plot = FALSE)
    breaks <- h$breaks; nB <- length(breaks)
    y <- h$counts; y <- y/max(y)
    rect(breaks[-nB], 0, breaks[-1], y, col="cyan")
  }
pairs(x,diag.panel=panel.hist,lower.panel=panel.cor,upper.panel=panel.smooth, ...)
} 

pairs.cor(iris[,1:4])

which looks like this:

在此处输入图片说明

What I would like to do is to put the partial correlation coefficients instead of the pairwise Pearson's r into the lower panel.

I can calculate the partial correlation coefficients easily:

library(ppcor)
pcor(iris[,1:4])$estimate

But I couldn't figure out how to modify the lower panel function panel.cor so that it shows these values. The problem seems to be that the lower panel function handles the pairwise x and y values, whereas the partial correlation function pcor requires the entire data frame (or matrix).

Looks like pairs doesn't make this very easy. Simplest thing I could come up with is to have panel.cor peek into the parent data.frame to find the row/col index for the current panel and then you can use that to index into pre-calculated values. Here's the updated panel.cor function

panel.cor <- function(x, y, ...) {
    env <- parent.frame(2)
    i <- env$i
    j <- env$j
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    r = as.numeric(pp[i,j])
    txt <- format(c(r, 0.123456789), digits=digits)[1]
    text(0.5, 0.5, txt)
}

Here use use parent.frame(2) to actually grab the local variables i and j from the pairs.default function. And we assume that pp contains the values from pcor . So you would define that variable before calling pairs.cor

pp <- ppcor::pcor(iris[,1:4])$estimate
pairs.cor(iris[,1:4])

This gives the following result

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM