How to get the width and height of a character in usr coordinates

How can I get the width and height of a character in coordinates usr

? I found something in R-help , but that doesn't seem to make it completely clear. I assumed that

plot(NULL, xlim=c(-1,1), ylim=c(-1,1))
h <- par()$cxy
rect(-h[1]/2, -h[2]/2, h[1]/2, h[2]/2)
text(0,0,"M")

      

enter image description here

would be the answer, but the rectangle is a little too big. Also, I want the size to account for different values ​​as well cex

. Thank you for your time!

+3


source to share


1 answer


I finally found the answer in the doc par

:

hu

RO; default character size (width, height) in custom unit coordinate. par ("cxy") is par ("cin") / par ("pin") scaled for custom coordinates. Note that c (strwidth (ch), strheight (ch)) for a given string ch is usually much more accurate.

Using strwidth

and strheight

instead par()$cxy

gives much better results.



plot(NULL, xlim=c(-1,1), ylim=c(-1,1))
h <- c(strwidth("M"), strheight("M"))
rect(-h[1]/2, -h[2]/2, h[1]/2, h[2]/2)
text(0,0,"M")

      

enter image description here

+1


source







All Articles