Plotting using ggplot2

First, we need to install the library using following command

install.packages("ggplot2")
  • ggplot is a popular data visualization package in R programming.

Then we can use it as usual by importing the library into script.

R

# Importing library
library(ggplot2)
 
# Reading Dataset
SalaryData <- read.csv("placement.csv")
 
# Preparing model
model <- lm(package ~ cgpa, data = SalaryData)
 
 
png(file = "placement_stats.png")
 
ggplot(SalaryData, aes(x = cgpa, y = package)) +
  geom_point() + # For displaying points
  geom_smooth(method = "lm", se = FALSE) + # for displaying line
  labs(title = "CGPA and Package regression", x = "CGPA", y = "Y")

                    

Output:

It works in similar manner as previous one, both line of best fit and points are plotted on the graph

Without geom_point (plotting points absent)

R

# Importing library
library(ggplot2)
 
# Reading Dataset
SalaryData <- read.csv("placement.csv")
 
# Preparing model
model <- lm(package ~ cgpa, data = SalaryData)
 
 
png(file = "placement_stats.png")
 
ggplot(SalaryData, aes(x = cgpa, y = package)) +
  geom_smooth(method = "lm", se = FALSE) + # geom_points removed
  labs(title = "CGPA and Package regression", x = "CGPA", y = "Y")

                    

Output:

plotting points absent

When removing geom_point parameter the plotting points are not displayed on the graph while keeping everything same.

Without geom_smooth (line absent)

R

# Importing library
library(ggplot2)
 
# Reading Dataset
SalaryData <- read.csv("placement.csv")
 
# Preparing model
model <- lm(package ~ cgpa, data = SalaryData)
 
 
png(file = "placement_stats.png")
 
ggplot(SalaryData, aes(x = cgpa, y = package)) +
  geom_point()+
  labs(title = "CGPA and Package regression", x = "CGPA", y = "Y")

                    

Output:

line absent

When removing geom_smooth parameter the line of best fit is not displayed on the graph while keeping everything same.

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.

Similar Reads

What is Linear Regression?

Linear Regression is a supervised learning model, which computes and predicts the output implemented from the linear relationship the model established based on the data it gets fed with. The aim of this model is to find the linear equation that best fits the relationship between the independent variables (features) and the dependent variable (target). This was some basic insight into the concept of Linear Regression. With this, now we can dive into how to plot linear regression in R....

Why R?

R is a popular programming language that was developed solely for statistical analysis and data visualization. Some of the machine learning models we use are already pre-trained into R (no need for installation of external libraries) including Linear Model (or Regression). Also, this language comes with a huge community and one of the best tools and libraries for Machine Learning, which makes it the first choice for Data Science and Machine Learning enthusiasts....

Plotting Linear Regression in R

The dataset we are using for this is: placement.csv...

Plotting using ggplot2

...

Exploring the Relationship Between Hours Studied and Test Scores

...

Contact Us