Open In App

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 in R using a number of examples and explanations.

Understanding Spearman Correlation

The Spearman correlation coefficient (ρ) is a non-parametric measure of association between two continuous or ordinal variables. Unlike Pearson correlation, which measures linear relationships, Spearman correlation assesses the monotonic relationship between variables. Monotonic relationships are those where one variable increases as the other decreases, or vice versa, without adhering to a specific linear pattern.



Spearman correlation is beneficial when:

Example 1: Correlation Heatmap for Ordinal Data




# Load required packages
library(corrplot)
 
# Create a sample dataset
data <- data.frame(
  ProductQuality = c(4, 5, 3, 2, 4),
  Price = c(2, 3, 1, 4, 2),
  CustomerSupport = c(3, 4, 2, 1, 3),
  DeliverySpeed = c(4, 5, 3, 2, 4)
)
 
# Calculate Spearman correlation coefficients
cor_matrix <- cor(data, method = "spearman")
 
# Create a correlation heatmap
corrplot(
  cor_matrix,
  method = "color",
  type = "upper",
  tl.cex = 0.8,
  tl.col = "black",
  tl.srt = 45,
  addCoef.col = "black",
  title = "Spearman Correlation Heatmap"
)

Output:

Spearman Correlation Heatmap in R

Example 2: Correlation Heatmap with Large Dataset




# Load required packages
library(corrplot)
library(datasets)
 
# Load the mtcars dataset
data(mtcars)
 
# Calculate Spearman correlation coefficients
cor_matrix <- cor(mtcars, method = "spearman")
 
# Create a correlation heatmap
corrplot(
  cor_matrix,
  method = "color",
  type = "upper",
  tl.cex = 0.7,
  tl.col = "black",
  tl.srt = 45,
  addCoef.col = "black",
  title = "Spearman Correlation Heatmap (mtcars)"
)

Output:



Spearman Correlation Heatmap in R

First we loads the mtcars dataset from the datasets package. The mtcars dataset contains information about various car models, including attributes such as miles per gallon (mpg), horsepower (hp), and weight (wt).

Example 3: Correlation Heatmap with Color Gradients




# Load required packages
library(corrplot)
library(datasets)
 
# Load the mtcars dataset
data(mtcars)
 
# Calculate Spearman correlation coefficients
cor_matrix <- cor(mtcars, method = "spearman")
 
# Create a correlation heatmap with color gradients
corrplot(
  cor_matrix,
  method = "color",
  type = "upper",
  col = colorRampPalette(c("blue", "white", "red"))(100),
  tl.cex = 0.7,
  tl.col = "black",
  tl.srt = 45,
  addCoef.col = "black",
  title = "Spearman Correlation Heatmap (mtcars)"
)

Output:

Spearman Correlation Heatmap in R

In this enhanced version, we use a color gradient that transitions from blue (negative correlations) to white (no correlations) to red (positive correlations). This creates a visually appealing and informative heatmap.

Example 4: Interactive Correlation Heatmap




# Load required packages
library(corrplot)
library(datasets)
library(heatmaply)
 
# Load the mtcars dataset
data(mtcars)
 
# Calculate Spearman correlation coefficients
cor_matrix <- cor(mtcars, method = "spearman")
 
# Create a correlation heatmap
heatmaply(cor_matrix,
  colors = colorRampPalette(c("blue", "white", "red"))(100),
  width = 800,
  height = 800,
  fontsize_row = 12,
  fontsize_col = 12,
  main = "Spearman Correlation Heatmap (mtcars)"
)

Output:

Spearman Correlation Heatmap in R

Conclusion:

Creating a Spearman correlation heatmap in R is a useful way to visualize the relationships between variables in your dataset. By examining the color patterns and values in the heatmap, you can gain insights into the strength and direction of the associations between variables, helping you make informed decisions in your data analysis and research.


Article Tags :