Correlation matrix and plots


Problem


You want to calculate and visualize the correlation between continuous variables in a dataset.

 


Solution


Use functions within the corrplot package, as illustrated with the following example code.

#load required package
require(corrplot)
 
#read built-in example data
dat <- iris
 
#create pearson correlation matrix of continuous variables (1-4 for this example)
M<-cor(dat[,1:4], use="complete.obs", method="pearson")
 
#demo different options to plot/visualize the correlation matrix
corrplot(M, method="number")                      #display pearson correlation

corrplot(M, type="upper", method="number")        #only show upper half (removing redundant info)

corrplot(M, method="circle")                      #display graduated symbols

corrplot.mixed(M, lower="circle", upper="number") #hybrid of both


Notes


Anne-Marie Hodge and Jason Carlisle contributed to the development of this tool.