Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extent of boundary of text in R plot [duplicate]

Tags:

r

Possible Duplicate:
finding the bounding box of plotted text

I've got a simple plot to which I've added some text to the plotting region; I'd like to put a border around this text. Is there a way to get the boundaries of the actual text so that I can use these for the plotting of the rectangle, or do I just need to use trial and error to make the rectangle look about right?!

At the moment, my code is:

plot(dep, eqt[,1], type="l")
text(12,200,"A notes about this plot!", font=2, cex=2)
rect(10,140,14,260)
like image 933
ChrisW Avatar asked Oct 15 '25 14:10

ChrisW


1 Answers

You can use strheight and strwidht to estimate that height and width of text.

Health warning: strheight and strwidth estimates text size in the current plot device. If you subsequently resize the plot, the R may resize the text automatically, but the rectangle will not resize. This is fine for interactive plots, but may cause problems when you save the plot to disk using png(); plot(...); dev.off()

x <- 1:300
y <- 1:300
plot(x, y, type="l")

txt <- "A note about this plot!"
rwidth <- strwidth(txt, font=2, cex=2)
rheight <- strheight(txt, font=2, cex=2)

tx <- 150
ty <- 100

text(tx, ty,txt, font=2, cex=2, col="blue", offset=1)

rect(tx-0.5*rwidth, ty-0.5*rheight, tx+0.5*rwidth, ty+0.5*rheight)

enter image description here

like image 108
Andrie Avatar answered Oct 18 '25 06:10

Andrie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!