Here is one of the options for the observed and predicted values โโin one graph as points. Itโs easier to get a regression line from the observed points, which I illustrate the second
Some dummy data first
set.seed(1) x <- runif(50) y <- 2.5 + (3 * x) + rnorm(50, mean = 2.5, sd = 2) dat <- data.frame(x = x, y = y)
Install our model
mod <- lm(y ~ x, data = dat)
Combine the output of the model and observe x in one object for plott
res <- stack(data.frame(Observed = dat$y, Predicted = fitted(mod))) res <- cbind(res, x = rep(dat$x, 2)) head(res)
Loading grids and graphics
require("lattice") xyplot(values ~ x, data = res, group = ind, auto.key = TRUE)
The resulting plot should look something like this:

To get only the regression line from the observed data, and the regression model is a simple straight line model according to the one I'm showing, then you can get around most of this and just build with
xyplot(y ~ x, data = dat, type = c("p","r"), col.line = "red")
(i.e. you donโt even have to fit the model or create new data to plot)
The resulting plot should look like this:

An alternative to the first example, which can be used with everything that gives coefficients for the regression line, is to write your own panel functions - not as scary as it seems
xyplot(y ~ x, data = dat, col.line = "red", panel = function(x, y, ...) { panel.xyplot(x, y, ...) panel.abline(coef = coef(mod), ...)
This gives the graph from Figure 2 above, but manually.
Assuming you did this with a caret , then
mod <- train(y ~ x, data = dat, method = "lm", trControl = trainControl(method = "cv")) xyplot(y ~ x, data = dat, col.line = "red", panel = function(x, y, ...) { panel.xyplot(x, y, ...) panel.abline(coef = coef(mod$finalModel), ...) ## using mod from caret } )
It will produce a graph in the same way as in Figure 2 above.