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

Clint Bowman clint at ecy.wa.gov
Fri Aug 7 22:33:24 CEST 2009


Because you know a priori the dates associated with your forecast points
you could use the col= in the plot function to change colors for the last
two points (may require some mathmatical gymnastics to specify the colors
desired--I've set up a vector and created an index from either the x or y
values to obtain the desired effect).

Clint Bowman			INTERNET:	clint at ecy.wa.gov
Air Dispersion Modeler		INTERNET:	clint at math.utah.edu
Air Quality Program		VOICE:		(360) 407-6815
Department of Ecology		FAX:		(360) 407-7534

	USPS:  		PO Box 47600, Olympia, WA 98504-7600
	Parcels:	300 Desmond Drive, Lacey, WA 98503-1274

On Fri, 7 Aug 2009, Jorgy Porgee wrote:

> Hi Jean,
> Thank you for the reply. I do have the forecast points before I plot,
> the example below was just for illustration purposes..If I am to add
> the forecast points to one y-series data plot however, is there a way
> of highlighting them? This is essentially what I'm trying to do below
> by plotting 3 separate series on the same graph...
> Any help would be much appreciated..
>
> Regards,
>
> George.
> In addition, I can't add the forecasts to the original series because I need
>
> On Fri, Aug 7, 2009 at 8:36 PM, Jean V Adams<jvadams at usgs.gov> wrote:
> >
> > Just wait until after you have the forecasts before you create the plot.
> >
> > # Sample dates
> > xValues <- seq.Date(as.Date("1990-01-31"), to=as.Date("1992-12-31"),
> > by="month")
> >
> > # Sample y value
> > yValues <- seq(0.1, length=length(xValues))
> >
> > # Sample forecast one year from xValue's end point
> > fcastDate <- seq.Date(from=as.Date(xValues[length(xValues)]), length=2,
> > by="year")[2]
> > fcast <- 20
> >
> > # The second forecast
> > fcastDate2 <- seq.Date(from=as.Date(fcastDate), length=2, by="year")[2]
> > fcast2 <- 15
> >
> > plot(xValues, yValues, type="n", xlim=range(c(xValues, fcastDate,
> > fcastDate2)), ylim=range(c(yValues, fcast, fcast2)))
> > lines(xValues, yValues)
> > points(fcastDate, fcast, col="red")
> > points(fcastDate2, fcast2, col="blue")
> >
> > Jean
> >
> >
> > -----
> >
> >
> > From: Jorgy Porgee <jorgy.porgee <at> gmail.com>
> > Subject: How do I plot a line followed by two forecast points?
> > Newsgroups: gmane.comp.lang.r.general
> > Date: 2009-08-07 15:17:52 GMT (2 hours and 55 minutes ago)
> >
> > 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.
>


More information about the R-help mailing list