R绘图技巧26| 热图聚类树中样品顺序修改 + 色块注释

西瓜站长 生信益站 2023-07-07 08:00 发表于湖北
图片

热图画完了,也聚类了,却发现样品/分组顺序不对?如何解决?别急别急,请接着往下看哈...

示例数据

格式如下:

图片
mat <- "gene_id Control_1 Control_2 Control_3 Case_1 Case_2 Case_3
gene1 0.31 0.2 0.2 0.01 0 0.02
gene2 12.39 12.65 6.39 4.96 4.93 4.51
gene3 1.48 1.37 0.53 2.33 3.24 3.15
gene4 5.18 5.51 3.55 1.33 1.34 1.42
gene5 134.98 139.83 181.99 77.62 78.55 58.7
gene6 21.92 22.42 17.51 7.89 8.67 9.54
gene7 0.64 0.76 0.77 0.15 0.16 0.07
gene8 32.35 34.33 39.91 18.83 17.14 14.21
gene9 0.27 0.35 0.22 0.6 0.6 0.93
gene10 12.59 12.25 7.87 5.41 5.29 5.91
gene11 2652.43 2621.32 2910.4 1274.81 1223.32 986.14
gene12 12.91 12.96 16.9 7.58 7.98 5.1
gene13 78.44 77.79 100.18 21.31 20.5 15.83
gene14 24.56 24.96 24.35 1.89 2 2.28
gene15 46.31 43.7 29.33 76.33 95.88 94.56
gene16 66.15 69.35 90.47 62.08 38.25 41.69
gene17 126.12 122.82 146.47 10.91 8.32 6.74
gene18 1.47 1.5 1.6 0.4 0.5 0.49
gene19 2.72 2.95 1.85 5.5 5.53 5.99
gene20 2.51 3.1 2.65 1.05 1.41 1.42
gene21 1.74 1.9 1.64 0.2 0.07 0.22
gene22 12.05 12.77 14.03 5.54 5.03 4.77
gene23 1.23 1.06 1.04 0.41 0.53 0.43
gene24 36.29 38.21 57.59 14.79 12.2 12.64
gene25 0.14 0.19 0.11 0.21 0.28 0.51
gene26 247.23 242.04 343.33 137.13 114.11 99.65
gene27 1.58 1.29 1.03 0.02 0.01 0.03"


df <- read.table(textConnection(mat), sep = "\t", header = T, row.names = 1, check.names = F)

基础绘图

可以发现control组在右边

library(ComplexHeatmap)

df <- t(scale(t(df)))

hc <- hclust(dist(t(df)))

dend <- as.dendrogram(hc)

Heatmap(df, cluster_columns = dend, column_split = 2, row_split = 2, name = "exp")
图片

调整聚类树顺序

如果想修改聚类树顺序,这里修改列(样品)聚类,可以使用 dendextend::rotate()函数。

这里把control组放在左边

library(dendextend)

dend2 = rotate(dend, c(c(5,6,4), c(2,3,1)))

Heatmap(df, cluster_columns = dend2, column_split = 2, row_split =2, name = "exp")
图片

可以看出,样品顺序现在是control1-2-3;case1-2-3

美化热图

增加分组注释:

Heatmap(df,
    name = "exp",
    cluster_columns = dend2,
    column_split = 2,
    # 新增的参数
    top_annotation = HeatmapAnnotation(foo = anno_block(gp = gpar(fill = 2:3),
        labels = c("Control""Case"),
        labels_gp = gpar(col = "white", fontsize = 10))),
    row_split = 2,
    left_annotation = rowAnnotation(foo = anno_block(gp = gpar(fill = 2:3),
        labels = c("Cluster1""Cluster2"),
        labels_gp = gpar(col = "white", fontsize = 10)))
)
图片

去掉列名:

Heatmap(df,
    name = "exp",
    cluster_columns = dend2,
    column_split = 2,
    top_annotation = HeatmapAnnotation(foo = anno_block(gp = gpar(fill = 2:3),
        labels = c("Control""Case"),
        labels_gp = gpar(col = "white", fontsize = 10))),
    row_split = 2,
    left_annotation = rowAnnotation(foo = anno_block(gp = gpar(fill = 2:3),
        labels = c("Cluster1""Cluster2"),
        labels_gp = gpar(col = "white", fontsize = 10))),
    # 新增的参数
    show_column_names = F
)
图片

去掉聚类树标题:

Heatmap(df,
    name = "exp",
    cluster_columns = dend2,
    column_split = 2,
    top_annotation = HeatmapAnnotation(foo = anno_block(gp = gpar(fill = 2:3),
        labels = c("Control""Case"),
        labels_gp = gpar(col = "white", fontsize = 10))),
    row_split = 2,
    left_annotation = rowAnnotation(foo = anno_block(gp = gpar(fill = 2:3),
        labels = c("Cluster1""Cluster2"),
        labels_gp = gpar(col = "white", fontsize = 10))),
    show_column_names = F,
    # 新增的参数
    column_title = NULL,
    row_title = NULL
)
图片

看起来还不错,就这样吧!


OK,今天的分享到此为止,希望能对您有所帮助。

您的关注、点赞、在看、转发是对益站最大的鼓励和支持哈。

图片

联系站长

对本篇文章有疑问,可以在益站发消息留言,也欢迎各位童鞋来 QQ837738558 进行交流学习

图片
我们的企鹅群

收录于合集 #绘图专题
 105
上一篇R绘图技巧25| 10种方法教你画出最靓的桑基/冲积/河流图!
文章已于2023-07-07修改

微信扫一扫
关注该公众号