library(ggplot2)ggplot(data = df, mapping =aes(x = x, y = y) ) +geom_point(color ="grey40", alpha =0.6) +stat_smooth(method ="lm", formula = y ~ x, se =FALSE, color ="blue", linetype ="dashed") +stat_smooth(method ="lm", formula = y ~ x +I(x^2), se =FALSE, color ="red", size =1.2) +stat_smooth(method ="lm", formula = y ~ x +I(x^2) +I(x^3) +I(x^4) +I(x^5) +I(x^6) +I(x^7), se =FALSE, color ="pink", size =1.2) +labs(title ="Straight Line vs Quadratic Fit vs Polynomial Fit" ,subtitle ="Blue dashed = Linear model, Red = Quadratic model",x ="x", y ="y")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.