我正在研究不同变量的分布及其相关性。有没有办法突出高相关性?例如,我可以将大于 0.8 的相关性标记为红色,低于 -0.8 的相关性标记为蓝色。
问问题
86 次
1 回答
0
正如@thefringthing 在他们的评论中所说,这不是一项简单的任务,但绝对是可行的。
# 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
于 2021-06-23T04:32:58.163 回答