[R] mtext coordinates
Jim Lemon
jim at bitwrit.com.au
Wed Aug 18 13:14:34 CEST 2010
On 08/18/2010 08:40 PM, David Hajage wrote:
> Hello,
>
> I am using mtext to write some text below a graph:
>
> plot(1)
> mtext("foo", side = 1, line = 2, at = seq(0.6, 1.6, 0.2))
>
> I would like to draw something near the "foo" texts, for example a segment.
> For this, I need to know the coordinates of the text ploted by mtext
> function.
>
> I spent a few hours to try different combination, using the option xpd =
> TRUE and trying to convert between graphics coordinate system using
> grconvertX and grconvertY... But I can't make it.
>
> Is there a way to know the coordinates of the text produce by mtext,
> particularly vertical coordinates? For example, how can you draw some
> segments to underline each foo in my example?
>
Hi David,
You can get the "X" coordinates of the beginning and end of the text by
using the strwidth function:
# this is inefficient, but easy to follow
begintext<-at-strwidth("foo")*par("adj")
endtext<-at+strwidth("foo")*(1-par("adj"))
the strheight function can be used to get the "Y" coordinates:
bottomtext<-par("usr")[1]-(line+0.5)*strheight("Ug")
You will probably have to fiddle with the constant (0.5) to get the line
in the correct place on a particular device, then:
par(xpd=TRUE)
segments(begintext,bottomtext,endtext,bottomtext)
par(xpd=FALSE)
Jim
More information about the R-help
mailing list