[R] labelling specific points xyplot

GOUACHE David D.GOUACHE at arvalisinstitutduvegetal.fr
Thu Sep 6 14:49:46 CEST 2007


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=pp>38
		)

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=pp>38
		)

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=pp>38
		)

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=pp>38
		)

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,
NA,49.800,50.200,52.900,NA,41.400,47.899,40.000,NA,NA,
NA,53.100,NA,39.400,40.700,NA,NA,48.703,NA,NA,
39.424,41.300,45.800,49.760,44.820,NA,NA,NA,NA,47.700,
43.600,43.700,45.800,NA,45.490,NA,53.200,38.300,45.500,NA,
35.900,37.104,34.296,44.000,47.000,43.499,49.804,41.126,48.900,49.000)

nn<-c(NA,18343.82,17582.40,20310.26,18482.91,NA,NA,21677.50,
21219.01,24817.07,20954.45,24261.90,NA,20258.84,24649.53,NA,
30644.43,30222.84,NA,NA,22909.93,NA,NA,NA,
18120.95,19779.56,22563.56,17699.62,26110.50,13691.41,NA,NA,
NA,NA,NA,25032.54,21234.12,20541.67,20105.49,NA,
17902.62,21985.43,22203.39,19880.48,23485.20,NA,NA,NA,
NA,23908.30,NA,19116.47,19322.71,NA,NA,20531.40,
22734.24,NA,NA,NA,NA,15106.30,NA,23477.16,
17420.15,NA,NA,16507.59,NA,NA,18529.41,24382.57,
20802.68,16942.67,NA,NA,NA,NA,NA,22851.15,
23807.34,NA,NA,NA,NA,NA,14624.06,17780.68,
18615.38,NA,NA,19136.01,22394.33,NA,21468.08,20392.55,
20580.23,25215.19,21650.59,26428.57)


loc<-c("3501","7699","6903","6903","6903","269","1083","6903","198","198",
"198","198","5503","198","269","7601","3002","3002","6001","249",
"6701","201","201","1002","4198","4198","4198","4198","4198","4198",
"2863","2863","6082","6254","201","6801","5101","851","851","1001",
"1400","1401","1401","1401","6082","6101","6082","278","5398","5398",
"8000","6903","6903","4102","5503","5503","2720","7761","7761","201",
"1412","2801","5472","5202","5599","5472","1057","2720","1057","1057",
"3601","3601","3601","3601","201","5503","1401","5472","6092","4199",
"5175","5175","265","8056","8000","265","8901","8901","8901","6092",
"8999","8999","8999","1002","1002","2490","2490","2700","2700","2720")

test<-data.frame(vari=vari,pp=pp,nn=nn,loc=loc)

David Gouache
Arvalis - Institut du Végétal
Station de La Minière
78280 Guyancourt
Tel: 01.30.12.96.22 / Port: 06.86.08.94.32



More information about the R-help mailing list