In this post, I show how to add change points to a trend chart with R. Readers can compare my R and Excel – VBA solutions for the same chart to compare R and Excel VBA charting programming.
My goal is to be ambidextrous in both Excel and R so that I can use the better tool for the charting job at hand rather than force a workaround solution to my problem. On the Excel Chart Doctor page of my ProcessTrends.com site, I have a number of advanced Excel charts, including this one showing El Nino and La Nina events on a long term temperature anomaly chart.
I used a VBA procedure to add the rectangles for the change points. The Excel Chart Doctor video explains the change point concept and shows my Excel-VBA procedure in action.
In this post, I make a similar chart with R.
Change Points on a Trend Chart
El Nino events, wars and recessions are examples of categorical conditions that may have an influence on time series data. We can show these conditions on our chart by inserting a rectangular shape to cover the chart area from the begining to the end of the period .
If we call the start date of each new period the change point, then we can build a series of rectangles by just knowing the change point dates. The sketch on the right shows several change points for our El Nino example. We consider the start date of the El Nino as a change point and the the end of the event as the start of another event period, nuetral or La Nina. This way we can establish event periods as the time between the change point dates. By recording the start date and code for each event, we can track and plot changing periods on our time series chart.
We can assign a color to each category rectangle so we can distinguish them.
R Trend Chart with Change Points
Here’s my R version of the GISS Temperature Anomaly and ENSO Condition chart.
The R version includes the 1950 – 2008 time period. The pink color periods are El Nino events, the light blue periods are La Nina events and the white background periods reflect neutral conditions.
The rectangles seem to work quite well.
Data Files and R Script File
Readers can download the R script and 2 data files on my ProcessTrends site at this link. If you try the script, be sure to change the source data file path to the actual folder where you saved the data files.