[R] Add coordinates at specific points...

Duncan Murdoch murdoch.duncan at gmail.com
Thu Jul 14 02:26:20 CEST 2011

On 13/07/2011 7:49 PM, David Winsemius wrote:
> On Jul 13, 2011, at 7:42 PM, JIA Pei wrote:
>> Hi, Thanks David Winsemius:
>> "mtext" works !! However, in R plot, "mtext" will automatically
>> overlap/overwrite the existing coordinates, which makes the
>> coordinates a messy.
>> Refer to http://www.visionopen.com/Rplot.png , which is produced by
>> only 3 lines.
>> dev.new(width = 640, height = 480)
>> plot(sin, -pi, 2*pi)
>> mtext(text="2.1000", side = 1, line = 1,  at = 2.1)
>> In order to avoid the coordinates overlap, can I change the position
>> of "2.1000" from outside the box to inside the box? If possible, how
>> to?
>> I tried “outer = FALSE,” nothing special happened ever !!
> You should use text() rather than mtext() if you are going to plot
> within the "user area".

I think that advice is not quite right (or it is descriptive, not 
prescriptive).  The main difference between text and mtext is what 
coordinate systems are used to specify locations.  If you want to 
specify something relative to the frame of your graph, mtext is the 
right tool.  Usually such things go outside the frame, but if you want 
something just inside, it still makes sense to use mtext.

For example, to put 2.1000 just inside the box on side 1, use

mtext(text="2.1000", side=1, line = -1, at = 2.1)

This works regardless of the scaling of the y axis, e.g. the same code 
would work if the original plot had been

curve(sin(x)*100, -pi, 2*pi)

Duncan Murdoch

More information about the R-help mailing list