Site icon R-bloggers

ggplot2 Time Series Heatmaps

[This article was first published on MarginTale, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
How do you easily get beautiful calendar heatmaps of time series in ggplot2? E.g:
From MarginTale
I was impressed by the lattice-based  implementation from Paul Bleicher of Humedica, which you can find referenced in http://blog.revolutionanalytics.com/2009/11/charting-time-series-as-calendar-heat-maps-in-r.html. Then, when other blogs like http://timelyportfolio.blogspot.com/2012/04/piggybacking-and-hopefully-publicizing.html picked up the topic, I decided to try a ggplot2 implementation. In a comment to the above Revolution Analytics post, Hadley already presented a quick ggplot rendition, upon which I build here.

How do you attack the problem? Looking at the example output above:
  1. We facet_grid by “months” and “years” 
  2. The data itself is plotted by “week of month” and “day of week” and coloured according to the value of interest
So, given a time series we just have to fiddle with time indexes to create a data.frame containing the time series as well as per observation the corresponding “month”, “year”, “week of month”, “day of week”. The rest is then a one-liner of code with Hadley’s wonderful ggplot2 system.

The following code contains step by step comments:


It should be easy to wrap into a function and I hope its useful.

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

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.