Checkout this resource: adding regression line per group with ggplot2
In general, you can use ggplot2, geom_point, and geom_line/geom_smooth to plot multiple datasets and lines in one graph. Without knowing your data, here's some steps I would take
- add a column specifying if the data is train vs. test:
train$source <- "train"
test$source <- "test"
- combine the datasets:
data <- (rbind(train, test))
- Use ggplot, geom_point(), and geom_smooth()/geom_line()
ggplot(data, aes(x=yourxvar, y=Vol, color=factor(source))) +
geom_point() + geom_smooth(method="lm")
You'll have to fill in a bit with your exact code and data. I would assume train and test are dataframes with your observations, but it seems like you're treating them like lines? Do you only want to chart the prediction line for test, or train as well? I would name it something more informative than prev either way.