破解 R 中的 plot data.frame 方法以在对角线上包含直方图并在散点图中包含低拟合

Hacking the plot data.frame method in R to include histograms on the diagonals and lowess fits in the scatterplots

虚拟示例:

N=1000
x1 = rgamma(N,5,10)
x2 = rnorm(N)+x1
x3 = (x1+x2)/(runif(N)+1)
d = data.frame(x1,x2,x3)
plot(d,col=rgb(1,0,0,.5),pch=19,cex=.5)

我想采用绘图数据框方法并对其进行扩充,以在对角线上包含直方图,并在每个散点图上包含低拟合。是否可以不完全重写函数?我在哪里可以找到方法的源代码?

当你像这样绘制 data.frames 时,你基本上是在调用 pairs() 函数。有关详细信息,请参阅 ?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, ...)
}
panel.loess<-function(x, y, ...) {
    ll <- loess(y~x)
    points(x,y, ...)
    nx<-seq(min(x), max(x), length.out=100)
    lines(nx, predict(ll, nx), col="black")

}


pairs(d,col=rgb(1,0,0,.5),pch=19,cex=.5, 
   diag.panel=panel.hist, 
   lower.panel=panel.loess)

这给出了