# Fit a linear regression model
model <- lm(mpg ~ wt, data = mtcars)
# Create scatter plot
plot_ly(data = mtcars, x = ~wt, y = ~mpg,
type = 'scatter', mode = 'markers',
text = row.names(mtcars),
mode ='markers',
name = "Data Points") %>%
add_trace(x = ~wt, y = ~fitted(model),
type = 'scatter',
mode = 'lines',
line = list(color = 'blue', width = 2),
name = "Trendline")