如何在 R 中创建带有回归线的散点图?
散点图使用点来表示两个不同数值变量的值。散点图用于观察变量之间的关系。线性回归是自变量和因变量之间关系的直线表示。在本文中,我们将讨论如何使用 R 及其库绘制带有线性回归的散点图。
散点图可用于显示所有可能的结果,在散点图上绘制的线性回归可用于概括共同特征或推导出结果的最大点。在这里,我们将首先讨论绘制散点图的方法,然后对其进行线性回归。
使用的数据集: Salary_Data.xls
在 R 中,用于绘制两个变量的散点图的函数是 plot()函数,它将返回散点图。
Syntax: plot(x, y, main, xlab, ylab, xlim, ylim, axes)
Parameters:-
- x- is the data set whose values are the horizontal coordinates.
- y- is the data set whose values are the vertical coordinates.
- main- is the tile of the graph.
- xlab- is the label on the horizontal axis.
- ylab- is the label on the vertical axis.
- xlim- is the limits of the values of x used for plotting.
- ylim- is the limits of the values of y used for plotting.
- axes- indicates whether both axes should be drawn on the plot.
Return:-
A 2-Dimension scatter plot.
程序:
R
library(readxl)
# import data
Salary_Data <- read_excel("Salary_Data.xls")
# plot scatter plot
plot(Salary_Data$YearsExperience,Salary_Data$Salary,
main='YearsExperience Vs Salary',
xlab='YearsExperience', ylab='Salary')
R
library(readxl)
# import data
Salary_Data <- read_excel("Salary_Data.xls")
# plot a scatter plot
plot(Salary_Data$YearsExperience,Salary_Data$Salary,
main='Regression for YearsExperience and Salary',
xlab='YearsExperience',ylab='Salary')
# plot a regression line
abline(lm(Salary~YearsExperience,data=Salary_Data),col='red')
输出:
回归线是描述响应变量 y(相关变量)如何随着解释变量 x(独立)变化而变化的直线。这用于预测给定 x 值的 y 值。
为了绘制回归线,我们需要两个函数:
- abline()函数用于在当前绘图中添加一条或多条直线
Syntax: abline(a=NULL, b=NULL, h=NULL, v=NULL, …)
Parameters:
a, b: It specifies the intercept and the slope of the line
h: specifies y-value for horizontal line(s)
v: specifies x-value(s) for vertical line(s)
Returns: a straight line in the plot
- lm()函数代表线性模型,”函数可用于创建简单的回归模型。
Syntax: lm(formula,data)
Parameters:
- the formula- is a symbol presenting the relation between x and y.
- data- is the vector on which the formula will be applied.
Returns:
The relationship line of x and y.
程序:
电阻
library(readxl)
# import data
Salary_Data <- read_excel("Salary_Data.xls")
# plot a scatter plot
plot(Salary_Data$YearsExperience,Salary_Data$Salary,
main='Regression for YearsExperience and Salary',
xlab='YearsExperience',ylab='Salary')
# plot a regression line
abline(lm(Salary~YearsExperience,data=Salary_Data),col='red')
输出: