2012-10-19 113 views
12

我已经加载了格子包。 然后我运行:用格线图形上的回归线绘制xyplot

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

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

回答

22

您真正需要的是:

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(...) 
       }) 

但我通常添加xy参数仅仅是明确的。

+0

Gavin谢谢你的回答。 – Selvam