• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Analysis Factor

The Analysis Factor

Statistical Consulting, Resources, and Statistics Workshops for Researchers

  • Home
  • Our Programs
    • Membership
    • Online Workshops
    • Free Webinars
    • Consulting Services
  • About
    • Our Team
    • Our Core Values
    • Our Privacy Policy
    • Employment
    • Collaborate with Us
  • Statistical Resources
  • Contact
  • Blog
  • Login

Linear Models in R: Plotting Regression Lines

by guest contributer 7 Comments

by David Lillis, Ph.D.

Today let’s re-create two variables and see how to plot them and include a regression line. We take height to be a variable that describes the heights (in cm) of ten people. Copy and paste the following code to the R command line to create this variable.

height <- c(176, 154, 138, 196, 132, 176, 181, 169, 150, 175)

Now let’s take bodymass to be a variable that describes the masses (in kg) of the same ten people. Copy and paste the following code to the R command line to create the bodymass variable.

bodymass <- c(82, 49, 53, 112, 47, 69, 77, 71, 62, 78)

Both variables are now stored in the R workspace. To view them, enter:

height
 [1] 176 154 138 196 132 176 181 169 150 175
bodymass
 [1] 82 49 53 112 47 69 77 71 62 78

We can now create a simple plot of the two variables as follows:

plot(bodymass, height)

tn_image001

We can enhance this plot using various arguments within the plot() command. Copy and paste the following code into the R workspace:

plot(bodymass, height, pch = 16, cex = 1.3, col = "blue", main = "HEIGHT PLOTTED AGAINST BODY MASS", xlab = "BODY MASS (kg)", ylab = "HEIGHT (cm)")

tn_image002

 

In the above code, the syntax pch = 16 creates solid dots, while cex = 1.3 creates dots that are 1.3 times bigger than the default (where cex = 1). More about these commands later.

Now let’s perform a linear regression using lm() on the two variables by adding the following text at the command line:

lm(height ~ bodymass)
Call:
lm(formula = height ~ bodymass)
Coefficients:
(Intercept)     bodymass
    98.0054       0.9528

We see that the intercept is 98.0054 and the slope is 0.9528. By the way – lm stands for “linear model”.

Finally, we can add a best fit line (regression line) to our plot by adding the following text at the command line:

abline(98.0054, 0.9528)

Another line of syntax that will plot the regression line is:

abline(lm(height ~ bodymass))

tn_image003

In the next blog post, we will look again at regression.

See our full R Tutorial Series and other blog posts regarding R programming.


About the Author:
David Lillis has taught R to many researchers and statisticians. His company, Sigma Statistics and Research Limited, provides both on-line instruction and face-to-face workshops on R, and coding services in R. David holds a doctorate in applied statistics.

Bookmark and Share

Four Critical Steps in Building Linear Regression Models
While you’re worrying about which predictors to enter, you might be missing issues that have a big impact your analysis. This training will help you achieve more accurate results and a less-frustrating model building experience.

Tagged With: abline, lines, plots, plotting, R, Regression

Related Posts

  • Linear Models in R: Diagnosing Our Regression Model
  • Linear Models in R: Improving Our Regression Model
  • R is Not So Hard! A Tutorial, Part 22: Creating and Customizing Scatter Plots
  • R Graphics: Plotting in Color with qplot Part 2

Reader Interactions

Comments

  1. Anjali Krishnan says

    May 1, 2020 at 1:42 pm

    Hello,

    I’m reaching out on behalf of the University of California – Irvine’s Office of Access and Inclusion. We are currently developing a project-based data science course for high school students. We would like your consent to direct our instructors to your article on plotting regression lines in R.

    Thanks and best regards,
    Anjali Krishnan

    Reply
  2. LUIZ AUGUSTO RODRIGUES says

    April 12, 2020 at 6:31 pm

    I have an experiment to do de regression analisys, but i have some hibrids by many population. Then I have two categorical factors and one respost variable.
    Could you help this case. If you have any routine or script this analisys and can share with me , i would be very grateful.

    Luiz

    Reply
  3. Solomon says

    June 14, 2019 at 6:40 am

    Thanks a lot. this really helped.

    Reply
  4. Katarzyna says

    May 19, 2019 at 6:52 am

    Any idea how to plot the regression line from lm() results? I have more parameters than one x and thought it should be strightforward, but I cannot find the answer…

    Reply
    • theStig says

      June 21, 2019 at 8:17 am

      Seems you address a multiple regression problem (y = b1x1 + b2x2 + … + e). In this case, you obtain a regression-hyperplane rather than a regression line. For 2 predictors (x1 and x2) you could plot it, but not for more than 2.

      Reply
  5. Néstor Toledo says

    November 29, 2018 at 11:21 am

    Nice! Don’t you should log-transform the body mass in order to get a linear relationship instead of a power one?

    Reply
  6. rishvanth yokesh says

    March 18, 2018 at 7:53 am

    Bro, seriously it helped me a lot.
    thank u yaar

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Please note that, due to the large number of comments submitted, any questions on problems related to a personal study/project will not be answered. We suggest joining Statistically Speaking, where you have access to a private forum and more resources 24/7.

Primary Sidebar

This Month’s Statistically Speaking Live Training

  • Member Training: Analyzing Pre-Post Data

Upcoming Free Webinars

Poisson and Negative Binomial Regression Models for Count Data

Upcoming Workshops

  • Analyzing Count Data: Poisson, Negative Binomial, and Other Essential Models (Jul 2022)
  • Introduction to Generalized Linear Mixed Models (Jul 2022)

Copyright © 2008–2022 The Analysis Factor, LLC. All rights reserved.
877-272-8096   Contact Us

The Analysis Factor uses cookies to ensure that we give you the best experience of our website. If you continue we assume that you consent to receive cookies on all websites from The Analysis Factor.
Continue Privacy Policy
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT