Re: [R] Text in Boxes?

From: Barry Rowlingson <B.Rowlingson_at_lancaster.ac.uk>
Date: Fri 07 Oct 2005 - 21:38:06 EST

David Reitter wrote:

> text(labels="123", 50, 0.5, bg="green")
>
> I've experimented with legend(),which will make the box too wide, and
> also with rect(), which doesn't know the extent of the text shown.
>

  strwidth and strheight know!

  here's a quickie - adjust to your specifications:

textBox <- function(x,y,text,bg,xpad=.1,ypad=1){

   w=strwidth(text)+xpad*strwidth(text)
   h=strheight(text)+ypad*strheight(text)

   rect(x-w/2,y-h/2,x+w/2,y+h/2,col=bg)
   text(x,y,text)
}

  plot(1:10)
  textBox(7,7,'Hello World This is a test','green',ypad=1)

Baz



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Oct 07 21:41:58 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:28:08 EST