### Plotting a Horizon Graph with R

Here is what the output looks like:

References:

```HorizonPlot <- function(  x, col = c("pink", "red", "lightgray", "blue"),
label.width = 15, label.cex = 0.7, xdiv = NULL,...)

{   # plot a horizon graph with various coloring options```
`# ONLY applicable to data with 3 main categories`
`# for (i in 1:50) {`
`# 	z1  <- zoo(rnorm(200),  as.Date("2007-01-01")  +  seq(0,  by  =  10, length  =  200))`
`# 	if (i == 1) z <- z1 else z <- merge(z, z1)`
`# }`
`# colnames(z) <- paste("Strategy", runif(50))`
`# HorizonPlot(as.xts(rollmean(z, 60)), main = "strategy returns")`
`# argument: class(x) = xts, color set for the 4 layers`
`# 10/16/2010 original version by Elliot`
`# 10/17/2010 last modified by Achim Zeileis`
`col <- rep(col, ncol(x))`
`lab <- colnames(x)`
`if (is.null(xdiv)) xdiv <- seq(min(x), max(x), length.out = 5)`
`createSingleHorizon <- function(y, ydiv=NULL)`
`{`
`if (is.null(ydiv)) ydiv <- seq(min(y), max(y), length.out=5)`
`y1 <- ifelse(y < ydiv[2], pmax(0, ydiv[2] - y), 0)`
`y2 <- ifelse(y < ydiv[3], pmax(0, pmin(ydiv[3] - ydiv[2], ydiv[3] - y)), 0)`
`y3 <- ifelse(y >= ydiv[3], pmax(0, pmin(ydiv[4] - ydiv[3], y - ydiv[3])), 0)`
`y4 <- ifelse(y >= ydiv[4], pmax(0, y - ydiv[4]), 0)`
`rval <- zoo(cbind(y2, y1, y3, y4), time(y))`
`if(is.xts(y)) as.xts(rval) else rval`
`}`
`for (i in seq_len(ncol(x))) {`
`horizon1 <- createSingleHorizon(x[,i], xdiv)`
`if (i == 1) horizon <- horizon1 else horizon <- merge(horizon, horizon1)`
`}`
`mypanel <- function(x, y, fill.base = 0, col = "lightgray", ...) {`
`lines(x, y, ...)`
`polygon(c(x[1], x, tail(x, 1), x[1]), c(fill.base, as.numeric(y), fill.base, fill.base), col = col)`
`panel.number <- parent.frame()\$panel.number`
`mtext(side = 2, text = lab[panel.number], las = 1, adj = 1, line = 4, cex = label.cex)`
`}`
`plot.zoo(horizon, n = 1, panel = mypanel, col = col, screens = sort(rep(seq_len(ncol(x)),4)),`
```mar = c(0, 2.1, 0, 2.1), oma = c(6, label.width, 5, 0), las = 2, xlab = "", ylab = "", ...)

}```