public
Last active

R Horizon Chart

  • Download Gist
horizon plot.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
require(lattice)
require(latticeExtra)
require(reshape2)
require(quantmod)
 
#set up horizon plots as a function
horizonplot <- function(prices,horizon.type="offset",scale=0.05,title=NA,alpha=0.4){
#get change in prices since beginning or 1st row
prices.change <- prices[,4]/as.numeric(prices[1,4])-1
#get as a data.frame so it will work well with melt and lattice
prices.change.df <- as.data.frame(cbind(as.Date(index(prices.change)),coredata(prices.change)))
#get date back to recognizable date form
prices.change.df[,1] <- as.Date(prices.change.df[,1])
#names columns for easier access
colnames(prices.change.df) <- c("date","change")
#smooth line for better appearance
prices.smooth <- spline(as.numeric(prices.change.df$change)~as.numeric(prices.change.df$date))
#do mirror graph
pmirror <-
xyplot(y~as.Date(x),data=prices.smooth,ylim=c(0,scale),origin=0,
par.settings=theEconomist.theme(box="transparent"),
lattice.options=theEconomist.opts(),
xlab=NULL,ylab=NULL,
#do function for an area chart
panel = function(x,y,...){
#divide by the scale specified and go through each time until no more left
#each pass will darken the graph with alpha
#first for loop will do the positive in green
for (i in 0:round(max(y)/scale,0))
panel.xyarea(x,y=ifelse(y>0,y,NA)-(scale * i),col="green",border="green",alpha=alpha,lwd=2,...)
#second for loop handles the negatives in red
#will take absolute value of y to mirror
for (i in 0:round(max(ifelse(y < 0,abs(y),0))/scale,0))
panel.xyarea(x,y=ifelse(y<0,abs(y),NA)-(scale * i),col="red",border="red",lwd=2,alpha=alpha,...)
},
main=title)
#get the positive and negative plots for the offset chart
#very similar to the mirror chart above
#except the negative values will be moved to the top of y range
#and inverted
ppos<-
xyplot(y~as.Date(x),data=prices.smooth,ylim=c(0,scale),origin=0,
par.settings=theEconomist.theme(box="transparent"),
lattice.options=theEconomist.opts(),
xlab=NULL,ylab=NULL,
panel = function(x,y,...){
#
for (i in 0:round(max(y)/scale,0))
panel.xyarea(x,y=ifelse(y>0,y,NA)-(scale * i),col="green",border="green",alpha=alpha,lwd=2,...)
},
main=title)
pneg <-
xyplot(y~as.Date(x),data=prices.smooth,ylim=c(0,scale),origin=scale,
panel=function(x,y,...){
for (i in 0:round(min(y)/-scale,0)) {
panel.xyarea(x,y=scale+ifelse(y<0,y,NA)+(scale*i),col="red",border="red",lwd=2,alpha=alpha,...)
}
})
ifelse(horizon.type=="mirror", return(pmirror), return(ppos+pneg))
 
}
 
 
getSymbols("^W0DOW",from="2011-12-31")
getSymbols("^GSPC",from="2011-12-31")
getSymbols("^DJUBS",from="2011-12-31")
 
#do the default offset
p1<-horizonplot(prices=W0DOW,horizon.type="offset",title="Dow Jones World Ex Americas")
p2<-horizonplot(prices=GSPC,horizon.type="offset",title="S&P 500")
p3<-horizonplot(prices=DJUBS,horizon.type="offset",title="Dow Jones UBS Commodity Index")
 
 
print(p1,position=c(0,0.66,1,1),more=TRUE)
print(p2,position=c(0,0.33,1,0.66),more=TRUE)
print(p3,position=c(0,0,1,0.33))
 
#do again but this time as mirror
p1<-horizonplot(prices=W0DOW,horizon.type="mirror",title="Dow Jones World Ex Americas")
p2<-horizonplot(prices=GSPC,horizon.type="mirror",title="S&P 500")
p3<-horizonplot(prices=DJUBS,horizon.type="mirror",title="Dow Jones UBS Commodity Index")
 
 
print(p1,position=c(0,0.66,1,1),more=TRUE)
print(p2,position=c(0,0.33,1,0.66),more=TRUE)
print(p3,position=c(0,0,1,0.33))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.