破解 R 中的 plot data.frame 方法,以在散点图中包含对角线上的直方图和 Lowess 拟合 [英] Hacking the plot data.frame method in R to include histograms on the diagonals and lowess fits in the scatterplots

查看:23
本文介绍了破解 R 中的 plot data.frame 方法,以在散点图中包含对角线上的直方图和 Lowess 拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虚拟示例:

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)

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

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?

推荐答案

当您像这样绘制 data.frames 时,您基本上是在调用 pairs() 函数.有关详细信息,请参阅 ?pairs.有一个例子,如果那里有直方图.这是一个也绘制黄土线的示例

When you plot data.frames like this, you are basically calling the pairs() function. See ?pairs for more information. 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)

给出

这篇关于破解 R 中的 plot data.frame 方法,以在散点图中包含对角线上的直方图和 Lowess 拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆