[R] strwidth and strheight for rotated text
Stéphane Dray
dray at biomserv.univ-lyon1.fr
Wed Nov 7 11:30:50 CET 2007
Dear All,
I would like to plot text with a box around it. I used strwidth and
strheight to compute the size of the box which is plotted with rect:
z <- rnorm(10)
# horizontal text works
plot(rnorm(10))
x1 <- 5
y1 <- 0
label <- "Label"
cha <- paste(" ", label, " ", sep = "")
xh <- strwidth(cha, cex = par("cex"))
yh <- strheight(cha, cex = par("cex")) * 5/3
rect(x1 - xh/2, y1 - yh/2, x1 + xh/2, y1 + yh/2, col = "white")
text(x1, y1, cha, cex = par("cex"))
Now I would like to do the same with rotated text. I found two solutions:
- modify the par("usr")
- compute ratio to transform width and height computed on the unrotated
text.
These two solutions work for square graphic window (e.g. X11(),
X11(width=3, height=3)..)
But the two solutions does not work for rectangular windows:
# does not works for rotated text using modif of par("usr")
X11(height=4)
plot(z)
x1 <- 5
y1 <- 0
xusr <- par("usr")
par("usr"=xusr[c(3,4,1,2)])
yh <- strwidth(cha, cex = par("cex"))
xh <- strheight(cha, cex = par("cex")) * 5/3
par("usr"=xusr)
rect(x1 - xh/2, y1 - yh/2, x1 + xh/2, y1 + yh/2, col = "white")
text(x1, y1, cha, cex = par("cex"), srt = 90)
# does not works for rotated text using manual modification of xh and yh
X11(height=4)
plot(z)
x1 <- 5
y1 <- 0
xusr <- par("usr")
xh <- strwidth(cha, cex = par("cex"))
yh <- strheight(cha, cex = par("cex")) * 5/3
tmp <- yh
yh <- xh/(xusr[2]-xusr[1])*(xusr[4]-xusr[3])
xh <- tmp/(xusr[4]-xusr[3])*(xusr[2]-xusr[1])
rect(x1 - xh/2, y1 - yh/2, x1 + xh/2, y1 + yh/2, col = "white")
text(x1, y1, cha, cex = par("cex"), srt = 90)
I can not understand why it does not work. Any help would be very
appreciated.
> sessionInfo()
R version 2.6.0 (2007-10-03)
i686-pc-linux-gnu
locale:
LC_CTYPE=fr_FR.UTF-8;LC_NUMERIC=C;LC_TIME=fr_FR.UTF-8;LC_COLLATE=fr_FR.UTF-8;LC_MONETARY=fr_FR.UTF-8;LC_MESSAGES=fr_FR.UTF-8;LC_PAPER=fr_FR.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=fr_FR.UTF-8;LC_IDENTIFICATION=C
Thanks in Advance,
Cheers.
--
Stéphane DRAY (dray at biomserv.univ-lyon1.fr )
Laboratoire BBE-CNRS-UMR-5558, Univ. C. Bernard - Lyon I
43, Bd du 11 Novembre 1918, 69622 Villeurbanne Cedex, France
Tel: 33 4 72 43 27 57 Fax: 33 4 72 43 13 88
http://biomserv.univ-lyon1.fr/~dray/
More information about the R-help
mailing list