GGPLOT:
ggplot(data = df, aes(x = Distance.from.Residence.to.Work, y = Absenteeism.time.in.hours)) + geom_point(alpha = 0.5, aes(color = “orange”, size = 5)) + geom_smooth(method = “lm”, color = “blue”, se = FALSE)
Plotly:
linear_model = lm(Distance.from.Residence.to.Work ~ Absenteeism.time.in.hours, data = df)
slope = coef(linear_model)[2]
intercept = coef(linear_model)[1]
scatter_plot = plot_ly(data = df, x = df\(Distance.from.Residence.to.Work, y = df\)Absenteeism.time.in.hours, type = “scatter”, mode = “markers”, name = “Data Points”)
scatter_plot = add_lines(scatter_plot, x = df$Distance.from.Residence.to.Work, y = predict(linear_model), type = “scatter”, mode = “lines”, name = “Linear Regression Line”)
scatter_plot = layout(scatter_plot, title = “Scatter Plot with Linear Regression Line”, xaxis = list(title = “Distance from Residence to Work”), yaxis = list(title = “Absenteeism Time in Hours”))
scatter_plot