How to Apply the abline() Function in R (5 Examples)

In this R programming tutorial you’ll learn how to draw lines to plots using the abline function.

Creating Example Data

set.seed(13579)                                 # Creating some random data
my_x <- rnorm(50)
my_y <- 0.75 * my_x + runif(50)
plot(my_x, my_y)                                # Plotting without lines

r graph figure 1 apply abline() function

Example 1: Drawing Vertical Line to Graphic

plot(my_x, my_y)                                # Creating plot without any lines
abline(v = 1, col = "green")                    # Adding vertical line with abline function

r graph figure 2 apply abline() function

Example 2: Drawing Horizontal Line to Graphic

plot(my_x, my_y)                                # Creating plot without any lines
abline(h = 1, col = "green")                    # Adding horizontal line with abline function

r graph figure 3 apply abline() function

Example 3: Modifying Line Type & Thickness

plot(my_x, my_y)                                # Creating plot without lines
abline(v = 1, col = "green",                    # Adding vertical line with abline function
       lty = "dotted",                          # Changing line type
       lwd = 3)                                 # Changing thickness

r graph figure 4 apply abline() function

Example 4: Drawing Line based on Intercept & Slope

plot(my_x, my_y)                                # Creating plot without lines
abline(a = 0, 0.75, col = "green")              # Adding line with intercept & slope

r graph figure 5 apply abline() function

Example 5: Drawing Regression Line to Graphic

plot(my_x, my_y)                                # Creating plot without lines
abline(reg = lm(my_y ~ my_x), col = "green")    # Adding regression line to plot

r graph figure 6 apply abline() function

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top