[R] Specifying relative position of text in a plot
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Mar 12 20:01:47 CET 2008
On Wed, 12 Mar 2008, Nordlund, Dan (DSHS/RDA) wrote:
>> -----Original Message-----
>> From: r-help-bounces at r-project.org
>> [mailto:r-help-bounces at r-project.org] On Behalf Of Alberto Monteiro
>> Sent: Wednesday, March 12, 2008 10:28 AM
>> To: Tom La Bone; r-help at r-project.org
>> Subject: Re: [R] Specifying relative position of text in a plot
>>
>>
>> Tom La Bone asked:
>>>
>>> What is the simplest way to specify the location of text in a
>>> scatter plot
>>> (created using the plot function) in relative terms rather than
>>> specific x-y coordinates? For example, rather than putting text at
>>> (300,49) on a plot, how do I put it 1/10 of the way over from the y
>>> axis and 1/2 of the way up from the x axis? Thanks.
>>>
>> See the help of par:
>> ?par
>>
>> Namely:
>>
>> plot(rnorm(100), rnorm(100))
>> pu <- par()$usr
>> x <- pu[1] * 0.5 + pu[2] * 0.5
>> y <- pu[3] * 0.1 + pu[4] * 0.9
>> text(x, y, "the quick brown fox jumps over a lazy dog")
>>
>> Alberto Monteiro
>
> This is a useful example of placing text, but I interpreted the request
> as aking for the text to be placed at the left side of the graph (1/10
> of the x range) and centered vertically (with respect to the y scale).
> So, then I would calculate x and y as
>
> x <- pu[1] * 0.9 + pu[2] * 0.1
> y <- pu[3] * 0.5 + pu[4] * 0.5
Yes, and there has been another wrong answer earlier. Note that my
suggestion of
plot(rnorm(100), rnorm(100))
op <- par(usr=c(0,1,0,1))
text(1/10, 1/2, "some text")
par(op)
is a lot less error-prone (and it also works with log scales).
> Also, text() centers the supplied text at the x,y coordinates. So, if
> Tom wants the text "begin" at that point, he will need to adjust the x
> coordinate for the length of the string being printed.
Better, use the adj= argument to text.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list