I am trying to make a bar plot using base R code along with a linear fit using abline, but it seems like I am not getting the right results when using abline. At least, when looking at the regression line, and comparing it with drawing some lines using the predicted model it is way off:
df <- data.frame(year = c(2018,2019,2020), PWI = c(64.7,71.3,75.2))
barplot(PWI~year, data = df, ylim = c(0,100))
text(x,y+2,labels=as.character(as.matrix(round(df,1))))
abline(lm(PWI~I(year-2018)), lty = "dashed", col = "red")
How do I get abline to align with the barplot?
For the record, I'm interested in a base R approach with a line behaving like abline. It can be done in ggplot by:
coeff <- coefficients(lm(PWI~year, data = df))
ggplot(df,aes(year,PWI)) +
geom_bar(stat = "identity") +
geom_abline(intercept = coeff[1], slope = coeff[2])

