Stacked time series in R

Stacked time series plots are cool. Vastly superior to double y-axis plots which are a big no no according to graph theory. Exhibit A and Exhibit B. If Hadley Wickham is anti double y-axis plots then maybe you should be too? The core of the problem is that there is no objective way to scale the two y-axes, making it easy to suggest correlation when in fact there is none.

Enter stacked time series. I like them probably because I was indoctrinated early on in my Matlab/Oceanography days. The only downside is that they can take up more space on the page. But I think they’re the bees knees. So here’s a primer on creating stacked time series plots using the packages ggplot2 and gridExtra. I think of this as the brute force method. There is a cleaner way to do this by faceting in ggplot2, but we’ll leave that for a future post.

This is time series data courtesy of the National Estuarine Research Reserve. You can download the data here, but you’ll have to convert it to a csv file because WordPress only supports xls files.

So here, I’ve loaded the packages, then the data. I’ve created a new variable called “rdate” which will be in POSIXlt format. Then, I’ve created another dataframe using every fourth value because the original dataset was a bit longish at 35,000 rows. The new one is 8,760 rows. Don’t judge me, my laptop is old.