热图

作者: 可能性之兽 | 来源:发表于2024-05-14 13:48 被阅读0次
# 安装并加载必要的R包
# install.packages("ComplexHeatmap")
# install.packages("circlize")
# install.packages("RColorBrewer")

library(ComplexHeatmap)
library(circlize)
library(RColorBrewer)

# 示例数据
set.seed(123)
data_matrix <- matrix(rnorm(200), nrow = 20)
rownames(data_matrix) <- paste0("Gene", 1:20)
colnames(data_matrix) <- paste0("Sample", 1:10)

# 示例注释数据
row_annotation <- data.frame(
  Pattern = factor(rep(1:4, each = 5)),
  Cluster = factor(rep(c("EPI", "NPP", "NCPLC", "PPLC"), each = 5))
)
col_annotation <- data.frame(
  Fate = factor(rep(c("Placode", "Neural Crest"), each = 5))
)

# 行注释
ha_row <- rowAnnotation(
  Pattern = row_annotation$Pattern,
  Cluster = row_annotation$Cluster,
  col = list(
    Pattern = setNames(brewer.pal(4, "Set1"), levels(row_annotation$Pattern)),
    Cluster = setNames(brewer.pal(4, "Dark2"), levels(row_annotation$Cluster))
  )
)

# 列注释
ha_col <- HeatmapAnnotation(
  Fate = col_annotation$Fate,
  col = list(Fate = c("Placode" = "lightblue", "Neural Crest" = "lightpink"))
)

# 绘制热图
P1=Heatmap(
  data_matrix,
  name = "Expression",
  col = colorRamp2(c(-2, 0, 2), c("blue", "white", "red")),
  top_annotation = ha_col,
  left_annotation = ha_row,
  show_row_names = TRUE,
  show_column_names = TRUE,
  cluster_rows = FALSE,
  cluster_columns = FALSE,
  row_title = "Patterns",
  column_title = "Samples",
  row_split = row_annotation$Pattern,
  column_split = col_annotation$Fate
)

image.png

可以对类别单独聚类的热图,比如对TypeA,TypeB,TypeC单独聚类

library(pheatmap)

create_clustered_heatmap <- function(gene_data, gene_info, color_scheme = NULL) {
  # 确保 gene_info 中有 Type 列
  if (!"Type" %in% colnames(gene_info)) {
    stop("gene_info must contain a 'Type' column")
  }

  # 如果没有提供 color_scheme,则自动生成
  if (is.null(color_scheme)) {
    unique_types <- unique(gene_info$Type)
    color_scheme <- setNames(rainbow(length(unique_types)), unique_types)
  }

  # 计算每种类型的基因数量
  gene_type_counts <- table(gene_info$Type)
  cumulative_gene_counts <- cumsum(gene_type_counts)

  # 对数据进行缩放,限制在 -3 到 3 之间
  scaled_data <- t(apply(gene_data, 1, scale))
  scaled_data <- pmin(pmax(scaled_data, -3), 3)
  colnames(scaled_data) <- colnames(gene_data)

  # 创建行注释数据框
  row_annotations <- data.frame(Type = gene_info$Type)
  rownames(row_annotations) <- rownames(gene_data)

  # 设置注释颜色
  annotation_color_scheme <- list(Type = color_scheme)

  # 按类别分组并在每个组内聚类
  ordered_gene_indices <- c()
  for (gene_type in names(gene_type_counts)) {
    type_indices <- which(gene_info$Type == gene_type)
    type_data <- scaled_data[type_indices, ]
    type_distances <- dist(type_data)
    type_clustering <- hclust(type_distances)
    type_order <- type_indices[type_clustering$order]
    ordered_gene_indices <- c(ordered_gene_indices, type_order)
  }

  # 重新排序数据和注释
  scaled_data <- scaled_data[ordered_gene_indices, ]
  row_annotations <- row_annotations[ordered_gene_indices, , drop = FALSE]

  # 创建热图
  heatmap_plot <- pheatmap(
    scaled_data,
    scale = "none",
    cluster_cols = FALSE,
    cluster_rows = FALSE,
    show_rownames = FALSE,
    show_colnames = TRUE,
    gaps_row = cumulative_gene_counts[-length(cumulative_gene_counts)],
    color = colorRampPalette(c("navy", "white", "firebrick3"))(100),
    breaks = seq(-3, 3, length.out = 100),
    annotation_row = row_annotations,
    annotation_colors = annotation_color_scheme,
    annotation_names_row = FALSE,
    annotation_legend = TRUE,
    main = "Clustered Heatmap of Gene Types",
    fontsize_col = 8,
    angle_col = 90
  )
  
  return(heatmap_plot)
}

# 示例数据
set.seed(123)
example_gene_data <- matrix(rnorm(1000), nrow = 200, ncol = 10)
colnames(example_gene_data) <- paste0("Sample_", 1:10)
rownames(example_gene_data) <- paste0("Gene_", 1:200)

example_gene_info <- data.frame(
  Type = sample(c("TypeA", "TypeB", "TypeC"), 200, replace = TRUE),
  row.names = rownames(example_gene_data)
)

# 使用示例
# 可以选择提供自定义的颜色方案,或让函数自动生成
custom_color_scheme <- c("TypeA" = "#C71000FF", "TypeB" = "#008EA0FF", "TypeC" = "#8A4198FF")

result_heatmap <- create_clustered_heatmap(example_gene_data, example_gene_info, custom_color_scheme)
print(result_heatmap)

image.png

相关文章

  • complexheatmap学习3——热图拼接

    最基本的例子 标题 热图的大小 热图之间的空隙 基于主要的热图自动调整 主要热图的设置 注释部分的调整 热图和注释...

  • 热图

    R绘图基础(四)热图 heatmap:https://qiubio.com/archives/2477 那些年画过...

  • 热图

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

  • 热图

    Create test matrix Draw heatmaps Show text within cells F...

  • 【热图】

    2021.4.28持续更新中。。。 参考:《R数据可视化手册》、学术数据分析及可视化[https://space....

  • 热图

    有点小瑕疵 参考:木舟笔记

  • 热图

    静态与交互式热图: heatmap():用于绘制简单热图的函数heatmap.2():绘制增强热图的函数d3hea...

  • 热图

    成品图: 核心要点: 左边和上面的分组以及最主要的核心数据 所需要的数据: 原始数据(属水平的相对丰度表) 目的:...

  • 1 初识Complexheatmap

    ComplexHeatmap绘制的热图种类分为: ① Heatmap 类:单个热图,包括热图主体、行/列名称、标题...

  • pheatmap包学习

    pheatmap pheatmap热图绘制 加载所需要的包 读入数据 查看数据 最初原始的热图绘制 热图颜色的调整...

网友评论

      本文标题:热图

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