6.2 C
London
Thursday, December 19, 2024
HomeStatistics TutorialRHow to Use abline() in R to Add Straight Lines to Plots

How to Use abline() in R to Add Straight Lines to Plots

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 in a dataset:

#define dataset
data #plot x and y values in dataset
plot(data$x, data$y, pch = 16)

Scatterplot in R

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

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

Example of abline() in R

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

#create scatterplot for x and y
plot(data$x, data$y, pch = 16)

#create horizontal line at mean value of y
abline(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)

Horizontal lines in R using abline() function

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 #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)

Abline on a histogram in R

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 #create scatterplot of x and y values
plot(data$x, data$y, pch = 16)

#fit a linear regression model to the data
reg_model #add the fitted regression line to the scatterplot
abline(reg_model, col="steelblue")

Abline() of a regression line in R

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 #create scatterplot of x and y values
plot(data$x, data$y, pch = 16)

#fit a linear regression model to the data
reg_model #define intercept and slope values
a #intercept
b #slope

#add the fitted regression line to the scatterplot
abline(a=a, b=b, col="steelblue")

Abline() of a regression line in R with specific intercept and slope

Notice that this produces the same line as before.


You can find more R tutorials on this page.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories