2012-10-19 154 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