Plotting a Horizon Graph with R

Here is what the output looks like:

References:

http://www.perceptualedge.com/articles/visual_business_intelligence/time_on_the_horizon.pdf

http://www.stonesc.com/Vis08_Workshop/DVD/Reijner_submission.pdf

http://vis.berkeley.edu/papers/horizon/2009-TimeSeries-CHI.pdf

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 = "", ...)

}
Advertisements
Comments
2 Responses to “Plotting a Horizon Graph with R”
  1. Tom says:

    This is just gorgeous, but I wonder what the advantage is over a level plot that would simply color code the time series. Given that the only intuitive thing about the graph is the color (the ups and downs are difficult to interpret at first sight), and that all the information is contained in the color (provided of course the color scheme is sufficient fine), why not use a level plot?
    Thanks!

    • elliotnoma says:

      We often find that a level plot is our preferred method for presenting large amounts of data. The horizon graph has an advantage if we want to see the big picture and then zoom in for a more detailed look at a particular series, but as the number of series grows this becomes less of an advantage.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: