2012-10-19 11 views
12

Ho caricato il pacchetto reticolo. Poi corro:Disegno xyplot con linea di regressione su grafica reticolare

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

Questo produce pannelli di trama, che mostrano la retta di regressione, senza le xyplots. Sto facendo il panel.lmline senza capire completamente come è fatto. So che c'è un argomento di dati, quali sono i dati, sapendo che ho le 3 variabili xxx, yyy, zzz?

risposta

22

Tutto ciò che ha realmente bisogno è:

xyplot(yyy ~ xxx | zzz, type = c("p","r")) 

dove l'argomento type è documentato in ?panel.xyplot

non voglio citare tutti, ma

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’. 

È possibile, come ho mostrato sopra, aggiungi questi in serie passando type un vettore di caratteri. In sostanza, il tuo codice ha dato lo stesso risultato di type = "r", ad esempio solo la linea di regressione è stata tracciata.

L'argomento panel di xyplot e le funzioni di plottaggio di Lattice in generale, è estremamente potente ma non sempre richiesto per cose così complesse. Fondamentalmente è necessario passare panel una funzione che attirerà le cose su ogni pannello della trama. Per modificare il codice per fare ciò che volevi, è necessario aggiungere una chiamata allo panel.xyplot(). Es .:

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

E 'anche molto utile per passare tutti gli altri argomenti sulle singole funzioni del pannello tramite ..., nel qual caso è necessario ... come argomento nelle funzioni anonime (come mostrato sopra). In realtà, probabilmente si può scrivere che parte funzione del pannello come:

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

ma io di solito aggiungere le x e y argomenti tanto per essere chiari.

+0

Gavin, grazie per la risposta. – Selvam

Problemi correlati