Jorgy Porgee
2009-Aug-07 15:17 UTC
[R] How do I plot a line followed by two forecast points?
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
Jean V Adams
2009-Aug-07 18:36 UTC
[R] How do I plot a line followed by two forecast points?
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 [[alternative HTML version deleted]]
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.
Jorgy Porgee
2009-Aug-07 20:21 UTC
[R] How do I plot a line followed by two forecast points?
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
Seemingly Similar Threads
- Setting up infile for R CMD BATCH
- Data.frames can not hold objects...What can be done in the following scenario?
- Forecasting with R/Need Help. Steps shown below with the imaginary data
- xyplot and abline
- Package "demography" - calculating percentiles of survival probabilities distribution