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

Jorgy Porgee jorgy.porgee at gmail.com
Sun Aug 9 17:05:30 CEST 2009


Thank you but just fyi, the lines() and points() approach proposed by
Jean earlier solved the problem. Thanks all once again.

On Sat, Aug 8, 2009 at 7:49 PM, Felipe Carrillo<mazatlanmexico at yahoo.com> wrote:
> Try this, it plots two points ahead based on the existing data of a month apart.
>
>
> # 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))
>  mydf <- data.frame(xValues,yValues);mydf
>  mydf[,1]
>  library(forecast)
>  yValues <- as.numeric(yValues)
> Ypredict <- forecast(yValues,h=2,level=95)
> Xpredict <- forecast(xValues,h=2,level=95);Xpredict
>
> yValues <- as.numeric(Ypredict$mean)
>  upper <- as.numeric(Ypredict$upper)
>  lower <- as.numeric(Ypredict$lower)
> xValues <- as.Date(Xpredict$mean)
>
>  forecast <- data.frame(yValues,upper,lower,xValues)
> forecast[1,4] <- '1993-01-31';forecast[2,4] <- '1993-02-28'
>  forecast
>  mydf$upper <- mydf$yValues
> mydf$lower <- mydf$yValues
> PredictedValues <- rbind(mydf, forecast)
>
> library(ggplot2)
> predict2 <- ggplot(PredictedValues,aes(xValues, yValues)) +  geom_line(colour="red",size=2)# + geom_point(colour="pink")
> predict2 <- predict2 +  geom_smooth(aes(ymin = lower, ymax = upper),stat = "identity",colour = "pink", fill = "lightgoldenrod") + geom_point(colour="goldenrod",size=3)
> predict2 <- predict2 +  geom_text(data=forecast,label=round(forecast$yValues,1),hjust=1.2,colour="blue",size=3)  +
>  geom_line(data=forecast,colour="green") + geom_point(data=forecast,colour="green",size=3.5) +
>   opts(panel.background = theme_rect(fill = "white")) + opts(panel.grid.minor = theme_blank())  + opts(panel.grid.major = theme_line(linetype = "dotted",colour="grey89"))
>  print(predict2)
>
>
> Felipe D. Carrillo
> Supervisory Fishery Biologist
> Department of the Interior
> US Fish & Wildlife Service
> California, USA
>
>
> --- On Fri, 8/7/09, Clint Bowman <clint at ecy.wa.gov> wrote:
>
>> From: Clint Bowman <clint at ecy.wa.gov>
>> Subject: Re: [R] How do I plot a line followed by two forecast points?
>> To: "Jorgy Porgee" <jorgy.porgee at gmail.com>
>> Cc: "Jean V Adams" <jvadams at usgs.gov>, r-help at r-project.org
>> Date: Friday, August 7, 2009, 1:33 PM
>> 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.
>> >
>> -----Inline Attachment Follows-----
>>
>> ______________________________________________
>> 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