Вы можете использовать abline
, чтобы нарисовать линию в соответствии с наклоном и пересечением скорректированной линейной модели, и использовать text
, чтобы добавить дополнительную информацию к графику (используя paste
, чтобы преобразовать всю информацию в строку ).
#Save summary of the linear model
sum_lm<-summary(lm(score1~PCTPOVALL_2018, data = df1))
#Get coefficients
coef_lm<-sum_lm$coefficients
#Plot
plot(df1$PCTPOVALL_2018,
df1$score1,
xlab="Pov",
ylab="Score",
main= "Score vs Pov")
#Set the abline as the coefficients obtained by your linear model
abline(a = coef_lm[1,1],
b = coef_lm[2,1],
col = "lightblue",
lwd = 2)
#Add text to the plot in the x and y position (you might need to adjust these values according to your data)
#and add as labels a string that pastes all the info you wish to include. \n is interpreted as a line break.
text(x = max(df1$PCTPOVALL_2018)-20,
y = min(df1$score1)+10,
labels = paste0("R^2 = ",
#Round r.squared to 2 decimals
round(sum_lm$r.squared,2),
"\np-value = ",
#Round p.value of slope to 2 decimals
round(coef_lm[2,4],2),
"\ny = ",
#Round slope coefficient to 2 decimals
round(coef_lm[2,1],1),
"x + ",
#Round intercept coefficient to 2 decimals
round(coef_lm[1,1],2)),
pos = 4)
Вот результирующий график с вашими данными.
![linear regression plot](https://i.stack.imgur.com/JFTZ3.png)