I am trying to wrap text labels in a rectangle.
Here is a simple plot with labels:
x = mtcars$wt
y = mtcars$mpg
l = rownames(mtcars)
plot(x, y)
text(x, y, l, adj = .5) # i.e., the default
I can successfully use strwidth
and strheight
to accomplish the wrapping like so:
delx = strwidth(l, cex = par('cex'))
dely = strheight(l, cex = par('cex'))
rect(x - .5*delx, y - .5*dely, x + .5*delx, y + .5*dely)
Notice the "gaps" of whitespace on the left and right of each label. For now, this is fine, but it leads to issues when trying to account for adj
in the plot:
adj = c(0, .5)
plot(x, y)
text(x, y, l, adj = adj)
rect(
x - adj[1L]*delx, y - adj[2L]*dely,
x + (1-adj[1L])*delx, y + (1-adj[2L])*dely
)
The box has adjusted directionally well, but it appears to be an accurate transformation, I need to account further for whatever is creating the "buffer" width.
What is that? I haven't seen anything in ?par or ?strwidth yet.