r - 如何在散点图矩阵中插入趋势线

标签 r matrix correlation scatter-plot

<分区>

我制作了一个散点图矩阵,并希望将趋势线添加到每个图或仅添加到信号图上。 我的 R 命令: cor(K4Full[,c(6:9,22:25)]) 情节(K4Full[,c(6:8,22:25)])

Scatterplot matrix

最佳答案

您可以尝试以下方法:

pairs(K4Full[,c(6:8,22:25)], panel=panel.smooth)

mtcars 数据集示例:

pairs(mtcars[1:6],panel=panel.smooth)

enter image description here

用 lm 拟合直线:

panel.lm <- function (x, y,  pch = par("pch"), col.lm = "red",  ...) {   
  ymin <- min(y)
  ymax <- max(y)
  xmin <- min(x)
  xmax <- max(x)
  ylim <- c(min(ymin,xmin),max(ymax,xmax))
  xlim <- ylim
  points(x, y, pch = pch,ylim = ylim, xlim= xlim,...)
  ok <- is.finite(x) & is.finite(y)
  if (any(ok)) 
    abline(lm(y[ok]~ x[ok]), 
           col = col.lm, ...)
}

pairs(mtcars[1:6],panel=panel.lm)

enter image description here

关于r - 如何在散点图矩阵中插入趋势线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39667830/

相关文章:

c - 在 C 中读取以前未知大小的矩阵

r - 用 R data.table 行的排列填充 "count matrix"

python - 如何找到两个变量之间的相关性但跨越不同的时间线('lagged correlation')

python - 使用 sklearn 在 Python 中进行典型相关分析

python - 了解 Pandas 的滚动相关性

r - 散点图矩阵 - 错误 : Viewport 'plot_01.panel.1.1.off.vp' was not found"

r - 使用fread导入csv时出现“字符串中嵌入nul”错误

r - 插入符 - 使用 train()、predict() 和 resamples() 的不同结果

R:数据框根据第一个时间戳选择每个ID的最大行

algorithm - 找到矩阵乘积中的单个错误元素?