logo

How to Draw Horizontal and Vertical Lines in R 📂R

How to Draw Horizontal and Vertical Lines in R

Example

1.png

1. abline(h=0)

Draws a horizontal line.

2. abline(v=0)

Draws a vertical line.

3. abline(0,3/4)

Draws a line with $y$-intercept $0$ and slope $3/4$.

The abline() function itself is named after $a,b$, the coefficients of $y=a+bx$. If you are using R for statistics, you will rarely need it except when drawing a regression line.

segments(4,0,4,3)

Draws a line segment from $(4,0)$ to $(4,3)$. It is useful when you want to neatly draw only the part you need.

win.graph(6,5)
plot(x=0,y=0,xlim=c(-1,5),ylim=c(-1,4),xlab="x",ylab="y")
points(4,3,col="red",pch=19)
 
#1
abline(h=0)
 
#2
abline(v=0)
 
#3
abline(0,3/4)
 
#4
segments(4,0,4,3)