Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Created March 14, 2022 18:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aagarw30/208ee7ec3bc16e8da1ee8ba73a95839f to your computer and use it in GitHub Desktop.
Save aagarw30/208ee7ec3bc16e8da1ee8ba73a95839f to your computer and use it in GitHub Desktop.
R Tutorial - Visualization of correlation matrix in R using ggcorrplot package in R - ggcorrplot tutorial - ggplot2 extension
# Demonstration of ggcorrplot package
# Package created by Alboukadel Kassambara
# Reference link - https://cran.r-project.org/web/packages/ggcorrplot/ggcorrplot.pdf
# Load the required libraries
# install.packages("ggcorrplot")
library(ggcorrplot)
# mtcars dataset from base R used here for demonstration
data(mtcars)
# Structure of mtcars dataset
str(mtcars)
# help page of dataset
# ?mtcars
######### Correlation matrix and visualization #####
# Step - 1 : Calculate the correlation measure
cor(mtcars)
cor = round(cor(mtcars), 1)
# Step - 2 : Applying the ggcorrplot() on correlation matrix just created
ggcorrplot(cor) # square tiles by default
# Applying the ggcorrplot with method="circle"
ggcorrplot(cor, method = "circle")
# Applying hc order to it
# Correlation matrix will be ordered based on hierarchical clustering method
ggcorrplot(cor,
method = "circle",
hc.order = TRUE)
# with square tiles
ggcorrplot(cor,
method = "square",
hc.order = TRUE)
# Correlogram layout - type = "lower"
ggcorrplot(cor,
method = "circle",
hc.order = TRUE, type = "lower")
ggcorrplot(cor,
method = "square",
hc.order = TRUE, type = "lower")
# Correlogram layout - type = "upper"
ggcorrplot(cor,
method = "circle",
hc.order = TRUE, type = "upper")
######### Formatting the Correlation matrix #######
## Add a title to correlogram
ggcorrplot(cor,
method = "circle",
hc.order = TRUE, type = "upper",
title = "Correlogram - mtcars dataset")
## Add a legend title
ggcorrplot(cor,
method = "circle",
hc.order = TRUE, type = "upper",
title = "Correlogram - mtcars dataset",
legend.title = "Pearson \n Corr"
)
## Add correlation coefficients
ggcorrplot(cor,
method = "square",
hc.order = TRUE, type = "upper",
title = "Correlogram - mtcars dataset",
legend.title = "Pearson \n Corr", lab = TRUE
)
# Change the coefficient label size and color
ggcorrplot(cor,
method = "square",
hc.order = TRUE, type = "upper",
title = "Correlogram - mtcars dataset",
legend.title = "Pearson \n Corr",
lab = TRUE, lab_col ="white" , lab_size = 2
)
## Add a ggplot2 theme
# theme_light
# theme_void
# theme_dark
# theme_grey
# theme_linedraw
library(ggplot2)
ggcorrplot(cor,
method = "square",
hc.order = TRUE, type = "upper",
title = "Correlogram - mtcars dataset",
legend.title = "Pearson \n Corr",
lab = TRUE, lab_size = 2, ggtheme = theme_void
)
# add outline color to tiles
ggcorrplot(cor,
method = "square",
hc.order = TRUE, type = "upper",
title = "Correlogram - mtcars dataset",
legend.title = "Pearson \n Corr",
lab = TRUE, lab_size = 2,
ggtheme = theme_void, outline.color = "black"
)
# change the legend colors
ggcorrplot(cor,
method = "square",
hc.order = TRUE, type = "upper",
title = "Correlogram - mtcars dataset",
legend.title = "Pearson \n Corr",
lab = TRUE, lab_size = 2,
ggtheme = theme_void,
outline.color = "white",
colors = c("#6D9EC1", "white", "#E46726")
)
######### Calculating p values and marking/dropping insignificant correlation coefficients ######
# Calculate p-values of the correlation for significance level
p.mat = cor_pmat(mtcars)
ggcorrplot(cor,
method = "square",
hc.order = TRUE,
outline.color = "white",
type = "upper",
p.mat = p.mat
)
# If the p-value in p-mat is bigger than sig.level, then the corresponding correlation coefficient is regarded as insignificant
# default significance level is 0.05 but can be changed
## Blank to insignificant levels
ggcorrplot(cor,
method = "square",
hc.order = TRUE,
outline.color = "white",type = "upper",
p.mat = p.mat, insig = "blank"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment