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)
