如何修改此相关矩阵图? [英] How to modify this Correlation Matrix plot?

查看:144
本文介绍了如何修改此相关矩阵图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来显示相关矩阵,

I have the following codes to display a correlation matrix,

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)

我想修改剧情,例如:

  1. 具有较小的蓝点作为

  1. Have smaller blue dots as

pairs(USJudgeRatings[,c(2:3,6,1,7)],
      main="xxx",
      pch=18,
      col="blue",
      cex=0.8)

  • 包括对角线上条目的直方图(如将相关性和p值显示为

    r=0.9;
    p=0.001;
    

  • 值不是星号.

    显示一条拟合线,用于配对数据的散点图.拟合的方法是什么?上面显示的代码将配件定义为哪一行?以及如何更改拟合方法?

    There is a fitting line displayed for the scatter plot of the paired data. What is the method used for the fitting? Which line is defined the fitting as the codes shown above? And how to change the fitting method?

    推荐答案

    函数pairs()的帮助页面为您提供了如何定义要绘制的面板的示例.

    Help page for the function pairs() gives you example how to define panels to plot.

    针对您的特定情况:

    更改了panel.cor()函数以显示为文本行-p值和相关系数.

    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)
    }
    

    对于panel.smooth()函数定义的cex=col=pch=自变量.

    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, ...)
    }
    

    要添加直方图,应定义panel.hist()函数(取自pairs()的帮助文件)

    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)
    

    这篇关于如何修改此相关矩阵图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆