简体   繁体   English

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

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

Dummy example: 虚拟示例:

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)

I'd like to take the plot data frame method and augment it to include histograms on the diagonals and lowess fits on each of the scatterplots. 我想采用图数据框方法,并将其扩展为在对角线上包括直方图,并在每个散点图上显示最低拟合。 Is it possible to do without completely re-writing the function? 是否可以在不完全重写功能的情况下进行操作? Where do I even find the source code for methods? 我什至在哪里可以找到方法的源代码?

When you plot data.frames like this, you are basically calling the pairs() function. 当您像这样绘制data.frames时,基本上就是在调用pairs()函数。 See ?pairs for more information. 有关更多信息,请参见?pairs There is an example if a histogram there. 这里有一个直方图的例子。 Here's an example that also plots a loess line 这是一个绘制黄土线的示例

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)

which gives 这使

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM