I am investigating the distribution of different variables and their correlations. Is there a way to highlight the high correlations? e.g. I can mark correlations greater than 0.8 as red and lower than -0.8 as blue.
Asked
Active
Viewed 640 times
1 Answers
0
As @thefringthing said in their comment, this is not a straightforward task but it is definitely doable.
This solution is based on this question and this answer:
# Load libraries
library(tidyverse)
library(GGally)
# Load some example data
mtcars <- mtcars[,1:6]
# Define function to colour panels according to correlation
cor_func <- function(data, mapping, method, symbol, ...){
x <- eval_data_col(data, mapping$x)
y <- eval_data_col(data, mapping$y)
corr <- cor(x, y, method=method, use='complete.obs')
colFn <- colorRampPalette(c("firebrick", "white", "dodgerblue"),
interpolate ='spline')
rampcols <- colFn(100)
match <- c(rampcols[1:10], rep("#FFFFFF", 80), rampcols[90:100])
fill <- match[findInterval(corr, seq(-1, 1, length = 100))]
ggally_text(
label = paste(symbol, as.character(round(corr, 2))),
mapping = aes(),
xP = 0.5, yP = 0.5,
color = 'black',
...) +
theme_void() +
theme(panel.background = element_rect(fill = fill))
}
plot1 <- ggpairs(mtcars,
upper = list(continuous = wrap(cor_func,
method = 'spearman', symbol = "Corr:\n")),
lower = list(continuous = function(data, mapping, ...) {
ggally_smooth_lm(data = data, mapping = mapping)}),
diag = list(continuous = function(data, mapping, ...) {
ggally_densityDiag(data = data, mapping = mapping)}
))
plot1

jared_mamrot
- 22,354
- 4
- 21
- 46