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 :
obviously, the following example doesn't work :
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,...)
},
subset=pp38
)
so I figured passing on the subscripts argument to my panel function would be
the way to go, but I've come across some behavior I can't make sense of :
xyplot(pp~nn,groups=vari,data=test,auto.key=list(space=right),arg1=test,arg2=2,arg3=loc,
panel=function(x,y,groups,subscripts,arg1,arg2,arg3,...)
{
panel.superpose(x,y,groups,...)
#étiq-rep(,times=length(x))
#étiq[groups[subscripts]==arg2]-as.character(arg1[subscripts,arg3][groups[subscripts]==arg2])
#étiq-as.character(étiq)
#panel.text(x,y,labels=étiq,pos=1,cex=0.5,...)
},
subset=pp38
)
returns :
Erreur dans inherits(x, factor) : l'argument groups est manquant, avec
aucune valeur par défaut
trying it this way I get a graph, but the groups have gone haywire, and I can't
figure out why
xyplot(pp~nn,groups=vari,data=test,auto.key=list(space=right),arg1=test,arg2=2,arg3=loc,
panel=function(x,y,groups,subscripts,arg1,arg2,arg3,...)
{
panel.superpose(x,y,groups,subscripts,...)
#étiq-rep(,times=length(x))
#étiq[groups[subscripts]==arg2]-as.character(arg1[subscripts,arg3][groups[subscripts]==arg2])
#étiq-as.character(étiq)
#panel.text(x,y,labels=étiq,pos=1,cex=0.5,...)
},
subset=pp38
)
finally, I ran the same call taking the '#' out :
xyplot(pp~nn,groups=vari,data=test,auto.key=list(space=right),arg1=test,arg2=2,arg3=loc,
panel=function(x,y,groups,subscripts,arg1,arg2,arg3,...)
{
panel.superpose(x,y,groups,subscripts,...)
étiq-rep(,times=length(x))
étiq[groups[subscripts]==arg2]-as.character(arg1[subscripts,arg3][groups[subscripts]==arg2])
étiq-as.character(étiq)
panel.text(x,y,labels=étiq,pos=1,cex=0.5,...)
},
subset=pp38
)
which returns :
Erreur dans étiq[groups[subscripts] == arg2] - as.character(arg1[subscripts,
:
NAs interdits dans les affectations indicées
I've racked my head and can't make out what's wrong with this line :
étiq[groups[subscripts]==arg2]-as.character(arg1[subscripts,arg3][groups[subscripts]==arg2])
That's a lot of questions in one, so thanks to anyone who can help me out, even
for just one of them !!
## dataframe follows :
vari-c(3,3,3,3,3,2,3,3,3,3,3,3,1,3,2,2,1,1,
1,1,1,2,3,1,3,3,3,3,3,3,3,3,2,1,3,1,
1,2,2,1,3,3,3,2,1,3,1,2,3,2,1,3,3,3,
1,1,2,1,1,2,1,3,1,2,1,1,1,2,1,1,3,2,
3,3,2,1,1,1,2,3,2,2,2,2,2,2,3,3,3,3,
1,1,1,1,2,1,3,2,3,2)
pp-c(NA,49.440,42.900,44.020,48.892,NA,NA,48.800,49.710,49.200,
46.100,44.075,NA,47.855,42.800,NA,32.696,35.900,NA,NA,
43.249,NA,NA,NA,49.405,49.900,47.200,52.600,51.300,51.200,
NA,54.200,NA,NA,NA,48.378,44.080,48.000,47.400,NA,
51.013,54.900,53.725,48.605,44.033,NA,43.200,49.000,NA,45.800,