【问题标题】:order of labels in corrplot after hclust在 hclust 之后 corrplot 中的标签顺序
【发布时间】:2017-04-02 20:53:19
【问题描述】:

在我的数据集中,变量属于不同的组,我想为每个变量分配颜色:

mycolors <- rep(NA,11)
names(mycolors) <- names(mtcars)
mycolors[1:4]   <- 'green' # mpg, cyl, disp, hp
mycolors[5:9]   <- 'red'   # drat, wt, qsec, vs, am
mycolors[10:11] <- 'blue'  # gear, carb

现在我想建立一个相关矩阵并为标签着色:

M <- cor(mtcars)
corrplot(M, tl.col = mycolors)

这很好用,但如果我用hclust 订购它,标签会以不同的顺序排列,这会导致颜色错误:

如何获取hclust之后标签的实际顺序,以便重新分配颜色?我尝试使用以下内容:

myclust <- hclust(dist(M))
myclust$labels[myclust$order]

这给出了与原始顺序不同的顺序,但仍与 corrplot 中使用的顺序不同...

【问题讨论】:

    标签: r r-corrplot


    【解决方案1】:

    使用here 的想法,您可以重新排序颜色向量,使其符合hclust 有序矩阵的顺序。

    ord <- corrMatOrder(M, order="hclust")
    newcolours <- mycolors[ord]
    newcolours
    #   carb      wt      hp     cyl    disp    qsec      vs     mpg    drat      am 
    # "blue"   "red" "green" "green" "green"   "red"   "red" "green"   "red"   "red" 
    #   gear 
    # "blue" 
    corrplot(M, tl.col = newcolours, order = "hclust")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      • 2018-09-13
      • 2017-09-22
      • 2016-06-28
      • 2017-05-31
      相关资源
      最近更新 更多