How can I plot a line of best fit in R, and what are some examples of how to do so? 2

How can I plot a line of best fit in R, and what are some examples of how to do so?

To plot a line of best fit in R, you can use the “lm” function to create a linear regression model and then use the “abline” function to add the line to your plot. This will show the overall trend of the data and help identify any potential patterns or relationships.

Some examples of how to do this would be:

1. Suppose you have a dataset with two variables, “x” and “y”. To plot a line of best fit for these variables, you can use the following code:

lm_model

Plot Line of Best Fit in R (With Examples)


You can use one of the following methods to plot a line of best fit in R:

Method 1: Plot Line of Best Fit in Base R

#create scatter plot of x vs. y
plot(x, y)

#add line of best fit to scatter plot
abline(lm(y ~ x))

Method 2: Plot Line of Best Fit in ggplot2

library(ggplot2)

#create scatter plot with line of best fit
ggplot(df, aes(x=x, y=y)) +
    geom_point() +
    geom_smooth(method=lm, se=FALSE)

The following examples show how to use each method in practice.

Example 1: Plot Line of Best Fit in Base R

The following code shows how to plot a line of best fit for a simple linear regression model using base R:

#define data
x <- c(1, 2, 3, 4, 5, 6, 7, 8)
y <- c(2, 5, 6, 7, 9, 12, 16, 19)

#create scatter plot of x vs. y
plot(x, y)

#add line of best fit to scatter plot
abline(lm(y ~ x))

Feel free to modify the style of the points and the line as well:

#define data
x <- c(1, 2, 3, 4, 5, 6, 7, 8)
y <- c(2, 5, 6, 7, 9, 12, 16, 19)

#create scatter plot of x vs. y
plot(x, y, pch=16, col='red', cex=1.2)

#add line of best fit to scatter plot
abline(lm(y ~ x), col='blue' , lty='dashed')

We can also use the following code to quickly calculate the line of best fit:

#find regression model coefficients
summary(lm(y ~ x))$coefficients

              Estimate Std. Error   t value     Pr(>|t|)
(Intercept) -0.8928571  1.0047365 -0.888648 4.084029e-01
x            2.3095238  0.1989675 11.607544 2.461303e-05

The line of best fit turns out to be: y = -0.89 + 2.31x.

Example 2: Plot Line of Best Fit in ggplot2

library(ggplot2)

#define data
df <- data.frame(x=c(1, 2, 3, 4, 5, 6, 7, 8),                 y=c(2, 5, 6, 7, 9, 12, 16, 19))

#create scatter plot with line of best fit
ggplot(df, aes(x=x, y=y)) +
    geom_point() +
    geom_smooth(method=lm, se=FALSE)

Feel free to modify the aesthetics of the plot as well:

library(ggplot2)

#define data
df <- data.frame(x=c(1, 2, 3, 4, 5, 6, 7, 8),                 y=c(2, 5, 6, 7, 9, 12, 16, 19))

#create scatter plot with line of best fit
ggplot(df, aes(x=x, y=y)) +
    geom_point(col='red', size=2) +
    geom_smooth(method=lm, se=FALSE, col='purple', linetype='dashed') +
    theme_bw()

Additional Resources

Cite this article

stats writer (2024). How can I plot a line of best fit in R, and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-plot-a-line-of-best-fit-in-r-and-what-are-some-examples-of-how-to-do-so/

stats writer. "How can I plot a line of best fit in R, and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-plot-a-line-of-best-fit-in-r-and-what-are-some-examples-of-how-to-do-so/.

stats writer. "How can I plot a line of best fit in R, and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-plot-a-line-of-best-fit-in-r-and-what-are-some-examples-of-how-to-do-so/.

stats writer (2024) 'How can I plot a line of best fit in R, and what are some examples of how to do so?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-plot-a-line-of-best-fit-in-r-and-what-are-some-examples-of-how-to-do-so/.

[1] stats writer, "How can I plot a line of best fit in R, and what are some examples of how to do so?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I plot a line of best fit in R, and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top