如何修改此相关矩阵图?

Sam*_*rom 7 plot r correlation

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

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)
Run Code Online (Sandbox Code Playgroud)

我想修改如下情节:

  1. 有较小的蓝点作为

    pairs(USJudgeRatings[,c(2:3,6,1,7)],
          main="xxx",
          pch=18,
          col="blue",
          cex=0.8)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在对角线上包含条目的直方图(如此处输入链接描述中所示)

  3. 将相关性和p值显示为

    r=0.9;
    p=0.001;
    
    Run Code Online (Sandbox Code Playgroud)

价值观不是明星.

为配对数据的散点图显示拟合线.拟合的方法是什么?哪条线被定义为上面显示的代码?以及如何改变拟合方法?

Did*_*rts 35

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

对于您的特定情况:

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

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)
}
Run Code Online (Sandbox Code Playgroud)

对于panel.smooth()定义的函数cex=,col=pch=论据.

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, ...)
}
Run Code Online (Sandbox Code Playgroud)

要添加直方图,panel.hist()应定义函数(取自帮助文件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", ...)
}
Run Code Online (Sandbox Code Playgroud)

最终情节:

pairs(USJudgeRatings[,c(2:3,6,1,7)],
          lower.panel=panel.smooth, upper.panel=panel.cor,diag.panel=panel.hist)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述