'How to draw lines by its degree wrt to the x-axis?

I want to draw a line 45 degrees with respect to the x-axis in R base graphics. How to calculate the slope of the line so that it is drawn 45 degrees with respect to the x-axis?

EDIT: The people answered the question can not get it right.

Let me rephase it. Please draw a 45 degree line on the following plot.

# You should not assume a particular device size.
# for example, you may use pdf(width=17, height=11)
# or some arbitrary width and height
# if you use a fixed slope,
# then the angle will not be constant when you change the device size
plot(1:10, (1:10)*10)

The 45 degree means what you see on the screen. slope of 1 is certainly not of 45 degree unless you scale your screen to a particular value.



Solution 1:[1]

Update II:

With a given point (x,y) and a given angle of 45° we could do it this way. Main point is to get the correct intercept:

# formula
# y=slope*x+intercept 

x =5
y =3
slope <- tan(45)
intercept = (slope*x - y)*-1


plot.new()
plot(-5:5, -5:5, type = "n")# setting up coord. system
abline(h=0, v=0)
points(5,3, pch = 22, cex=2, col="green", bg="red", lwd=2)
abline(a=intercept, b=slope, col="red")
#a, b : single values specifying the intercept and the slope of the line

enter image description here enter image description here

Update I: I am sorry but I don't know if it is clear for you that each line through the positive x axis with a degree of 45 has the slope 1!?

See here:

General approach: slope of line through (point x,y) with an angle of 45°: slope = tan(alpha) Inclination of alpha is the angle of line with positive x-axis.

For your example: slope = tan(45°) = 1 Take any point of x,y for example (x, y) = (5,3):

y - y0 = slope(x-x0)

hence: y - 3 = 1 (x-5) y = x-2

y = the intercept = 3 Now you have x, the intercept and the slope and you could plot:

plot.new()
plot(1, type="n", xlab="", ylab="", xlim=c(0, 10), ylim=c(0, 10))
abline(a=y, b=1)

#a, b : single values specifying the intercept and the slope of the line

You get: enter image description here

Maybe you want a line through the x axis then you have to adapt the intercept:

plot.new()
plot(1, type="n", xlab="", ylab="", xlim=c(0, 10), ylim=c(0, 10))
abline(a=-3, b=1)
#a, b : single values specifying the intercept and the slope of the line

You get: enter image description here

First answer:

You could modify the degree of 45° as you desire: Note angles are in radians for the tan function!

plot.new()
plot(x=0, y=0, xlim=c(1, 8), ylim=c(1, 8), pch=15, cex=3)
abline(a=0, b=tan(45*pi/180))

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1