Skip to content
Prev 22883 / 398502 Next

Legends and Fonts

Ronnen Levinson wrote:
Don't know how clever it is, but this might do the job:
+ "Weight=100","Weight=150","Weight=130"),ncol=3)
[,1]        [,2]     [,3]
[1,] "Bob"       "Age=7"  "Weight=100"
[2,] "Alexander" "Age=13" "Weight=150"
[3,] "Susan"     "Age=20" "Weight=130"

longest.string<-function(stringvec) {
 longest<-0
 for(i in 1:length(stringvec))
  if(strwidth(stringvec[i]) > longest) longest<-strwidth(stringvec[i])
 return(longest)
}

tab.list<-function(x,y,tl,margin=0.1) {
 dtl<-dim(tl)
 par(adj=0)
 height<-strheight("M")*1.5
 if(length(dtl) > 1) {
  widths<-vector("numeric",0)
  for(i in 1:dtl[2]) widths[i]<-longest.string(tl[,i])
  xpos<-x
  for(i in 1:dtl[2]) {
   for(j in 1:dtl[1]) {
    ypos<-y-(j-1)*height
    text(xpos,ypos,tl[j,i])
   }
   xpos<-xpos + widths[i] * 1.1
  }
 }
 else {
  width<-longest.string(tl)
  for(j in length(tl)) {
   ypos<-y+(i-1)*height
   text(xpos,ypos,tl[j,i])
  }
 }
 rect(x-margin*(xpos-x),
      ypos - (height + margin*(y-ypos)),
      xpos+margin*(xpos-x),
      y + (height + margin*(y-ypos)))
}

plot(...)
tab.list(x,y,tl)

where x and y are the top left corner of the _text_, and there will be a 
bit of margin left around the text within the box.

Jim
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._