Skip to content

Instantly share code, notes, and snippets.

@JoesDataDiner
Last active December 14, 2015 11:09
Show Gist options
  • Save JoesDataDiner/5077292 to your computer and use it in GitHub Desktop.
Save JoesDataDiner/5077292 to your computer and use it in GitHub Desktop.
All That Glitters - Prices
#Gold and stock prices in '07-'08:
hist.prices =
do.call(cbind,
lapply(c("GLD","SPY","QQQ"), function(symbol){
symbol.data = get(symbol) #get from enviroment
symbol.data.adj = Ad(symbol.data) #extract the adjusted price
symbol.data.adj
})
)
hist.prices = hist.prices[index(hist.prices) > as.Date("2007-01-01"),] #start '07
hist.prices = hist.prices[index(hist.prices) < as.Date("2009-01-01"),] #end '08
hist.prices = data.frame(hist.prices, date = index(hist.prices))
hist.prices = melt(hist.prices,id.var="date") #get into form for ggplot2
hist.prices$variable = gsub(".Adjusted","",hist.prices$variable) #tidy variable names
#use ggplot2 to plot a simple time-series of the prices
ggplot(hist.prices,aes(x=date,y=value,colour=variable))+geom_line()+theme_bw() +
scale_y_continuous("Price")+
scale_colour_brewer("",palette="Set1")+
theme(axis.title.x = element_blank())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment