I am plotting the interaction of the fixed effects in a mixed effects model based on a lmer() object. To do so, I predict new values based on my model. This works fine, except that, due to how I generate them, the predictions stretch out over the whole possible x-axis range. I could now restrict the predicted regression lines to the range of their respective grouping variable by defining new.dat based on a loop (change max and min values depending on the grouping variable "Variety") etc., but - is there a more elegant/ easier solution to plot this? Am I missing out on something (I'm relatively new to R)?
Data:
library(datasets)
data("Oats")
# manipulate data so it resembles more my actual data
Oats <- Oats %>%
filter((Variety == "Golden Rain" & nitro>=0.2) | (Variety == "Marvellous" & nitro <=0.4) | (Variety == "Victory" & nitro<=0.4 & nitro>=0.2)) #%>%
Model & plotting:
mod2 <- lmer(yield ~ nitro * Variety + (1| Variety), data=Oats)
new.dat <- data.frame(nitro=seq(min(Oats$nitro),max(Oats$nitro), length.out = 48), Variety= Oats$Variety)
new.dat$pred<-predict(mod2,newdata=new.dat,re.form=~0)
ggplot(data=Oats, aes(x=nitro, y=yield, col = Variety)) +
geom_point() +
geom_line(data=new.dat, aes(y=pred)) +
geom_point(data=new.dat, aes(y=pred))
Thanks a lot for every hint!

