美文网首页
R-聚类热图

R-聚类热图

作者: 花生学生信 | 来源:发表于2024-04-11 20:53 被阅读0次
临床信息聚类热图

基础版

OG聚类热图
setwd("D:\\data\\output\\NLR_add\\Orthogroups")
#library(tidyverse)
library(ComplexHeatmap)
library(pheatmap)
library(circlize)

ml <- df #画图的ssGSEA数据
ml <- as.data.frame(t(apply(ml, 2, scale))) #scale标化
colnames(ml) <- rownames(ml)

col_fun <- colorRamp2(c(-5, 0, 5), c("#377EB8", "white", "#E41A1C"))

#获取聚类分组信息
h1 <- Heatmap(ml, cluster_rows = TRUE, cluster_columns = TRUE, clustering_method_columns = "ward.D2",show_row_names = TRUE, show_column_names = FALSE,
              clustering_distance_columns = "euclidean", 
              clustering_distance_rows = "euclidean",
              clustering_method_rows  = "ward.D2")

tree <- column_dend(h1)
ind <- cutree(as.hclust(tree), k = 2)[order.dendrogram(tree)] #此处划分为2类,根据你自己的数据调整
table(ind)


ml$Immune_infiltration <- ind[ml$barcode]
pdf("1heatmap.pdf",h=20,w=15)
draw(h1)

dev.off()


数据格式

进阶版

heatmapinput$Immune_infiltration <- str_replace(heatmapinput$Immune_infiltration, "1", "Low infiltration")
heatmapinput$Immune_infiltration <- str_replace(heatmapinput$Immune_infiltration, "2", "High infiltration")

#临床信息注释
Immune_infiltration <- heatmapinput[, "Immune_infiltration"]
Tumor_site <- heatmapinput[, "tumor_site"] 
TP53_mutation <- heatmapinput[, "TP53mut"]
KRAS_mutation <- heatmapinput[, "KRASmut"]
BRAF_mutation <- heatmapinput[, "BRAFmut"]
EGFR_mutation <- heatmapinput[, "EGFRmut"]
Gender <- heatmapinput[, "gender"]
MSI <- heatmapinput[, "MSI"]
Polyps <- heatmapinput[, "colon_polyps_present"]
Survival <- heatmapinput[, "vital_status"] #这种生存信息是没有什么价值的,建议用生存状态加时间,详细画法可以参考complexheatmap
Anatomic_location <- heatmapinput[, "anatomic_neoplasm_subdivision"]
Stage <- heatmapinput[, "stage_event_pathologic_stage"]
ha = HeatmapAnnotation(Immune_infiltration =Immune_infiltration, Tumor_site = Tumor_site, 
                       TP53_mutation = TP53_mutation, KRAS_mutation = KRAS_mutation, 
                       BRAF_mutation = BRAF_mutation, EGFR_mutation = EGFR_mutation,
                       Gender = Gender, MSI = MSI, Polyps = Polyps, 
                       Survival = Survival, Anatomic_location = Anatomic_location,
                       Stage = Stage, show_annotation_name = FALSE, 
                       col = list(Immune_infiltration = c("High infiltration" = "#3FA538", 
                                                          "Low infiltration" = "#9FD29BFF"),
                                  Tumor_site = c("left" = "#E00115", "right" = "#5E84B6"),
                                  TP53_mutation = c("mutant" = "black", "wildtype" = "grey"),
                                  KRAS_mutation = c("mutant" = "black", "wildtype" = "grey"),
                                  BRAF_mutation = c("mutant" = "black", "wildtype" = "grey"),
                                  EGFR_mutation = c("mutant" = "black", "wildtype" = "grey"),
                                  Gender = c("MALE" = "#C4868E", "FEMALE" = "#97A8C7"),
                                  MSI = c("MSI-H" = "#E5554D", "MSI-L" = "#C4868E", 
                                          "MSS" = "#AEB6CE", "Indeterminate" = "#2B3D44"),
                                  Polyps = c("YES" = "black", "NO" = "grey"),
                                  Survival = c("Alive" = "#3FA538", "Dead" = "#E00115"),
                                  Stage =  c("Stage I" = "#B0B0FFFF", "Stage IA" = "#6060FFFF", "Stage II" = "#B0FFB0FF" , 
                                             "Stage IIA" ="#95FF95FF", "Stage IIB" = "#7AFF7AFF", "Stage IIC" = "#60FF60FF",
                                             "Stage III" = "#F7E897FF", "Stage IIIA" = "#F9EF64FF", 
                                             "Stage IIIB" = "#FCF732FF", "Stage IIIC" = "#FFFF00FF",
                                             "Stage IV" = "#FF6060FF", "Stage IVA"="#FF3030FF" , 
                                             "Stage IVB" = "#FF0000FF")),
                       na_col = "white", #各个临床信息的颜色,Anatomic_location颜色输入也同其他,更好颜色搭配信息请参考FigureYa28color
                       show_legend = rep(TRUE, 12),#是否要显示annotation legend
                       annotation_height = unit(rep(5, 12), "mm"),#临床annotation的高度
                       annotation_legend_param = list(
                         Immune_infiltration = list(title = "Immune infiltration"),
                         Tumor_site = list(title = "Tumor site"),
                         TP53_mutation = list(title = "TP53 mutation"),
                         KRAS_mutation = list(title = "KRAS mutation"),
                         BRAF_mutation = list(title = "BRAF mutation"),
                         EGFR_mutation = list(title = "EGFR mutation"),
                         Gender = list(title = "Gender"),
                         MSI = list(title = "MSI"),
                         Polyps = list(title = "Polyps"),
                         Survival = list(title = "Survival"),
                         Anatomic_location = list(title = "Anatomic location"),
                         Stage = list(title = "Stage"))#annotation legend的标签
)

ht <- Heatmap(ml, col = col_fun, 
              name = "CRC ssGSEA",
              cluster_rows = TRUE, cluster_columns = TRUE,          
              show_row_names = TRUE, show_column_names = FALSE,
              bottom_annotation = ha, column_title = qq("TCGA CRC samples (n = @{ncol(ml)})"),
              clustering_method_columns = "ward.D2",
              clustering_distance_columns = "euclidean", 
              clustering_distance_rows = "euclidean",
              clustering_method_rows  = "ward.D2", column_dend_height = unit(30, "mm")
)


pdf("2ssGSEA.pdf", 16, 12)
draw(ht, annotation_legend_side = "left", heatmap_legend_side = "left")

#装饰heatmap
annotation_titles <- c(Immune_infiltration = "Immune infiltration",
                       Tumor_site = "Tumor site",
                       TP53_mutation = "TP53 mutation",
                       KRAS_mutation = "KRAS mutation",
                       BRAF_mutation = "BRAF mutation",
                       EGFR_mutation = "EGFR mutation",
                       Gender = "Gender",
                       MSI = "MSI",
                       Polyps = "Polyps",
                       Survival = "Survival",
                       Anatomic_location = "Anatomic location",
                       Stage = "Stage")
for(an in names(annotation_titles)) {
  decorate_annotation(an, {
    grid.text(annotation_titles[an], unit(-2, "mm"), just = "right")#对齐方向是右边
    grid.rect(gp = gpar(fill = NA, col = "black"))
  })
}

#分组划线:具体数值要参考聚类的信息
decorate_heatmap_body("CRC ssGSEA", {
  grid.lines(c(0, 0), c(0, 1), gp = gpar(lty = 1, lwd = 2))
  grid.lines(c(table(ind)[[1]]/sum(table(ind)), table(ind)[[1]]/sum(table(ind))), 
             gp = gpar(lty = 2, lwd = 2))
  grid.lines(c(1, 1), c(0, 1), gp = gpar(lty = 1, lwd = 2))
})

#在heatmap上注释文字信息
decorate_heatmap_body("CRC ssGSEA", {
  grid.text("High infiltration", (table(ind)[[1]]/2)/sum(table(ind)), 0.1, #文字所放位置的x和y,根据自己的数据调整
            default.units = "npc", gp = gpar(fontsize = 16))
  grid.text("Low infiltration", (table(ind)[[1]] + table(ind)[[2]]/2)/sum(table(ind)), 0.1, default.units = "npc", gp = gpar(fontsize = 16))
})

dev.off()

相关文章

  • 10高通量测序-层次聚类

    层次聚类 层次聚类(Hierarchical Clustering)通常与热图有关(heatmap)。热图的列代表...

  • R 数据可视化 —— 聚类热图 ComplexHeatmap(二

    热图注释 前面我们介绍了如何绘制聚类热图,在这一节我们将介绍如何添加注释 热图的注释是聚类热图的重要组成部分,能够...

  • 使用ComplexHeatmap绘制复杂热图|Note2:单个热

    2. 单个热图 2.1 颜色2.2 标题2.3 聚类2.3.1 距离方法2.3.2 聚类方法2.3.3 渲染树状图...

  • 在线绘制聚类热图

    聚类热图是转录组测序类文章中常见的一种分析内容。今天小编就给大家讲解一个在线绘制聚类热图的方法。 我们使用的在线工...

  • 热图

    「热图」ComplexHeatmap展示单细胞聚类:http://xuzhougeng.top/archives/...

  • 聚类热图注释说明

    heatmap.2 聚类热图主要分为四个部分:热图,纵轴和横轴的两个分枝树图,一个色键。每个小方格表示一个基因,颜...

  • 分组聚类的热图

    1.问题 应该是会经常遇到这样的热图,就是说它的聚类情况无法与分组信息吻合。其实这个并不是错了,是因为用于聚类的行...

  • Hierarchical 聚类原理

    层次聚类(hierarchical clustering)通常和热图(heatmap)密切相关。 绘制热图前,通常...

  • 热图去掉聚类树但是保留聚类

    看到这个问题,你可能搜了好多答案,比如 热图如何去掉聚类树的同时保留聚类的顺序? - 简书 (jianshu.co...

  • R-热图-pheatmap

    pheatmap是绘制热图的经典R包。其中一些细节参数设置,之前每次遇到都是网上搜索。这次系统整理下常用用法,为以...

网友评论

      本文标题:R-聚类热图

      本文链接:https://www.haomeiwen.com/subject/wclhxjtx.html