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

whizvast whizvast at gmail.com
Fri Aug 7 20:56:41 CEST 2009


Run this codes:

plot( 1:20, type='l')
lines( 0.5*1:20, col=2)


If you use par(new=T), this will call a new high-level graphic device, 
which is why the axes do not match. Alternatively, you can set ylim
and pass the arguments to two plot functions:

plot( rnorm(100), type='h', ylim=c(-3,3))
par(new=T, mar=c(3,4,3,3))
plot( 0.25*rnorm(100), type='l', col=2, ylim=c(-3,3), ylab='')
axis(4)





Jorgy Porgee wrote:
> 
> 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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://www.nabble.com/How-do-I-plot-a-line-followed-by-two-forecast-points--tp24866692p24870219.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list