# Fit a simple linear regression model
model <- lm(mpg ~ wt, data = mtcars)
# Get regression line data
regression_line <- data.frame(wt = seq(min(mtcars$wt), max(mtcars$wt),
length.out = 100))
regression_line$mpg <- predict(model, newdata = regression_line)
# Plot with regression line
regression_plot <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_line(data = regression_line, aes(x = wt, y = mpg),
color = "red") +
labs(x = "Weight (1000 lbs)", y = "Miles per Gallon") +
ggtitle("Regression Line of MPG vs. Weight")
# Print the plots
print(regression_plot)
