[R] labelling specific points xyplot

Deepayan Sarkar deepayan.sarkar at gmail.com
Thu Sep 6 22:08:47 CEST 2007


On 9/6/07, GOUACHE David <D.GOUACHE at arvalisinstitutduvegetal.fr> wrote:
> Hello R-helpers,
>
> I'm trying to add labels to points in xyplot graphs, but I want lo label only those points which have a certain level of my grouping variable, and have encountered a few problems.
> An example dataframe that goes with the following code is at the end of this message.
>
> 1st step, adding labels (from another column in the dataframe) to my chosen points  :
> I've figures this out, but I am passing my whole dataframe as an extra argument (arg1) which I realize is probably useless, so if anybody can give me a way to work around this...
>
> xyplot(pp~nn,groups=vari,data=test,auto.key=list(space="right"),arg1=test,arg2="2",arg3="loc",
>                 panel=function(x,y,groups,arg1,arg2,arg3,...)
>                 {
>                         panel.superpose(x,y,groups,...)
>                         étiq<-rep("",times=length(x))
>                         étiq[groups==arg2]<-as.character(arg1[groups==arg2,arg3])
>                         étiq<-as.character(étiq)
>                         panel.text(x,y,labels=étiq,pos=1,cex=0.5,...)
>                 }
>                 )
>
> What I would also like is to write a function that is robust for multiple panel displays or subsetting :
>

This is not the only way, but the most transparent one I can think of:

xyplot(pp ~ nn, groups = vari,data = test,
       labels = test$loc, do.label = (test$vari == "2"),
       auto.key=list(space="right"),
       panel = function(x, y, groups, subscripts, labels, do.labels, ...) {
           panel.xyplot(x, y, groups = groups, subscripts = subscripts, ...)
           labels <- labels[subscripts]
           do.labels <- do.labels[subscripts]
           panel.text(x[do.labels], y[do.labels],
                      labels = labels[do.labels],
                      pos = 1, cex=0.5, ...)
       })

-Deepayan



More information about the R-help mailing list