用格子图形上的回归线绘制xyplot

Sel*_*vam 12 r panel lattice

我加载了格子包.然后我跑:

> xyplot(yyy ~ xxx | zzz, panel = function(x,y) { panel.lmline(x,y)}
Run Code Online (Sandbox Code Playgroud)

这会生成绘图板,显示回归线,没有xyplots.我正在做panel.lmline而没有完全理解它是如何完成的.我知道有一个数据参数,什么是数据,知道我有3个变量xxx,yyy,zzz

Rei*_*son 23

你真正需要的是:

xyplot(yyy ~ xxx | zzz, type = c("p","r"))
Run Code Online (Sandbox Code Playgroud)

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’.
Run Code Online (Sandbox Code Playgroud)

正如我上面所示,你可以通过传递type一个字符向量来串行添加它们.基本上,您的代码给出了相同的结果type = "r",即绘制了回归线.

一般来说,和Lattice绘图函数的panel论点xyplot非常强大,但并不总是需要这么复杂的事情.基本上你需要传递panel一个函数,它将在绘图的每个面板上绘制东西.要修改代码以执行您想要的操作,我们还需要添加一个调用panel.xyplot().例如:

xyplot(yyy ~ xxx | zzz,
       panel = function(x, y, ...) {
                 panel.xyplot(x, y, ...)
                 panel.lmline(x, y, ...)
               })
Run Code Online (Sandbox Code Playgroud)

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

xyplot(yyy ~ xxx | zzz,
       panel = function(...) {
                 panel.xyplot(...)
                 panel.lmline(...)
               })
Run Code Online (Sandbox Code Playgroud)

但我通常添加xy论证只是为了清楚.