[R] problem with abline and lines
(Ted Harding)
Ted.Harding at manchester.ac.uk
Mon Mar 22 12:43:35 CET 2010
On 22-Mar-10 11:13:39, Martin Ivanov wrote:
> Dear R users,
> I need to plot to perpendicular straight lines. However, although
> I set the coefficients so that the lines are perpendicular, they
> do not look to be so in the plot. Here is a minimal working example:
>
> plot(x=c(-1, 1), y=c(-1, 1)); abline(a=0, b=1/sqrt(2)); abline(a=0,
> b=-1/sqrt(2))
>
> Please tell me if the same problem is valid by you. I am running
> R-2.10.1 on Linux.
>
> Is there a way out of this?
>
> Regards,
> Martin
Hi Martin,
For one thing, your coefficients are wrong for orthogonality.
The condition for y = a1 + b1*x and y = a2 + b2*x to be perpendicular
to each other is that b1*b2 = -1. It will look much better with
plot(x=c(-1, 1), y=c(-1, 1));
abline(a=0, b=1/sqrt(2));
abline(a=0, b=-sqrt(2))
but probably will not look quite right, because the plot() command
does not necessarily ensure that the x and y axes are identically
scaled (in screen coordinates) when they appear on screen.
To ensure this as well, you need to set the aspect-ratio to be 1:
plot(x=c(-1, 1), y=c(-1, 1), asp=1);
abline(a=0, b=1/sqrt(2));
abline(a=0, b=-sqrt(2))
It can take a little while to discover the parameter "asp".
>From ?plot (which does not mention it) you will find a suggestion
to look at ?plot.default for further details, where you can read:
plot(x, y = NULL, type = "p", xlim = NULL, ylim = NULL,
log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
ann = par("ann"), axes = TRUE, frame.plot = axes,
panel.first = NULL, panel.last = NULL, asp = NA, ...)
and:
asp: the y/x aspect ratio, see 'plot.window'. And then ?plot.window
tells you all about it:
asp: If 'asp' is a finite positive value then the window is set up
so that one data unit in the x direction is equal in length
to 'asp' * one data unit in the y direction.
This even applies if you distort a previous plot window by dragging
one edge of it. For example, kill any existing plot window, and then
execute the above to create a new one. Then, say, drag the left-hand
edge so as to halve the width without changing the height. Then
repeat the plot commands. You will find that in the new plot the
two lines are again perpendicular, the shape of the window has
not changed, but the y-axis has now been extended numerically so that
the aspect ratio (as defined above) is still 1.0!
Hoping this helps!
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 22-Mar-10 Time: 11:43:32
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list