[R] Lattice: reasons for not passing subscripted "col", "pch", "lty", "fill" to panel functions

Deepayan Sarkar deepayan.sarkar at gmail.com
Thu Jul 3 18:59:06 CEST 2008


On 7/3/08, Vitalie Spinu <vitosmail at rambler.ru> wrote:
> Dear UseRs!
>
>  I am really amazed by lattice capabilities but one thing is really beyond
> my understanding.
>  I would like to have conditional plots with points inside each panel
> colored by groups "gr1" and "pched" by "gr2":
>
>  xyplot(A~B|C,myD,col=gr1,pch=gr2) #this makes sense for me
>
>  But this does not work, because "col","pch","lty","fill" etc. are passed
> directly to panel function and not subscripted by conditional variable C.
> The result is that the same truncated vectors col,pch,lty are used for all
> panels.
>
>  I know I can write something like this to overcome the problem:
>
>  xyplot(A~B|C,myD,col=gr1,pch=gr2,
>        panel=function(x,y,...,subscripts,col,pch){
>
> panel.xyplot(x,y,col=col[subscripts],pch=pch[subscripts]
>         )
>  )
>  but is so clumsy :(.
>
>  Isn't it possible to have
> panel.XUZ(x,y,...,col=col[subscritps],...) or something
> like that? And dispense whith "groups" variable whatsoever?

Sure. Define

my.panel.xyplot <-
    function(x,y,...,subscripts,col,pch) {
        panel.xyplot(x,y,col=col[subscripts],pch=pch[subscripts], ...)
    }

and change your call to

xyplot(A~B|C,myD,col=gr1,pch=gr2, panel = my.panel.xyplot)

If you don't like the extra 'panel' argument, you can set your panel
function to be the default:

lattice.options(panel.xyplot = my.panel.xyplot)

-Deepayan



More information about the R-help mailing list