r - 相关矩阵"Clustering"

标签 r

我想以高度相关的变量彼此相邻的方式“整理”或“聚类”给定数据矩阵的相关性。例如,如果我有这样的数据:

n <- 1000
my_corr <- matrix(c(1,   0.8, 0.8, 0,   0, 0,
                    0.8, 1,   0.8, 0,   0,   0,
                    0.8, 0.8, 1,   0,   0, 0,
                    0,   0,   0,   1,   0.9, 0.9,
                    0,   0,   0,   0.9, 1,   0.9,
                    0,   0,   0,   0.9, 0.9, 1), nrow = 6)
my_chol <- chol(my_corr)
# Edit: Rui is right, I should have used replicate
#d <- do.call(cbind, lapply(1:6, function(x) rnorm(n)))
d <- replicate(6, rnorm(n))
d <- d %*% my_chol
d <- cbind(d[, -c(2, 4)], d[, 2], d[, 4])

相关性如下:

library(GGally)
ggcorr(d)

enter image description here

但是,“想要”看起来像这样:

enter image description here

如果我不知道解决方案/是否可以使用预构建的函数,我该怎么做?

最佳答案

corrplot 有此选项:

my_corr <- matrix(c(1,   0.8, 0.8, 0,   0, 0,
                    0.8, 1,   0.8, 0,   0,   0,
                    0.8, 0.8, 1,   0,   0, 0,
                    0,   0,   0,   1,   0.9, 0.9,
                    0,   0,   0,   0.9, 1,   0.9,
                    0,   0,   0,   0.9, 0.9, 1), nrow = 6)

corrplot::corrplot(my_corr, order = "AOE", type = "lower", method = "color")

enter image description here

Here ,您几乎可以更改所有内容并使其更像 ggcorr

关于r - 相关矩阵"Clustering",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52868124/

相关文章:

r - 在 MacOS Catalina 上安装多个 R 版本(版本 10.15.2)

r - 将数据框中的一列字符串转换为 R 中的数字(不是通常的类型)

r - 使用readr时如何使用通配符定义col_type?

r - 向量化 R 表达式

r - 如何在 R 中的 ggplot2 中绘制混合效应模型估计值?

r - 从 github 安装 ggthemes r-package 开发者版本时出错

r - 设置矩阵值与 R 中的向量比较

r - 交叉产品滚动值

R Shiny : Write looped reactive inputs to textfile

r - ggplot折线图中的多行x轴标签