在 R 中使用 abline 绘制回归图
Plotting regression using abline in R
你好,我正在尝试使用 abline 在我的散点图中画一条线,我尝试了几种不同的方法,但我不太确定我做错了什么! (对 R 相当陌生)
编辑:我也试过的代码
plot(data$GRE.Score, data$Chance.of.Admit, main = "Regression Line plot",
xlab = "Chance of Admit", ylab = "GRE Score",
pch = 19, frame = FALSE)
abline(lm(GRE.Score ~ Chance.of.Admit, data = data), col = "red")
我希望这个小例子能为您指明方向:
# dummy data
df <- data.frame(x = 1:100, y = rpois(100, lambda = 4))
# plot
plot(df$x, df$y, main = "Main title",
xlab = "X axis title", ylab = "Y axis title",
pch = 19, frame = FALSE)
# linear regression line (can only be called AFTER the plot call)
abline(lm(y ~ x, data = df), col = "blue")
# vertical line that cuts X at 10
abline(v = 10, col = "red")
# horizontal line that cuts Y at 10
abline(h = 10, col = "green")
有了kaggle数据:
df <- read.csv("C:/.../Admission_Predict.csv")
# plot
plot(df$GRE.Score, df$Chance.of.Admit, main = "Main title",
xlab = "X axis title", ylab= "Y axis title",
pch = 19, frame = FALSE)
abline(lm(Chance.of.Admit ~ GRE.Score, data = df), col = "red")
你好,我正在尝试使用 abline 在我的散点图中画一条线,我尝试了几种不同的方法,但我不太确定我做错了什么! (对 R 相当陌生)
编辑:我也试过的代码
plot(data$GRE.Score, data$Chance.of.Admit, main = "Regression Line plot",
xlab = "Chance of Admit", ylab = "GRE Score",
pch = 19, frame = FALSE)
abline(lm(GRE.Score ~ Chance.of.Admit, data = data), col = "red")
我希望这个小例子能为您指明方向:
# dummy data
df <- data.frame(x = 1:100, y = rpois(100, lambda = 4))
# plot
plot(df$x, df$y, main = "Main title",
xlab = "X axis title", ylab = "Y axis title",
pch = 19, frame = FALSE)
# linear regression line (can only be called AFTER the plot call)
abline(lm(y ~ x, data = df), col = "blue")
# vertical line that cuts X at 10
abline(v = 10, col = "red")
# horizontal line that cuts Y at 10
abline(h = 10, col = "green")
有了kaggle数据:
df <- read.csv("C:/.../Admission_Predict.csv")
# plot
plot(df$GRE.Score, df$Chance.of.Admit, main = "Main title",
xlab = "X axis title", ylab= "Y axis title",
pch = 19, frame = FALSE)
abline(lm(Chance.of.Admit ~ GRE.Score, data = df), col = "red")