【问题标题】:How to highlight high correlations in ggpairs correlation matrix?如何突出 ggpairs 相关矩阵中的高相关性?
【发布时间】:2021-06-23 03:10:10
【问题描述】:

我正在研究不同变量的分布及其相关性。有没有办法突出高相关性?例如我可以将大于 0.8 的相关性标记为红色,低于 -0.8 的相关性标记为蓝色。

enter image description here

【问题讨论】:

标签: r ggplot2 correlation ggally ggpairs


【解决方案1】:

正如@thefringthing 在他们的评论中所说,这不是一项简单的任务,但它绝对是可行的。

此解决方案基于this questionthis 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

【讨论】:

    猜你喜欢
    • 2021-11-18
    • 2020-11-21
    • 2013-07-20
    • 1970-01-01
    • 2016-06-12
    • 2020-09-09
    • 2012-11-04
    • 1970-01-01
    相关资源
    最近更新 更多