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, 2020 in Data Analytics by anonymous
• 150 points
2,934 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, 2020 by MD
• 95,440 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 14, 2019 in Data Analytics by anonymous
• 33,030 points
2,963 views
0 votes
2 answers

"Train" and "Test" sets in Data Science

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

answered Aug 3, 2018 in Data Analytics by Abhi
• 3,720 points
986 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
4,709 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,790 points
1,660 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,700 points
684 views
+1 vote
1 answer

R query and Data Science

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

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
584 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,660 points
1,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 14, 2018 in Data Analytics by Sahiti
• 6,370 points
1,846 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, 2020 in Data Analytics by MD
• 95,440 points
1,282 views
0 votes
1 answer

How do you have 2 different colors in a bar graph, ggplot in where one color for the negatives and positives

Hi@Andris, Once you have the data, you can make the ...READ MORE

answered Oct 5, 2020 in Data Analytics by MD
• 95,440 points
763 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP