0

我创建了一个带有 heatmaply 的树状图,其中包含来自 dendextend 包的树状图对象。我想知道是否有办法更改列的顺序:vs、am、carb、wt、drat、gear 等。例如,我想按字母顺序对它们进行排序。这可能吗?

library(heatmaply)
library(tidyverse)
library(RColorBrewer)
library(dendextend)

custom_colors <- c("#B6E2D3", "#887BB0", "#FFD53D", "#EF7C8E", "#A1F7A1", "#EFE7BC", "#C26DBC", "#14C2DD",
                   "#5CD85A", "#AB6B51", "#39918C", "#FFA384", "#57DDF3", "#D0B49F", "#EEB5EB", "#74BDCB")

dend_example <- mtcars %>% dist %>% hclust(method = "ward.D2") %>% 
  as.dendrogram %>% color_branches(k=5) %>% color_labels

heatmaply_mtcars<- heatmaply(mtcars, hclustfun = hclust, hclust_method = "ward.D2",
                             Rowv = dend_example, 
                             main = "mtcars",
                             show_dendrogram = c(TRUE, FALSE),
                             showticklabels = c(TRUE, TRUE),
                             scale_fill_gradient_fun = ggplot2::scale_fill_gradient2(high = "#025492"),
                             file = "heatmaply_mtcars.html",
                             column_text_angle = 30,
                             colorbar_thickness = 50)

在此处输入图像描述

4

1 回答 1

0

我在带有 colv 参数的 heatmaply 中包含了一个列树状图。使用包 dendextend 中的旋转函数旋转列树状图:

dend_example <- mtcars %>% dist %>% hclust(method = "ward.D2") %>% 
  as.dendrogram %>% color_branches(k=5) %>% color_labels

column_dend <- t(mtcars) %>% dist %>% hclust(method = "ward.D2") %>% 
  as.dendrogram
column_dend  <- column_dend %>% rotate(c(vector_with_labels))

heatmaply_mtcars<- heatmaply(mtcars, hclustfun = hclust, hclust_method = "ward.D2",
                             Rowv = dend_example, 
                             Colv = column_dend,
                             main = "mtcars",
                             show_dendrogram = c(TRUE, FALSE),
                             showticklabels = c(TRUE, TRUE),
                             scale_fill_gradient_fun = ggplot2::scale_fill_gradient2(high = "#025492"),
                             file = "heatmaply_mtcars.html",
                             column_text_angle = 30,
                             colorbar_thickness = 50)
于 2020-12-09T22:41:29.710 回答