【1.5】pairs比较多因素之间的关系

一批测序数据下来以后,需要比较一下不同变量之间的关系。

    ## changed panel.cor() function to show to lines of text - p-values and correlation coefficients.
    panel.cor <- function(x, y, digits=2, 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]
        test <- cor.test(x,y)
        Signif <- ifelse(round(test$p.value,3)<0.001,"p<0.001",paste("p=",round(test$p.value,3)))  
        text(0.5, 0.25, paste("r=",txt))
        text(.5, .75, Signif)
    }


    #For panel.smooth() function defined cex=, col= and pch= arguments.
    panel.smooth<-function (x, y, col = "blue", bg = NA, pch = 18, 
                                                    cex = 0.8, col.smooth = "red", span = 2/3, iter = 3, ...) 
    {
        points(x, y, pch = pch, col = col, bg = bg, cex = cex)
        ok <- is.finite(x) & is.finite(y)
        if (any(ok)) 
            lines(stats::lowess(x[ok], y[ok], f = span, iter = iter), 
                        col = col.smooth, ...)
    }


    #To add histograms, panel.hist() functions should be defined (taken from help file of pairs())
    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(USJudgeRatings[,c(2:3,6,1,7)],
                        lower.panel=panel.smooth, upper.panel=panel.cor,diag.panel=panel.hist)

参考资料:

https://stackoverflow.com/questions/15271103/how-to-modify-this-correlation-matrix-plot

个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn

Sam avatar
About Sam
专注生物信息 专注转化医学