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

时间:2012-10-19 10:12:57

标签: r panel lattice

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

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

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

1 个答案:

答案 0 :(得分:23)

你真正需要的是:

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的{​​{1}}参数和莱迪思一般的绘图函数非常强大,但并不总是需要这么复杂的事情。基本上你需要传递xyplot一个函数,它将绘制绘图的每个面板上的东西。要修改代码以执行您想要的操作,我们还需要添加对panel的调用。 E.g:

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参数只是为了清楚。