4.5 C
London
Thursday, December 19, 2024
HomeStatistics TutorialRHow to Plot lm() Results in R

How to Plot lm() Results in R

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...

You can use the following methods to plot the results of the lm() function in R:

Method 1: Plot lm() Results in Base R

#create scatterplot
plot(y ~ x, data=data)

#add fitted regression line to scatterplot
abline(fit)

Method 2: Plot lm() Results in ggplot2

library(ggplot2)

#create scatterplot with fitted regression line
ggplot(data, aes(x = x, y = y)) + 
  geom_point() +
  stat_smooth(method = "lm")

The following examples shows how to use each method in practice with the built-in mtcars dataset in R.

Example 1: Plot lm() Results in Base R

The following code shows how to plot the results of the lm() function in base R:

#fit regression model
fit #create scatterplot
plot(mpg ~ wt, data=mtcars)

#add fitted regression line to scatterplot
abline(fit)

The points in the plot represent the raw data values and the straight diagonal line represents the fitted regression line.

Example 2: Plot lm() Results in ggplot2

The following code shows how to plot the results of the lm() function using the ggplot2 data visualization package:

library(ggplot2)

#fit regression model
fit #create scatterplot with fitted regression line
ggplot(mtcars, aes(x = x, y = y)) +
  geom_point() +
  stat_smooth(method = "lm")

The blue line represents the fitted regression line and the grey bands represent the 95% confidence interval limits.

To remove the confidence interval limits, simply use se=FALSE in the stat_smooth() argument:

library(ggplot2) 

#fit regression model
fit #create scatterplot with fitted regression line
ggplot(mtcars, aes(x = x, y = y)) +
  geom_point() +
  stat_smooth(method = "lm", se=FALSE)

plot lm() results in R

You can also add the fitted regression equation inside the chart by using the stat_regline_equation() function from the ggpubr package:

library(ggplot2)
library(ggpubr)

#fit regression model
fit #create scatterplot with fitted regression line
ggplot(mtcars, aes(x = x, y = y)) +
  geom_point() +
  stat_smooth(method = "lm", se=FALSE) +
  stat_regline_equation(label.x.npc = "center")

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Perform Simple Linear Regression in R
How to Interpret Regression Output in R
The Difference Between glm and lm in R

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