lattice xyplot - 向部分数据添加回归线

lattice xyplot - add a regression line to part of the data

我对 R 中 lattice 库中的 xyplot 有疑问。

这里是数据的例子:

set.seed(4)
mydata <- data.frame(x.data = rnorm(50),
                 y.data = rnorm(50),
                 type = rep(c("A","B"), 50))
head(mydata)

mod <- lm(x.data ~ y.data*type, data= mydata)
summary(mod)

xyplot(y.data + fitted(mod) ~ x.data, groups= type, data= mydata, auto.key=F)

如何才能添加回归线仅用于键入 A 数据并且仅在图中的拟合 (mod) 部分。是否有可能只有从最小值到最大值的回归线?

我希望结果看起来像这样:

感谢您的帮助。非常感谢。

library(lattice)
library(latticeExtra)

set.seed(4)
mydata <- data.frame(x.data = rnorm(50),
                     y.data = rnorm(50),
                     type = rep(c("A","B"), 50))
head(mydata)

mod <- lm(x.data ~ y.data*type, data= mydata)
p1 <- predict(mod, newdata = data.frame(y.data = range(mydata$y.data), type = "A"))

xyplot(y.data + fitted(mod) ~ x.data, groups= type, data= mydata, auto.key=F) +
  layer(panel.lines(range(mydata$x.data), p1, col = "black", lty = 1), rows = 1, columns = 2)