我已经加载了格子包。然后我运行:
> xyplot(yyy ~ xxx | zzz, panel = function(x,y) { panel.lmline(x,y)}
这会产生绘图面板,显示回归线,没有 xyplots。我正在做 panel.lmline 没有完全理解它是如何完成的。我知道有一个数据参数,数据是什么,知道我有 3 个变量xxx
, yyy
, zzz
?
您真正需要的是:
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"
,即只绘制了回归线。
和格子绘图函数的panel
参数xyplot
通常非常强大,但对于如此复杂的事情并不总是需要。基本上,您需要传递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(...)
})
但为了清楚起见,我通常会添加x
and参数。y