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) |
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 |
plot(my_x, my_y) # Plotting without lines
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 |
plot(my_x, my_y) # Creating plot without any lines abline(v = 1, col = "green") # Adding vertical line with 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 |
plot(my_x, my_y) # Creating plot without any lines abline(h = 1, col = "green") # Adding horizontal line with 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 |
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
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 |
plot(my_x, my_y) # Creating plot without lines abline(a = 0, 0.75, col = "green") # Adding line with intercept & slope
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 |
plot(my_x, my_y) # Creating plot without lines abline(reg = lm(my_y ~ my_x), col = "green") # Adding regression line to plot