如何在R中的`pairs()`中绘制回归线而不是最低线?

rno*_*ian 4 plot r

我试图用回归线绘制函数而不是低线来替换for参数,但没有成功panel.smoothpanelpairs()

我尝试创建函数reg并将其放置为参数panel,但这不起作用?有什么解决办法吗?

reg <- function(x, y) abline(lm(y~x)) # made to draw regression line instead of lowess line


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 <- paste0(prefix, txt)
 text(0.5, 0.5, txt, cex = 1.1, font = 4)
 }
 #EXAMPLE OF USE:
pairs(USJudgeRatings[1:5], panel = panel.smooth, # replace HERE for panel.smooth #
  cex = 1.5, pch = 19, col = adjustcolor(4, .4), cex.labels = 2, font.labels = 2, lower.panel = panel.cor)
Run Code Online (Sandbox Code Playgroud)

G5W*_*G5W 5

你从来没有使用过你的功能reg。我稍微修改了一下reg以采用颜色参数。我没有使用panel.smooth(给出黄土曲线),而是将其修改为使用线性模型和您的 reg 函数。

reg <- function(x, y, col) abline(lm(y~x), col=col) 

panel.lm =  function (x, y, col = par("col"), bg = NA, pch = par("pch"), 
    cex = 1, 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)) reg(x[ok], y[ok], col.smooth)
}

pairs(USJudgeRatings[1:5], panel = panel.lm,
    cex = 1.5, pch = 19, col = adjustcolor(4, .4), cex.labels = 2, 
    font.labels = 2, lower.panel = panel.cor)
Run Code Online (Sandbox Code Playgroud)

对