r语言中用xyplot()画完图之后在图上叠加

大概问题是这样的
我在r中用xyplot画了一幅图
现在想在图像上加上两个变量的平均值线和曲线拟合
该怎么在原图上加呢

img

这里data换成你的data

y=horsepower
x=mpg

# 计算x和y的平均值
mean_x <- mean(x)
mean_y <- mean(y)

# 绘制xyplot
xyplot(y ~ x, data = data)

# 添加平均线
abline(v = mean_x, col = "red")  # x轴平均线
abline(h = mean_y, col = "blue") # y轴平均线

# 计算loess拟合曲线
fit <- loess(y ~ x, data = data)

# 绘制xyplot
xyplot(y ~ x, data = data)

# 添加loess拟合曲线
x_vals <- seq(min(x), max(x), length.out = 100) # 用于绘制拟合线的x值序列
lines(x_vals, predict(fit, newdata = data.frame(x = x_vals)), col = "green")