Skip to content
Back to formatted view

Raw Message

Message-ID: <43465E1E.6090901@lancaster.ac.uk>
Date: 2005-10-07T11:38:06Z
From: Barry Rowlingson
Subject: Text in Boxes?
In-Reply-To: <9C73BD1D-CC18-49A6-B2E5-04385A3D1F09@gmail.com>

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)

  - possible tweaks may involve text-alignment (look at the 'adj' 
parameter of 'par()'), changing the text colour etc etc!

Baz