Thanks a lot Brian, you were completely correct. The good way to do it :
z <- rnorm(10) x1 <- 5 y1 <- 0 label <- "Label" cha <- paste(" ", label, " ", sep = "") X11(height=4) plot(z) xusr <- par("usr") xh <- strwidth(cha, cex = par("cex")) yh <- strheight(cha, cex = par("cex")) * 5/3 tmp <- xh xh <- yh/(xusr[4]-xusr[3])*par("pin")[2] xh <- xh/ par("pin")[1] * (xusr[2]-xusr[1]) yh <- tmp/(xusr[2]-xusr[1])* par("pin")[1] yh <- yh/ par("pin")[2] * (xusr[4]-xusr[3]) rect(x1 - xh/2, y1 - yh/2, x1 + xh/2, y1 + yh/2, col = "white") text(x1, y1, cha, cex = par("cex"), srt = 90) Prof Brian Ripley wrote: > See ?par, 'cxy' for how to go from width/heights in inches to user > coordinates and vice versa. You appear to have overlooked 'pin'. > > On Wed, 7 Nov 2007, Stéphane Dray wrote: > >> 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 ([EMAIL PROTECTED] ) 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/ ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.