# Horizon plots with ggplot2, sort-of

August 19, 2012
By

(This article was first published on socialdatablog » R, and kindly contributed to R-bloggers)

The Timely Portfolio blog via R-bloggers has recently published some interesting entries about the value of horizon plots for visual comparison of a number of time series.

Very nice it looks too. You can read more about them here. The trick to understanding them is to imagine that each row was orginally a line chart of six times the height. First colour the area between the origin and the line so that dark reds are very negative and dark blues very positive. Then, for each row, slice the chart into six horizontal slices and lay them on top of one another. That way you save a lot of vertical space so comparisons are easier. Dark red still means very negative, etc. The vertical scale is the same for each chart.

This one was done using the latticeExtra package in R. I couldn’t figure out how to do them in ggplot. It was trivially easy to do a normal line chart and add a coloured background:

```library(ggplot2)
library(reshape)
require(PerformanceAnalytics)
data(edhec)
ed=data.frame(edhec)
ed\$date=as.Date(rownames(ed))
m=melt(ed,id="date")
m\$variable=gsub('\\.',' ',m\$variable)
ggplot(m,aes(date,0,fill=value))+geom_tile(aes(height=max(m\$value)-min(m\$value)))+geom_line(aes(x=date,y=value))+facet_grid(variable~.)+ scale_fill_gradient2(low="red",high="blue")+ylab("value")+ opts(strip.text.y=theme_text(angle=0, hjust=1))```

That’s it.

Not as sophisticated, but actually the colours help quite nicely for comparison without the complexity of the horizon approach. It seems less exciting – but then perhaps the horizon plot overstates its case a bit. And it is trivial to understand, which the horizon plot isn’t.

What do you think, is the horizon plot worth the extra effort?
Is there an easy way to do horizon plots in ggplot?

To leave a comment for the author, please follow the link and comment on their blog: socialdatablog » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , ,

Comments are closed.

# Never miss an update! Subscribe to R-bloggers to receive e-mails with the latest R posts.(You will not see this message again.)

Click here to close (This popup will not appear again)