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:

 

How to Create Correlation Heatmap in R

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.

Similar Reads

Loading data

Let’s load the environmental dataset and view the first 6 rows of the data using the head( ) function....

Create Correlation Matrix

...

Correlation Heatmap using ggplot2

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

Reorder the Correlation matrix and Plot Heatmap

...

Adding Correlation coefficients to Heatmap

Using ggplot2 let’s visualize correlation matrix on a heatmap....

Correlation Heatmap using heatmaply

...

Correlation Heatmap using ggcorplot

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

Plotting the lower triangle of the correlation heatmap

...

Plotting the upper triangle of the correlation 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....

Creating an Interactive Correlation Heatmap

...