0
votes

I typically use mtext to add labels to figures. I often wish to plot labels in the upper left corner of a figure. This is easy using par('usr')[1], except that the text is centered instead of right-oriented as I want it. When using 'text,' I would use pos=4, but there does not seem to be an equivalent of this for mtext. Example:

plot(1)
text(par('usr')[1], 1.5, "test", xpd=TRUE, pos=4)
mtext('test', 3, at=par('usr')[1])

I would like to avoid simply adding values to at= or using adj=, since I often am plotting multiple panels with different axes.

1

1 Answers

0
votes

Use adj= in base plots:

plot(1)
usr <- par("usr")
text(usr[1], usr[3], adj = c(-0.1, -0.1), "lower-left")
text(usr[2], usr[3], adj = c( 1.1, -0.1), "lower-right")
text(usr[1], usr[4], adj = c(-0.1,  1.1), "upper-left")
text(usr[2], usr[4], adj = c( 1.1,  1.1), "upper-right")

text with adj

From ?text:

 adj: one or two values in [0, 1] which specify the x (and
      optionally y) adjustment ('justification') of the labels,
      with 0 for left/bottom, 1 for right/top, and 0.5 for
      centered.  On most devices values outside [0, 1] will also
      work.  See below.

I use -0.1 and 1.1 to inset a little from the margin line itself. The .1 component might need to be adjusted depending on the rest of the plot.