Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
Add legend for multiple lines in R using ggplot2
Next article icon

Multiple linear regression using ggplot2 in R

Last Updated : 24 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

A regression line is basically used in statistical models which help to estimate the relationship between a dependent variable and at least one independent variable. There are two types of regression lines :

  • Single Regression Line.
  • Multiple Regression Lines.

In this article, we are going to discuss how to plot multiple regression lines in R programming language using ggplot2 scatter plot.

Dataset Used: Here we are using a built-in data frame "Orange" which consists of details about the growth of five different types of orange trees. The data frame has 35 rows and 3 columns. The columns in this data frame are :

  • Tree: The ordering of trees on which experiment is made on the basis of increasing diameter values of the orange.
  • Age: The age of the trees since when they were planted.
  • Circumference: The circumference of the orange.

We first create a scatter plot. We will use the function geom_point( ) to plot the scatter plot which comes under the ggplot2 library. 

Syntax:

geom_point( mapping=NULL, data=NULL, stat=identity, position="identity")

Basically, we are doing a comparative analysis of the circumference vs age of the oranges. The function used is geom_smooth( ) to plot a smooth line or regression line. 

Syntax: geom_smooth(method="auto",se=FALSE,fullrange=TRUE,level=0.95)

Parameter :

  • method : The smoothing method is assigned using the keyword loess, lm, glm etc
  • lm : linear model, loess : default for smooth lines during small data set observations.
  • formula : You can also use formulas for smooth lines. For example : y~poly(x,4) which will plot a smooth line of degree 4. Higher the degree more bends the smooth line will have.
  • se : It takes logical values either "TRUE" or "FALSE".
  • fullrange : It takes logical value either "TRUE" or "FALSE".
  • level : By default level is 0.95 for the confidence interval.

Let us first draw a simple single-line regression and then increase the complexity to multiple lines.

Example:

R
# Scatter Plot library(ggplot2)  ggplt <- ggplot(Orange,aes(x=circumference,y=age))+          geom_point()+          theme_classic()  ggplt  # Plotting a single Regression Line ggplt+geom_smooth(method=lm,se=FALSE,fullrange=TRUE) 

Output:

This is a single smooth line or popularly known as a regression line. Here, the points are combined and are not segregated on the basis of any groups.

Multiple linear regression will deal with the same parameter, but each line will represent a different group. So, if we want to plot the points on the basis of the group they belong to, we need multiple regression lines. Each regression line will be associated with a group. 

Basic Formula for Multiple Regression Lines :

The syntax in R to calculate the coefficients and other parameters related to multiple regression lines is :

var <- lm(formula, data = data_set_name)

summary(var)

lm : linear model 

var : variable name 

To compute multiple regression lines on the same graph set the attribute on basis of which groups should be formed to shape parameter.

Syntax:

shape = attribute    

A single regression line is associated with a single group which can be seen in the legends of the plot. Now, to assign different colors to every regression lines write the command :

color = attribute

Example:

R
library(ggplot2)  ggplt <- ggplot(Orange,aes(x=circumference,y=age,shape=Tree))+          geom_point()+          theme_classic()  ggplt  # Plotting multiple Regression Lines ggplt+geom_smooth(method=lm,se=FALSE,fullrange=TRUE,                   aes(color=Tree)) 

Output:


Next Article
Add legend for multiple lines in R using ggplot2
author
rishabhchakrabortygfg
Improve
Article Tags :
  • R Language
  • R Machine-Learning
  • R-ggplot

Similar Reads

  • Add Regression Line to ggplot2 Plot in R
    Regression models a target prediction value based on independent variables. It is mostly used for finding out the relationship between variables and forecasting. Different regression models differ based on – the kind of relationship between dependent and independent variables, they are considering a
    4 min read
  • Line Plot using ggplot2 in R
    In a line graph, we have the horizontal axis value through which the line will be ordered and connected using the vertical axis values. We are going to use the R package ggplot2 which has several layers in it.  First, you need to install the ggplot2 package if it is not previously installed in R Stu
    6 min read
  • Add legend for multiple lines in R using ggplot2
    In this article, we are going to see how to add legends for multiple line plots in R Programming Language using ggplot2. First, you need to install the ggplot2 package if it is not previously installed in R Studio. The functions used to create the line plots are : geom_line( ) : To plot the line and
    3 min read
  • Multiple Line Plots or Time Series Plots with ggplot2 in R
    In this article, we will discuss how to plot Multiple Line Plots or Time Series Plots with the ggplot2 package in the R Programming Language. We can create a line plot using the geom_line() function of the ggplot2 package. Syntax: ggplot( df, aes( x, y ) ) + geom_line() where,  df: determines the da
    2 min read
  • How to Plot the Linear Regression in R
    In this article, we are going to learn to plot linear regression in R. But, to plot Linear regression, we first need to understand what exactly is linear regression. What is Linear Regression?Linear Regression is a supervised learning model, which computes and predicts the output implemented from th
    8 min read
  • How to create a plot using ggplot2 with Multiple Lines in R ?
    In this article, we will discuss how to create a plot using ggplot2 with multiple lines in the R programming language. Method 1: Using geom_line() function In this approach to create a ggplot with multiple lines, the user need to first install and import the ggplot2 package in the R console and then
    3 min read
  • How to Plot a Smooth Line using ggplot2 in R ?
    In this article, we will learn how to plot a smooth line using ggplot2 in R Programming Language. We will be using the "USArrests" data set as a sample dataset for this article. Murder Assault UrbanPop Rape Alabama 13.2 236 58 21.2 Alaska 10.0 263 48 44.5 Arizona 8.1 294 80 31.0 Arkansas 8.8 190 50
    3 min read
  • Non-Linear Regression in R
    Non-Linear Regression is a statistical method that is used to model the relationship between a dependent variable and one of the independent variable(s). In non-linear regression, the relationship is modeled using a non-linear equation. This means that the model can capture more complex and non-line
    6 min read
  • Remove Axis Labels using ggplot2 in R
    In this article, we are going to see how to remove axis labels of the ggplot2 plot in the R programming language. We will use theme() function from ggplot2 package. In this approach to remove the ggplot2 plot labels, the user first has to import and load the ggplot2 package in the R console, which i
    2 min read
  • Linear Regression Assumptions and Diagnostics using R
    Linear regression is a statistical method used to model the relationship between a dependent variable and one or more independent variables. Before interpreting the results of a linear regression analysis in R, it's important to check and ensure that the assumptions of linear regression are met. Ass
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences