« Plotting forecast() objects in ggplot part 2: Visualize Observations, Fits, and Forecasts | Main | Property rights and the economic origins of the Sicilian mafia »

Plotting forecast() objects in ggplot part 1: Extracting the Data

Lately I've been using Rob J Hyndman's excellent forecast package. The package comes with some built in plotting functions but I found I wanted to customize and make my own plots in ggplot. In order to do that, I need a generalizable function that will extract all the data I want (forecasts, fitted values, training data, actual observations in the forecast period, confidence intervals, et cetera) and place it into a data.frame with a properly formatted date field (ie, not  a ts() object).

The function below does all that and should work for any forecast object (though I've only tested it on Arima() outputs). The only arguments it takes are the original observations and the forecast object (whatever results from calling forecast()). In my next post I'll give some examples of plotting the results using ggplot and explain why I wanted more than the default plot.forecast() function.


#--Produces a data.frame with the Source Data+Training Data, Fitted Values+Forecast Values, forecast data Confidence Intervals
	require(zoo) #needed for the 'as.yearmon()' function
	en<-max(time(fcast$mean)) #extract the max date used in the forecast
	#Extract Source and Training Data
	#Extract the Fitted Values (need to figure out how to grab confidence intervals)
	ds<-merge(ds,dfit,all.x=T) #Merge fitted values with source and training data
	#Exract the Forecast values and confidence intervals
	pd<-merge(ds,dfcastn,all.x=T) #final data.frame for use in ggplot

Created by Pretty R at


PrintView Printer Friendly Version

EmailEmail Article to Friend

References (2)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
  • Response
    Response: Pascher Shop
    Endesa League: 7 days nine notes Classification: Information, Notes, Con Mens Work Boots Are A Necessity cheap pants for men- tests, Liga Endesa, newgorras Endesa Group,Mens Work Boots Are A Necessity cheap pants for men- , Perspective Express Endesa Little league Spherical 9: synopsis. We have reached the cen ...

Reader Comments (2)

Thanks for showing how to do this Frank. Just a couple of comments. You shouldn't need to pass the original observations as they are stored as component x in the forecast object. Also, if the forecast object contains other prediction intervals than 80 and 95% intervals, this function will cause an error. Finally, rather than use as.Date() on the row names, it would be simpler (and probably more robust) to use times() on the mean component.

March 22, 2012 | Unregistered CommenterRob J Hyndman

Hi Rob-

Thanks for the feedback! The idea behind including the original observations was that they also contain the observations during the forecast period. I suppose to make it simpler though I could just have the user pass the those observations rather than the whole series.

March 23, 2012 | Unregistered CommenterFrank Davenport

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>