美文网首页
ComplexHeatmap

ComplexHeatmap

作者: 余绕 | 来源:发表于2023-01-04 14:59 被阅读0次
  1. 准备数据
load(file="data/geo-cesc/prepare.rdata")
load(file="data/geo-cesc/de.rdata")

cancer_normal_samples<-rownames_to_column(sample_info,var = "sample_id") %>% filter(
  group=='Cancer' | group=='Normal') %>%
  pull(sample_id)


# 提取 cancer 和 normal 样本信息表
cancer_normal_samples_info <- 
  sample_info[cancer_normal_samples, ]

tail(cancer_normal_samples_info)

top100_de <- select(de_result, Gene_Symbol, one_of(cancer_normal_samples)) %>%
  filter(!is.na(Gene_Symbol)) %>%
  distinct(Gene_Symbol, .keep_all = T) %>%
  dplyr::slice(1:100) %>%
  column_to_rownames(var = 'Gene_Symbol')

  1. 加载r包
library(tidyverse)
library(ComplexHeatmap) 
library(circlize)

3.1 绘图

Heatmap(top100_de[1:20,],
        show_column_names = F,
        # 图例加名字
        name = "Expression",
        # 加列名
        column_title = "Gene Expression Heatmap",
        column_title_side = "top"
)
image.png

3.2 自定义颜色绘图

library(circlize) #后续颜色选择来自该包
Heatmap(top100_de[1:20,],
        show_column_names = F,
        name = "Expression",
        column_title = "Gene Expression Heatmap",
        column_title_side = "top",
        # 修改颜色
        col = colorRamp2(
          # 将 0,10,20
          breaks = c(0, 10, 20),
          # 分别对应给三种颜色
          colors = c('green','white', 'red')
        )
)
image.png

3.3 加边框

Heatmap(top100_de[1:20,],
        show_column_names = F,
        name = "Expression",
        column_title = "Gene Expression Heatmap",
        column_title_side = "top",
        col = colorRamp2(
          breaks = c(0, 10, 20),
          colors = c('green','white', 'red')
        ),
        # 外边框
        border = 'white',
        # 内边框
        rect_gp = gpar(col = 'white', lwd = 1)
)

image.png

3.4 按照聚类分割图

Heatmap(top100_de[1:20,],
        show_column_names = F,
        name = "Expression",
        column_title = "Gene Expression Heatmap",
        column_title_side = "top",
        col = colorRamp2(
          breaks = c(0, 10, 20),
          colors = c('green','white', 'red')
        ),
        border = 'white',
        rect_gp = gpar(col = 'white', lwd = 1),
        row_names_gp = gpar(fontsize = 8, fontface = 'italic'),
        column_title_gp = gpar(fontface = 'bold'),
        # 分为 3 组
        column_km = 3,
        # 组间 gap
        column_gap = unit(0.03, 'npc') #安装聚类分割,npc是按照比例分割
)
image.png

3.5 按照样品类型进行分割column_split = cancer_normal_samples_info$group

Heatmap(top100_de[1:20,],
        show_column_names = F,
        name = "Expression",
        column_title = "Gene Expression Heatmap",
        column_title_side = "top",
        col = colorRamp2(
          breaks = c(0, 10, 20),
          colors = c('green','white', 'red')
        ),
        border = 'white',
        rect_gp = gpar(col = 'white', lwd = 1),
        row_names_gp = gpar(fontsize = 8, fontface = 'italic'),
        column_title_gp = gpar(fontface = 'bold'),
        # 对列分割,按照样品信息进行分割
        column_split = cancer_normal_samples_info$group
)

image.png

3.6 添加注释图

#1.注释
column_ha <- HeatmapAnnotation(
  group = cancer_normal_samples_info$group,
  test1 = cancer_normal_samples_info$test1,
  test2 = cancer_normal_samples_info$test2,
  col = list(
    group = c(Cancer = '#fc8d59', Normal = '#99d594'),
    test1 = colorRamp2(
      breaks = c(20, 50, 80),
      colors = c('white', '#E38169', '#E9593F')
    )
  )
)

#添加注释
Heatmap(top100_de[1:20,],
        show_column_names = F,
        name = "Expression",
        column_title = "Gene Expression Heatmap",
        column_title_side = "top",
        col = colorRamp2(
          breaks = c(0, 10, 20),
          colors = c('green','white', 'red')
        ),
        border = 'white',
        rect_gp = gpar(col = 'white', lwd = 1),
        row_names_gp = gpar(fontsize = 8, fontface = 'italic'),
        column_title_gp = gpar(fontface = 'bold'),
        column_km = 3,
        column_gap = unit(0.03, 'npc'),
        # 在上方添加列注释
        top_annotation = column_ha
)

image.png

3.7 其他类型注释

#注释
column_ha <- HeatmapAnnotation(
  group = cancer_normal_samples_info$group,
  test1 = cancer_normal_samples_info$test1,
  test2 = anno_points(cancer_normal_samples_info$test2,
                      size = unit(2, "mm"),
                      height = unit(0.7, 'cm')
                      #axis = F
                      ),
  test3 = anno_lines(cancer_normal_samples_info$test3,
                     height = unit(0.7, 'cm')
                    # axis = F
                     ),
  test4 = anno_barplot(cancer_normal_samples_info$test4,
                       height = unit(0.7, 'cm'),
                       axis_param = list(
                         at = c(0, 50, 100),
                         labels = c('0', '50', '100'),
                         gp = gpar(fontsize = 7)
                       )),
  col = list(
    group = c(Cancer = '#fc8d59', Normal = '#99d594'),
    test1 = colorRamp2(
      breaks = c(20, 50, 80),
      colors = c('white', '#E38169', '#E9593F')
    )
  ),
  annotation_name_gp = gpar(fontsize = 8, fontface = 'italic')
 # simple_anno_size = unit(0.02, 'npc')
)

## ----------------------------------------------------
Heatmap(top100_de[1:20,],
        show_column_names = F,
        name = "Expression",
        column_title = "Gene Expression Heatmap",
        column_title_side = "top",
        col = colorRamp2(
          breaks = c(0, 10, 20),
          colors = c('green','white', 'red')
        ),
        border = 'white',
        rect_gp = gpar(col = 'white', lwd = 1),
        row_names_gp = gpar(fontsize = 6, 
                            fontface = 'italic'),
        column_title_gp = gpar(fontface = 'bold'),
        column_km = 3,
        column_gap = unit(0.03, 'npc'),
        # 在下方添加列注释
     top_annotation = column_ha
)
image.png

3.8 添加重点基因注释

set.seed(123)
key_genes <- sample(rownames(top100_de), size = 20, replace = F)


## ----------------------------------------------------
row_ha <- rowAnnotation(
  key_genes = anno_mark(which(rownames(top100_de) %in% key_genes),
                        labels = key_genes,
                        labels_gp = gpar(fontsize = 6))
)


## ----------------------------------------------------
Heatmap(top100_de,
        show_column_names = F,
        name = "Expression",
        column_title = "Gene Expression Heatmap",
        column_title_side = "top",
        col = colorRamp2(
          breaks = c(0, 10, 20),
          colors = c('green','white', 'red')
        ),
        border = 'white',
        rect_gp = gpar(col = 'white', lwd = 1),
        row_names_gp = gpar(fontsize = 8, fontface = 'italic'),
        column_title_gp = gpar(fontface = 'bold'),
        column_km = 2,
        column_gap = unit(0.03, 'npc'),
        # 在下方添加列注释
        bottom_annotation = column_ha,
        # 在右侧添加行注释
        right_annotation = row_ha,
        show_row_names = F
)

image.png

相关文章

网友评论

      本文标题:ComplexHeatmap

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