How to Create Correlation Heatmap in R
Last Updated :
12 Jul, 2022
In this article let's check out how to plot a Correlation Heatmap in R Programming Language.
Analyzing data usually involves a detailed analysis of each feature and how it's correlated with each other. It's essential to find the strength of the relationship between each feature or in other words how two variables move in association to each other. If the variables grow together in the same direction it's a positive correlation otherwise a negative correlation. This correlation can be visualized via various graphs such as scatter plots etc.
Loading data
Let's load the environmental dataset and view the first 6 rows of the data using the head( ) function.
R
# Loading package,data and
# viwing 1st 6 rows of data
install.packages("lattice")
library(lattice)
# Load the New York City
# environmental dataset.
data(environmental)
data <-environmental
head(data)
Output:

Create Correlation Matrix
Let's create a correlation matrix for our data using cor( ) function and round each value to 2 decimal places. This matrix can be used to easily create the heatmap.
R
# create a correlation matrix of the data
# rounding to 2 decimal places
corr_mat <- round(cor(data),2)
head(corr_mat)
Output:

Correlation Heatmap using ggplot2
Using ggplot2 let's visualize correlation matrix on a heatmap.
Function: ggplot(data = NULL, mapping = aes(), ... , environment = parent.frame())
Arguments:
- data - Default dataset to use for plot.
- mapping - Default list of aesthetic mappings to use for plot
- environment - DEPRECATED. Used prior to tidy evaluation
Let's reduce the size of the correlation matrix by plotting the heatmap using melt( ) function and using ggplot to plot the heatmap. From this heatmap we can easily interpret which variables/features are more correlated and use them for in-depth data analysis. The ggplot function takes in a reduced correlation matrix and aesthetic mappings.
R
# Install and load reshape2 package
install.packages("reshape2")
library(reshape2)
# creating correlation matrix
corr_mat <- round(cor(data),2)
# reduce the size of correlation matrix
melted_corr_mat <- melt(corr_mat)
# head(melted_corr_mat)
# plotting the correlation heatmap
library(ggplot2)
ggplot(data = melted_corr_mat, aes(x=Var1, y=Var2,
fill=value)) +
geom_tile()
Output:

Reorder the Correlation matrix and Plot Heatmap
Reordering or sorting the correlation matrix with respect to coefficient helps us to easily identify patterns between the features/variables. Let's check out how to reorder the correlation matrix using hclust( ) function by clustering the features hierarchically(hierarchical clustering).
R
# Code to plot a reorederd heatmap
# Install and load reshape2 package
install.packages("reshape2")
library(reshape2)
# creating correlation matrix
corr_mat <- round(cor(data),2)
# reorder corr matrix
# using corr coefficient as distance metric
dist <- as.dist((1-corr_mat)/2)
# hierarchical clustering the dist matrix
hc <- hclust(dist)
corr_mat <-corr_mat[hc$order, hc$order]
# reduce the size of correlation matrix
melted_corr_mat <- melt(corr_mat)
#head(melted_corr_mat)
#plotting the correlation heatmap
library(ggplot2)
ggplot(data = melted_corr_mat, aes(x=Var1, y=Var2, fill=value)) +
geom_tile()
Output:

Adding Correlation coefficients to Heatmap
Correlation coefficients are a measure that represents how strong the relationship is between two variables. The higher the absolute value of the coefficient, the higher is the correlation.
Let's visualize a correlation heatmap along with correlation coefficients on the map using the "value" column in the correlation matrix as text. Using geom_text( ) function annotations can be added on the heatmap and use "value" as labels.
R
# Install and load reshape2 package
install.packages("reshape2")
library(reshape2)
# creating correlation matrix
corr_mat <- round(cor(data),2)
# reduce the size of correlation matrix
melted_corr_mat <- melt(corr_mat)
head(melted_corr_mat)
# plotting the correlation heatmap
library(ggplot2)
ggplot(data = melted_corr_mat, aes(x=Var1, y=Var2,
fill=value)) +
geom_tile() +
geom_text(aes(Var2, Var1, label = value),
color = "black", size = 4)
Â
Â
Output:
Â

Correlation Heatmap using heatmaply
Â
Let's use the heatmaply package in R to plot a correlation heatmap using the heatmaply_cor( ) function. Correlation of the data is the input matrix with "Features" column as x and y axis parameters.
Â
Function: heatmaply_cor(x, limits = c(-1, 1), xlab, ylab, colors = cool_warm,k_row, k_col ...)
Arguments:
- x - can either be a heatmapr object, or a numeric matrix
- limits - a two dimensional numeric vector specifying the data range for the scale
- colors - a vector of colors to use for heatmap color
- k_row - an integer scalar with the desired number of groups by which to color the dendrogram's
- branches in the rows
- k_col - an integer scalar with the desired number of groups by which to color the dendrogram's branches in the columns
- xlab - A character title for the x axis.
- ylab - A character title for the y axis.
Â
R
# Load and install heatmaply package
install.packages("heatmaply")
library(heatmaply)
# plotting corr heatmap
heatmaply_cor(x = cor(data), xlab = "Features",
ylab = "Features", k_col = 2, k_row = 2)
Â
Â
Output:
Â

Correlation Heatmap using ggcorplot
Â
Let's use the ggcorplot package in R to plot a correlation heatmap using ggcorrplot( ) function. The correlation matrix of the data is given as the input corr matrix.
Â
Function: ggcorrplot(corr,method = c("square", "circle") ... )
Arguments:
- corr - the correlation matrix to visualize
- method - character, the visualization method of correlation matrix to be used
Â
R
# load and install ggcorplot
install.packages("ggcorplot")
library(ggcorrplot)
# plotting corr heatmap
ggcorrplot::ggcorrplot(cor(data))
Output:

Plotting the lower triangle of the correlation heatmap
Let's check out how to plot the lower triangle of the correlation heatmap and visualize it. This can be done by replacing the upper triangle values of the correlation matrix as NA and then this matrix is reduced by melting process and plotted.
R
# get the corr matrix
corr_mat <- round(cor(data),2)
# replace NA with upper triangle matrix
corr_mat[upper.tri(corr_mat)] <- NA
# reduce the corr matrix
melted_corr_mat <- melt(corr_mat)
# plotting the corr heatmap
library(ggplot2)
ggplot(data = melted_corr_mat, aes(x=Var1,
y=Var2,
fill=value)) +
geom_tile()
Â
Â
Output:
Â

Plotting the upper triangle of the correlation heatmap
Â
Let's check out how to plot the upper triangle of the correlation heatmap and visualize it. This can be done by replacing the lower triangle values of the correlation matrix as NA and then this matrix is reduced by melting process and plotted.
Â
R
# get the corr matrix
corr_mat <- round(cor(data),2)
# replace NA with lower triangle matrix
corr_mat[lower.tri(corr_mat)] <- NA
# reduce the corr matrix
melted_corr_mat <- melt(corr_mat)
# plotting the corr heatmap
library(ggplot2)
ggplot(data = melted_corr_mat, aes(x=Var1, y=Var2,
fill=value)) +
geom_tile()
Â
Â
Output:
Â

Creating an Interactive Correlation Heatmap
Â
An interactive plot shows detailed information of each data point when the user hovers on the plot. Let's check out how to plot an interactive correlation heatmap using the correlation matrix and p-value matrix. ggplotly( ) function takes in a correlation matrix of the data and gives an interactive heatmap plot and the details can be viewed on hovering on the map.
Â
Function: ggplotly( p = ggplot2::last_plot(), Â width = NULL, Â height = NULL ... )
Arguments:
- p - a ggplot object
- width - Width of the plot in pixels (optional, defaults to automatic sizing)
- height - Height of the plot in pixels (optional, defaults to automatic sizing)
Â
R
# install and load the plotly package
install.packages("plotly")
library(plotly)
library(ggcorrplot)
# create corr matrix and
# corresponding p-value matrix
corr_mat <- round(cor(data),2)
p_mat <- cor_pmat(data)
# plotting the interactive corr heatmap
corr_mat <- ggcorrplot(
corr_mat, hc.order = TRUE, type = "lower",
outline.col = "white",
p.mat = p_mat
)
ggplotly(corr_mat)
Â
Â
Output:
Â

Â
Similar Reads
Spearman Correlation Heatmap in R
In R Programming Language it is an effective visual tool for examining the connections between variables in a dataset is a Spearman correlation heatmap. In this post, we'll delve deeper into the theory underlying Spearman correlation and show how to construct and read Spearman correlation heatmaps i
6 min read
Create a correlation Matrix using Python
Correlation matrix is a table that shows how different variables are related to each other. Each cell in the table displays a number i.e. correlation coefficient which tells us how strongly two variables are together. It helps in quickly spotting patterns, understand relationships and making better
2 min read
How to Create a Heat Map in Excel?
Heatmaps are basically used to calculate the intensity of an event. It helps us in easily understanding and interpreting any complex dataset and make deductions from it. It uses colors in the bar graph of a dataset and shows different regions with different colors, which area needs more attention an
2 min read
How to Create a Biplot in R
A biplot is a graphical representation that combines both the scores and loadings of a principal component analysis (PCA) in a single plot. This allows for the visualization of the relationships between variables and observations in a dataset. Creating a biplot in R can be done using several package
3 min read
How to Calculate Cross Correlation in R?
In this article we will discuss how to calculate cross correlation in R programming language. Correlation is used to get the relation between two or more variables. The result is 0, if there is no correlation between two variablesThe result is 1, if there is positive correlation between two variable
1 min read
Create Heatmap in R
Heatmap is a graphical way to represent data. It is most commonly used in data analysis. In data analysis, we explore the dataset and draws insight from the dataset, we try to find the hidden patterns in the data by doing a visual analysis of the data. Heatmap visualizes the value of the matrix with
4 min read
How to Create a Forest Plot in R?
In this article, we will discuss how to create a Forest Plot in the R programming language. A forest plot is also known as a blobbogram. It helps us to visualize estimated results from a certain number of studies together along with the overall results in a single plot. It is extensively used in med
4 min read
How to Calculate Correlation By Group in R
Calculating correlation by group in R Programming Language involves finding the correlation coefficient between two variables within each subgroup defined by another variable. In R, correlation by group can be achieved by using the cor() function along with other functions like group_by() from the '
5 min read
How to Draw Heatmap with Colorful Dendrogram in R
Heatmaps are powerful visualizations for representing data density, correlations, or the relationship between variables. Combined with dendrograms, they allow for an insightful exploration of hierarchical clustering. In this article, weâll learn how to create a heatmap with a colorful dendrogram in
3 min read
How to Create Radar Charts in R?
In this article, we are going to see how to create Radar Charts in R Programming Language. Radar charts are also known as Spider or Web or Polar charts. It is a graphical graph to display multivariate data in form of 2D charts of three or more quantitative variables which are represented on axes sta
4 min read