[R] text at the upper left corner outside of the plot region

Jim Lemon jim at bitwrit.com.au
Thu Mar 5 11:32:02 CET 2009


batholdy at googlemail.com wrote:
> Hi,
>
> is there a way to place text at the upper left corner (or another 
> corner) of the plot?
>
> I want to place it really at the upper left corner of the whole plot 
> (the file I get),
> not at the upper left corner of the plot-region.
>
>
>
> I tried text() and mtext(), and corner.label() of the plotrix package 
> but it didn't work out.
>
Hi batholdy,
I suppose we could rewrite corner.label like this:

corner.label<-function(label=NULL,x=-1,y=1,xoffset=NA,yoffset=NA,
 space=c("plot","figure"),...) {

 if(is.na(xoffset)) xoffset<-strwidth("m")/2
 if(is.na(yoffset)) yoffset<-strheight("m")/2
 par.usr<-par("usr")
 xpos<-par.usr[(3+x)/2]
 ypos<-par.usr[(3+y)/2+2]
 if(match(space[1],"figure",0)) {
  par.pin<-par("pin")
  xplotrange<-par.usr[2]-par.usr[1]
  yplotrange<-par.usr[4]-par.usr[3]
  par.mai<-par("mai")
  xmar<-xplotrange*par.mai[3+x]/par.pin[1]
  ymar<-yplotrange*par.mai[2+y]/par.pin[2]
  cat(xmar,ymar,"\n")
  xpos<-xpos+x*xmar
  ypos<-ypos+y*ymar
 }
 if(!is.null(label)) {
  if(match(space[1],"figure",0)) par(xpd=TRUE)
  text(xpos-x*xoffset,ypos-y*yoffset,label,adj=c((1+x)/2,(1+y)/2))
  if(match(space[1],"figure",0)) par(xpd=FALSE)
 }
 return(list(x=xpos,y=ypos))
}

The default behavior is to put the label just inside one corner of the 
plot. If you set "space" to "figure", the label will appear just inside 
one corner of the figure region. Owzat?

Jim



More information about the R-help mailing list