[R] Misunderstanding with lines (or elsewhere)

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Jul 11 16:34:11 CEST 2006

```On Tue, 2006-07-11 at 15:39 +0200, vincent at 7d4.com wrote:
> Prof Brian Ripley a écrit :
>
> > Think about the coordinate system you are using: if you don't set the
> > margins to zero you will see what it is.
> > I suggest you supply x and y to image() to set the coordinate system to
> > what you want it to be.
>
> If I have well understood, the right way to proceed
> must be :
>
> test = function()
> {
> m = matrix(0, 2, 2)
> par(new=T, fig = c(0,1,0,1), mai=c(0,0,0,0), mar=c(0,0,0,0), bty='n')
> x0 = c(0.25 , 0.75)
> y0 = x0
> image(x=x0, y=y0, z=m)
> lines(c(0,1), c(1/4,1/4))         # horizontal line
> }
>
> which seems indeed to work.
> Thanks.

You could swap the line:

lines(c(0,1), c(1/4, 1/4))

with:

abline(h = 0.25)

Does the same thing but is more readable IMHO.

Also, I get a warning with your code if no plot is open:

Warning message:
calling par(new=) with no plot

To stop this, drop the new = T (sic) in the call to par. Also, don't use
T when you mean TRUE.

So, in summary this seems to do the same thing as your function:

test = function()
{
m = matrix(0, 2, 2)
par(fig = c(0,1,0,1), mai=c(0,0,0,0), mar=c(0,0,0,0), bty='n')
x0 = c(0.25 , 0.75)
y0 = x0
image(x=x0, y=y0, z=m)
abline(h = 0.25)
}

HTH,

G
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                 [t] +44 (0)20 7679 0522
ECRC & ENSIS, UCL Geography,  [f] +44 (0)20 7679 0565
Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/cv/
London, UK. WC1E 6BT.         [w] http://www.ucl.ac.uk/~ucfagls/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

```