Re: [R] Draw text with a box surround in plot.

From: <>
Date: Wed, 09 Jun 2010 10:13:58 -0600 (MDT)

Rectangle R centered at (x,y) with width 2w and height 2h is given by



Rotating a point (u,v) at (0,0) by theta degree is given by matrix [cos(theta),-sin(theta)
so we have a new point (u*cos(theta)-v*sin(theta),u*sin(theta)+v*cos(theta)).

Hence rotated R by theta at (x,y) is given by

x.rotated = c(x + (x1-x)*cos(theta)-(y1-y)*sin(theta),
              x + (x2-x)*cos(theta)-(y2-y)*sin(theta),
              x + (x3-x)*cos(theta)-(y3-y)*sin(theta),
              x + (x4-x)*cos(theta)-(y4-y)*sin(theta))
y.rotated = c(y + (x1-x)*sin(theta)+(y1-y)*cos(theta),
              y + (x2-x)*sin(theta)+(y2-y)*cos(theta),
              y + (x3-x)*sin(theta)+(y3-y)*cos(theta),
              y + (x4-x)*sin(theta)+(y4-y)*cos(theta))


But it turns out to be a parallelogram with angles not equal to 90, not a rectangle. See R code below.

Any way to improve this so that the rotated rectangle looks like a rectangle? Thanks,


plot(1:10,1:10,xlim=c(1,20),ylim=c(1,40),type="n", main = "Rotated rectangle looks like a ")
## a rect at (10,20) with w = 3 and h = 2

x = 10
y = 20
w = 3
h = 2


##Rotate it at (10,10) by 45 degree
theta = 45/180*pi

x.rotated = c(10 + (x1-10)*cos(theta)-(y1-20)*sin(theta),
              10 + (x2-10)*cos(theta)-(y2-20)*sin(theta),
              10 + (x3-10)*cos(theta)-(y3-20)*sin(theta),
              10 + (x4-10)*cos(theta)-(y4-20)*sin(theta))
y.rotated = c(20 + (x1-10)*sin(theta)+(y1-20)*cos(theta),
              20 + (x2-10)*sin(theta)+(y2-20)*cos(theta),
              20 + (x3-10)*sin(theta)+(y3-20)*cos(theta),
              20 + (x4-10)*sin(theta)+(y4-20)*cos(theta))


> On 06/04/2010 01:21 AM, wrote:
>> boxed.labels draw text with box well.
>> But, the box cannot be shadowed and srt = 45 seems not to work:
>> text is rotated but the box does not.
>> polygon.shadow can rotate and shadow but have to calculate its
>> dimensions,
>> based on the text length and size.
>> Do you have any other way to draw text with rotated and shadowed box?
> The srt argument was intended to allow the user to rotate the text in 90
> degree increments, and the box just changes shape to fit whatever is in
> it. The underlying function that draws the box (rect) doesn't have a
> rotation argument. It would be possible to write a special function
> using polygon, just do the calculations for box size and then rotate the
> text with srt= and the polygon by transforming the coordinates of the
> vertices, as long as the default justification (center) is used. I can't

> do this right at the moment, but if you are really stuck I might be able
> to do it in the near future.
> Jim
> mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Wed 09 Jun 2010 - 16:16:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Jun 2010 - 20:30:53 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive