
The 'pairs' function in R contains a lot of additional options, which can be used to make very informative plots. These options can get a little cumbersome, but fortunately several package authors have written wrapper functions that automatically enable some extra magic. Two such packages are psych and PerformanceAnalytics. I happen to prefer the 1 liner from PerformanceAnalytics, but it's a matter of personal taste:

This chart contains a LOT of information: On the diagonal are the univariate distributions, plotted as histograms and kernel density plots. On the right of the diagonal are the pair-wise correlations, with red stars signifying significance levels. As the correlations get bigger the font size of the coefficient gets bigger. On the left side of the diagonal is the scatter-plot matrix, with loess smoothers in red to help illustrate the underlying relationship. This is one of my favorite plots in R, because it combines a large amount of information into one command and one easy to follow plot. In fact, this plot contains more information than is revealed by the 1st two commands in this post!
Of course, you can use this command on data from other domains besides Ecology. PerformanceAnalytics is intended for the analysis of financial data, so lets put it through its paces. First we download some financial data (a stock index, a bond index, and a gold index) from yahoo finance using quantmod, and then combine the daily close series of those indexes into one dataframe. I'm not 100% happy with the legend in the plot, but I wanted to show how the correlations between these indexes have changed over the years. I also skipped red (color #2) in the plots and in the legend, because the loess smoother is also red.

Finally, I'd like to acknowledge Stephen Turner over on cross-validated for inspiring this post.
Great Post!!!
ReplyDeleteFabio
Thank you!
ReplyDeleteThanks for the mention, and very nice post. Didn't know about the PerformanceAnalytics function.
ReplyDelete@Stephen Turner: No problem! Glad you liked the post.
ReplyDeleteMagnificent Post! Good job!
ReplyDeleteThanks very much for the post, but is there a possibility to get rid of
ReplyDeletethe significance asterisks? In my case they make no sense whatsoever
and I would like to know if there is a more elegant solution than
photoshop or paint.
Then are you sure you want to be calculating correlation in the upper right panel? You can write your own customer "pairs" function, e.g. "pairs(iris[-5], upper.panel=panel.smooth, lower.panel=panel.smooth)". See "?pairs" for more options, including correlations without asterisks.
ReplyDelete