R plot for train and test data on same graph

+3 votes
I have data which i divided into training and test. I want to forecast my test data using my model obtained from the training on the same graph and compare them. I am doing this in R. I want the original data (cocoa) in blue and training data with it 3 period forecast in red. I used the following codes. please help

cocoa<-ts(cofst$Production,start = c(1948),end = c(2019),frequency = 1)

train<-window(cocoa,start = c(1948), end= c(2016), frequency = 1)
test<-window(cocoa,start = c(2017), end= c(2019), frequency = 1)
train
arimaModel_1=arima(train,order=c(1,1,0))
forecast1=forecast(arimaModel_1, h=3)

plot(cocoa,col="blue")
lines(fitted(forecast1),col="red")
Jul 21 in Data Analytics by anonymous
• 150 points
108 views

1 answer to this question.

0 votes

Hi,

You can use the subplot function. It allows you to plot different graphs within a plot. You can see the below-given example.

fig1 <- plot_ly(economics, x = ~date, y = ~unemploy) 
fig1 <- fig1 %>% add_lines(name = ~"unemploy") 
fig2 <- plot_ly(economics, x = ~date, y = ~uempmed) 
fig2 <- fig2 %>% add_lines(name = ~"uempmed") 
fig <- subplot(fig1, fig2)
answered Jul 27 by MD
• 57,800 points

Related Questions In Data Analytics

0 votes
1 answer

Create train and test data from dataset in R

Hi, Use sample or sample.split function to create ...READ MORE

answered Oct 13, 2019 in Data Analytics by anonymous
• 32,490 points
821 views
0 votes
2 answers

"Train" and "Test" sets in Data Science

Normally to perform supervised learning you need ...READ MORE

answered Aug 2, 2018 in Data Analytics by Abhi
• 3,680 points
168 views
0 votes
1 answer

Plot two variables as lines on the same graph using ggplot

If you have small number of variables, ...READ MORE

answered Apr 17, 2018 in Data Analytics by kappa3010
• 2,090 points
2,191 views
0 votes
1 answer

How can I print string and variable contents on the same line using R?

There are two options for doing so.  You ...READ MORE

answered May 9, 2018 in Data Analytics by zombie
• 3,750 points
110 views
+1 vote
1 answer

R programming: How to subset data and plot graphs in R?

You can create a grouping variable depending ...READ MORE

answered Feb 18, 2019 in Data Analytics by Tyrion anex
• 8,550 points
115 views
+1 vote
1 answer

R query and Data Science

Dear Deepika, Hope you are doing great. You can ...READ MORE

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
93 views
0 votes
1 answer

Plotting multiple graphs on the same page in R

If you want to plot 4 graphs ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,580 points
197 views
0 votes
1 answer

Save a plot as image on the disk using R

Consider for both the situations: 1. Image will ...READ MORE

answered Apr 13, 2018 in Data Analytics by Sahiti
• 6,320 points
570 views
0 votes
1 answer

R program: Filter and re-arrange data based on multiple conditions

Hi, You need to create your own customized ...READ MORE

answered Jul 27 in Data Analytics by MD
• 57,800 points
197 views