[R] How do I plot a line followed by two forecast points?

Jorgy Porgee jorgy.porgee at gmail.com
Fri Aug 7 17:17:52 CEST 2009


Good day all,

I'm trying to plot a continuous line plot, which is followed by two
forecast points eg. one forecast point is 12 months out, and another
24 months out from the last date of the line plot.

In my attempts so far, the second plot (the forecast points) is scaled
against a new axis scale, thus the two plots are not directly
comparable (I need the forecast points to be scaled according to the
existing y axis).

An example is pasted below. Any ideas on how to achieve this would be
much appreciated.

Thanking you in advance,

George.

# Sample dates
>xValues = seq.Date(as.Date("1990-01-31"),to=as.Date("1992-12-31"),by="month");

# Sample y value
> yValues<-NULL;
> yValues[1:length(xValues)]=seq(0.1,length=length(xValues))

# Plot the series as a line
> plot(xValues,yValues,type="l");

# Sample forecast dates that start from xValue's data point
> fcastDates=seq.Date(from=as.Date(xValues[length(xValues)]),length=12,by="month");
> fcastDates
 [1] "1992-12-31" "1993-01-31" "1993-03-03" "1993-03-31" "1993-05-01"
"1993-05-31"
 [7] "1993-07-01" "1993-07-31" "1993-08-31" "1993-10-01" "1993-10-31"
"1993-12-01"

# Sample forecast (we only want the forecast point to be displayed)

> fcast<-NULL; fcast[1:length(fcastDates)]="NA"; fcast[length(fcast)]<-20;
> fcast
 [1] "NA" "NA" "NA" "NA" "NA" "NA" "NA" "NA" "NA" "NA" "NA" "20"

# Add the forecast plot to the original plot
> par(new=TRUE)
> plot(fcastDates,fcast,yaxt="n",xaxt="n",col="red")
Warning message:
In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion

# The second forecast

> fcastDates2=seq.Date(from=as.Date(fcastDates[length(fcastDates)]),length=12,by="month");
> fcastDates2
 [1] "1993-12-01" "1994-01-01" "1994-02-01" "1994-03-01" "1994-04-01"
"1994-05-01"
 [7] "1994-06-01" "1994-07-01" "1994-08-01" "1994-09-01" "1994-10-01"
"1994-11-01"
> fcast2<-NULL; fcast2[1:length(fcastDates2)]="NA"; fcast2[length(fcast2)]<-15;
> par(new=TRUE);plot(fcastDates2,fcast2,yaxt="n",xaxt="n",col="blue")
Warning message:
In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion




More information about the R-help mailing list