Correlation matrix : How to make a heatmap ?


The goal of this document is to show you how to visualize correlation matrix using R heatmap function. This type of plot can help to quickly identify the most correlated variables. You can read more on correlation matrix by clicking here.

Data

The mtcars data is used in the following examples :

mydata <- mtcars[, c(1,3,4,5,6,7)]
head(mydata)
                   mpg disp  hp drat    wt  qsec
Mazda RX4         21.0  160 110 3.90 2.620 16.46
Mazda RX4 Wag     21.0  160 110 3.90 2.875 17.02
Datsun 710        22.8  108  93 3.85 2.320 18.61
Hornet 4 Drive    21.4  258 110 3.08 3.215 19.44
Hornet Sportabout 18.7  360 175 3.15 3.440 17.02
Valiant           18.1  225 105 2.76 3.460 20.22

Computing the correlation matrix

The correlation matrix is computed as follow:

cormat<-signif(cor(mydata),2)
cormat
       mpg  disp    hp   drat    wt   qsec
mpg   1.00 -0.85 -0.78  0.680 -0.87  0.420
disp -0.85  1.00  0.79 -0.710  0.89 -0.430
hp   -0.78  0.79  1.00 -0.450  0.66 -0.710
drat  0.68 -0.71 -0.45  1.000 -0.71  0.091
wt   -0.87  0.89  0.66 -0.710  1.00 -0.170
qsec  0.42 -0.43 -0.71  0.091 -0.17  1.000

Drawing a heatmap

# Get some colors
col<- colorRampPalette(c("blue", "white", "red"))(20)
heatmap(cormat, col=col, symm=TRUE)

Heatmap of correlation matrix

Negative correlations are shown in blue and the positive ones in red.

Infos

This analysis has been performed with R (ver. 3.1.0).



Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!





This page has been seen 52851 times