gpt4 book ai didi

r - 在点阵图形上使用回归线绘制 xyplot

转载 作者:行者123 更新时间:2023-12-03 04:12:05 29 4
gpt4 key购买 nike

我已经加载了lattice包。然后我运行:

> xyplot(yyy ~ xxx | zzz, panel = function(x,y) { panel.lmline(x,y)}

这会生成绘图面板,显示回归线,但不带 xy 图。我正在做 panel.lmline ,但没​​有完全理解它是如何完成的。我知道有一个数据参数,数据是什么,知道我有 3 个变量 xxxyyyzzz

最佳答案

您真正需要的是:

xyplot(yyy ~ xxx | zzz, type = c("p","r"))

其中 type 参数记录在 ?panel.xyplot

我不会全部引用

type: character vector consisting of one or more of the following:
‘"p"’, ‘"l"’, ‘"h"’, ‘"b"’, ‘"o"’, ‘"s"’, ‘"S"’, ‘"r"’,
‘"a"’, ‘"g"’, ‘"smooth"’, and ‘"spline"’. If ‘type’ has more
than one element, an attempt is made to combine the effect of
each of the components.

The behaviour if any of the first six are included in ‘type’
is similar to the effect of ‘type’ in ‘plot’ (type ‘"b"’ is
actually the same as ‘"o"’). ‘"r"’ adds a linear regression
line (same as ‘panel.lmline’, except for default graphical
parameters). ‘"smooth"’ adds a loess fit (same as
‘panel.loess’). ‘"spline"’ adds a cubic smoothing spline fit
(same as ‘panel.spline’). ‘"g"’ adds a reference grid using
‘panel.grid’ in the background (but using the ‘grid’ argument
is now the preferred way to do so). ‘"a"’ has the effect of
calling ‘panel.average’, which can be useful for creating
interaction plots. The effect of several of these
specifications depend on the value of ‘horizontal’.

正如我上面所示,您可以通过传递type字符向量来串联添加这些。本质上,您的代码给出的结果与 type = "r" 相同,即绘制了回归线。

xyplotpanel 参数以及一般的点阵绘图函数非常强大,但并不总是需要如此复杂的事情。基本上,您需要传递 panel 一个函数,该函数将在绘图的每个面板上绘制内容。要修改代码以执行您想要的操作,我们还需要添加对 panel.xyplot() 的调用。例如:

xyplot(yyy ~ xxx | zzz,
panel = function(x, y, ...) {
panel.xyplot(x, y, ...)
panel.lmline(x, y, ...)
})

通过 ... 传递各个面板函数上的所有其他参数也非常有用,在这种情况下,您需要 ... 作为参数匿名函数(如上所示)。事实上,您可以将该面板功能部分编写为:

xyplot(yyy ~ xxx | zzz,
panel = function(...) {
panel.xyplot(...)
panel.lmline(...)
})

但为了清楚起见,我通常添加 xy 参数。

关于r - 在点阵图形上使用回归线绘制 xyplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12972039/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com