# 安装并加载必要的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
)
![](https://img.haomeiwen.com/i23627621/b8cc8eeda06aba0e.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)
![](https://img.haomeiwen.com/i23627621/fcf0493a1290ed7d.png)
网友评论