R-聚类热图

2024-04-11  本文已影响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()

上一篇下一篇

猜你喜欢

热点阅读