You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为pheatmap生成的热图添加行颜色及对应行数据图例

Add Row Colors & Row-Based Legend to Pheatmap in R

Hey there! Let's fix up your pheatmap to include row-specific colors and a legend that only reflects your row data. Here are two common approaches depending on whether you're working with categorical row groups or continuous row values:

Approach 1: Categorical Row Groups (e.g., predefined gene clusters)

If you have distinct groups for your rows (like different gene sets), you can map each group to a unique color and display a legend for these groups:

library(pheatmap)

# Generate your test data (same as your original code)
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")

# Step 1: Create a data frame defining row groups (adjust groups to match your actual data)
row_groups <- data.frame(Gene_Cluster = factor(
  c(rep("Cluster 1", 10), rep("Cluster 2", 4), rep("Cluster 3", 6))
))
rownames(row_groups) <- rownames(test)  # Match row names to your test matrix

# Step 2: Define custom colors for each row cluster
cluster_colors <- list(Gene_Cluster = c(
  "Cluster 1" = "#F8766D", 
  "Cluster 2" = "#00BA38", 
  "Cluster 3" = "#619CFF"
))

# Step 3: Draw the heatmap with row annotations and legend
pheatmap(test,
         annotation_row = row_groups,  # Attach row group annotations
         annotation_colors = cluster_colors,  # Assign colors to each group
         show_annotation_legend = TRUE,  # Ensure the row legend is visible
         annotation_legend = TRUE)  # Confirm legend is enabled

What this does:

  • annotation_row passes your row group data to the heatmap, which adds color bars next to each row.
  • annotation_colors lets you customize exactly which color maps to each group, so the legend only reflects these row-based categories.

Approach 2: Continuous Row Values (e.g., row means)

If you want row colors based on a numerical value from your row data (like average expression per gene), use a continuous color scale:

# Step 1: Calculate a numerical metric for each row (e.g., row mean expression)
row_expression_means <- rowMeans(test)

# Step 2: Create a data frame for the continuous annotation
row_cont_annot <- data.frame(Avg_Expression = row_expression_means)
rownames(row_cont_annot) <- rownames(test)

# Step 3: Draw the heatmap with continuous row colors
pheatmap(test,
         annotation_row = row_cont_annot,
         # Use a color ramp for continuous values (adjust colors to your preference)
         annotation_colors = list(Avg_Expression = colorRampPalette(c("navy", "white", "firebrick3"))(100)),
         show_annotation_legend = TRUE)

What this does:

  • The row colors now reflect the average expression of each gene, and the legend shows the full range of this continuous value.
  • colorRampPalette creates a smooth gradient between your chosen start/end colors.

Either way, the legend will be tied exclusively to your row data, just like you wanted!

内容的提问来源于stack exchange,提问作者Julie Hardy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 09:12:47