Heatmaps are used to show relationships between two variables, one plotted on each axis. By observing how cell colors change across each axis, you can observe if there are any patterns in value for one or both variables.

Here is an example of heat map. This is called as Risk Map. It shows the likelihood and impact of organisation’s risks. Risks that fall in Green areas of map require no action or monitoring. Risks that fall in red portions of the map require urgent attention. Risks in yellow and orange portions require some countermeasures.

In data analysis, Heatmap is powerful data visualization tool to understand the correlations between variables. Darker colors usually indicate higher correlation values, while lighter colors indicate lower or no correlation.

In this blog post, we’ll dive into the world of correlation heatmaps using R, using the `mtcars`

dataset as examples. By the end of this post, you’ll be equipped to create informative correlation heatmaps on your own. Let’s get started.

**What is correlation?**

Let’s first quickly understand the term correlation.

Correlation is a statistical measure that *quantifies the strength and direction* of the linear relationship between two variables. It ranges from -1 to +1, where -1 indicates a perfect negative correlation, +1 indicates a perfect positive correlation, and 0 indicates no linear correlation.

In R, the correlation between two variables can be found out using `cor`

function. If `cor`

function is used on data set or matrix, it computes the correlation between all the columns of data frame or matrix .

So, If `cor`

function is used on mtcars data set, then it gives the correlation values for all the variables in the data set.

`cor(mtcars)`

You can also notice that all the values along the diagonals of correlation matrix are 1, indicating a correlation of a variables with its own values.

Navigating through the myriad of correlation values makes it quite challenging to interpret the numbers in this matrix. For e.g. To determine the correlation between the variables `am`

and `gear`

, we need to locate the corresponding row and column in the matrix and then identify the value at their intersection. This process is prone to errors. Instead we will use Data visualization tool – the heatmap, for understanding the relationships between variables in a data set.

**What are heatmaps?**

Heat maps are a visual representation of data where values are depicted using colors. In the context of correlation, heat maps use color intensity to represent the strength of the correlation between variables. Darker colors usually indicate higher correlation values, while lighter colors indicate lower or no correlation.

**How to create heat maps in R?**

To generate the correlation heat map for `mtcars`

data set we have to first install `corrplot`

package. `mtcars`

data set contains information on various car models and their characteristics.

```
install.packages("corrplot")
library(corrplot)
```

`corrplot()`

function from `corrplot`

package is used to generate correlation heat map.

```
cor_matrix <- cor(mtcars)
corrplot(cor_matrix,method="color")
```

In this example, we use the `cor()`

function to compute the correlation matrix for the mtcars data set. The `corrplot()`

function is then used to create the heatmap. The argument `method = "color"`

specifies that we want to represent the correlation values using colors.

The dark blue colors are high positive correlation values and dark red colors are high negative correlation values. You can quickly spot the highly positive correlation between variables `gear`

and `am`

**Different representations of heatmaps**

`corrplot()`

function supports seven different `methods`

to visualize the heatmap.

- square
- circle
- ellipse
- number
- pie
- shade

The areas of circles or squares show the absolute value of corresponding correlation coefficients.

```
corrplot(cor_matrix,method="square")
corrplot(cor_matrix,method="circle")
```

`method=number`

shows the values of correlation coefficients in different color bands.

`corrplot(cor_matrix,method="number")`

The correlation matrix is symmetrical, with identical values in the lower and upper diagonal elements. Therefore, it is sufficient to display only the lower or upper triangular matrix elements instead of the entire matrix.

`type = c("full", "lower", "upper")`

displays full matrix, lower triangular or upper triangular matrix respectively.

```
corrplot(cor_matrix,method="square",type="lower")
corrplot(cor_matrix,method="square",type="upper")
```

**Summary**

In data analysis, Heatmap is powerful data visualization tool to understand the correlations between variables.

We can create heatmap from a correlation matrix. `cor`

function is used to create correlation matrix.

We created heatmap using `corrplot`

function from `corrplot`

package. We also used various visual representations to show the heatmap.