The **abline()** function in R can be used to add one or more straight lines to a plot in R.

This function uses the following syntax:

**abline(a=NULL, b=NULL, h=NULL, v=NULL, …)**

where:

**a, b:**single values that specify the intercept and slope of the line**h:**the y-value for the horizontal line**v:**the x-value for the vertical line

The following examples show how to use this function in practice.

**How to Add Horizontal Lines**

The basic code to add a horizontal line to a plot in R is: **abline(h = some value)**

Suppose we have the following scatterplot that displays the values for *x *and *y *in a dataset:

#define dataset data <- data.frame(x = c(1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11), y = c(13, 14, 17, 12, 23, 24, 25, 25, 24, 28, 32, 33, 35, 40, 41)) #plotxandyvalues in dataset plot(data$x, data$y, pch = 16)

To add a horizontal line at the value y = 20, we can use the following code:

abline(h = 20, col = 'coral2', lwd = 2)

The following code illustrates how to add a horizontal solid line at the mean value of *y *along with two horizontal dashed lines at one standard deviation above and below the mean value:

#create scatterplot forxandyplot(data$x, data$y, pch = 16) #create horizontal line at mean value ofyabline(h = mean(data$y), lwd = 2) #create horizontal lines at one standard deviation above and below the mean value abline(h = mean(data$y) + sd(data$y), col = 'steelblue', lwd = 3, lty = 2) abline(h = mean(data$y) - sd(data$y), col = 'steelblue', lwd = 3, lty = 2)

**How to Add Vertical Lines**

The basic code to add a vertical line to a plot in R is: **abline(v = some value)**

The following code illustrates how to add a vertical line at the mean value on a histogram:

#make this example reproducible set.seed(0) #create dataset with 1000 random values normally distributed with mean = 10, sd = 2 data <- rnorm(1000, mean = 10, sd = 2) #create histogram of data values hist(data, col = 'steelblue') #draw a vertical dashed line at the mean value abline(v = mean(data), lwd = 3, lty = 2)

**How to Add Regression Lines **

The basic code to add a simple linear regression line to a plot in R is: **abline(model)**

The following code illustrates how to add a fitted linear regression line to a scatterplot:

#define dataset data <- data.frame(x = c(1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11), y = c(13, 14, 17, 12, 23, 24, 25, 25, 24, 28, 32, 33, 35, 40, 41)) #create scatterplot ofxandyvalues plot(data$x, data$y, pch = 16) #fit a linear regression model to the data reg_model <- lm(y ~ x, data = data) #add the fitted regression line to the scatterplot abline(reg_model, col="steelblue")

Note that we simply need a value for the intercept and the slope to fit a simple linear regression line to the data using the abline() function.

Thus, another way of using **abline()** to add a regression line is to explicitly specify the intercept and slope coefficients of the regression model:

#define dataset data <- data.frame(x = c(1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11), y = c(13, 14, 17, 12, 23, 24, 25, 25, 24, 28, 32, 33, 35, 40, 41)) #create scatterplot ofxandyvalues plot(data$x, data$y, pch = 16) #fit a linear regression model to the data reg_model <- lm(y ~ x, data = data) #define intercept and slope values a <- coefficients(reg_model)[1] #intercept b <- coefficients(reg_model)[2] #slope #add the fitted regression line to the scatterplot abline(a=a, b=b, col="steelblue")

Notice that this produces the same line as before.

You can find more R tutorials on this page.