-2

I want to plot the trainingset and the testing set in the same graph with in order to add the lines of prediction, all I can do is

 prev <- as.data.frame(predict(m,newdata=as.data.frame(test)))
plot(train,ylab="Vol",type="l",col="red")
lines(prev,type="l",col="blue")
lines(test,type="l",col="yellow")
  • Can you please provide your data as well? You can use function `dput()`. – Bloxx Nov 18 '21 at 13:18
  • I have the same issue but in R [link](https://stackoverflow.com/questions/63677752/how-to-plot-train-and-test-together-using-matplotlib/70021143#70021143) – CHAIMAE HAFID Nov 18 '21 at 13:58

1 Answers1

0

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

  1. add a column specifying if the data is train vs. test:
train$source <- "train"
test$source <- "test"
  1. combine the datasets:

data <- (rbind(train, test))

  1. 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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
tbrk
  • 173
  • 1
  • 8
  • Thanks, I'm a beginner in stackoverflow, but I have the same problem posed in this link but with R [link](https://stackoverflow.com/questions/63677752/how-to-plot-train-and-test-together-using-matplotlib/70021143#70021143) – CHAIMAE HAFID Nov 18 '21 at 14:44