VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-to-create-correlation-heatmap-in-r/

⇱ How to Create Correlation Heatmap in R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.

Output:

👁 Image

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.

Output:

👁 Image

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.

Output:

👁 Image

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).

Output:

👁 Image

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.

 
 

Output:


 

👁 Image

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.


 

 
 

Output:


 

👁 Image

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


 

Output:

👁 Image

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.

 
 

Output:


 

👁 Image

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.


 

 
 

Output:


 

👁 Image

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)


 

 
 

Output:


 

👁 Image


 

Comment

Explore