Graphing Fed Data

The quantmod package is great for grabbing various financial data series from online sources like Google, Yahoo, Oanda, and the Federal Reserve. It also has some nifty technical charts that look very nice but are rather difficult to modify. One such problem is charting multiple data series. In quantmod, you could do this:

library(quantmod)
variables=c(“INDPRO”,”TCU”); names=c(“Industrial Production”,”Capacity Utilization SA”)
getSymbols(variables,src=”FRED”)
recent=120 #Set number of most recent observations
chartSeries(last(get(variables[1]),recent),name=names[1],minor.ticks=FALSE,theme=”white”,yrange=c(70,100))
addTA(last(get(variables[2]),recent),on=1)

It works but doesn’t really give you a result that you would want to publish. While there are probably ways to clean this up and make it presentable, it starts to become more work than it is worth.

The ggplot2 package does not really take advantage of xts and zoo class objects but it is fairly simple to transform them into a dataframe to use with ggplot. The same exercise as above can be done with greater control and what I would consider to be better quality graphics in ggplot.

library(quantmod,ggplot2)
variables=c(“INDPRO”,”TCU”); names=c(“Industrial Production”,”Capacity Utilization SA”)
getSymbols(variables,src=”FRED”)
recent=120 #Set number of most recent observations
temp=as.data.frame(merge.xts(last(get(variables[1]),recent),last(get(variables[2]),recent)))
temp$Date=as.Date(row.names(temp)); temp=melt(temp,id=”Date”,measure=c(“TCU”,”INDPRO”))
qplot(Date,value,data=temp,geom=”line”,colour=variable,ylab=”Index”)

Is it longer? Yes. Is it worth the extra effort? I certainly think so.

Author: Probable Pattern

Former Marine and Curious Critter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s