automatically categorize and add annotations in r using pheatmap

You need to use annotation_col option in pheatmap.

library(pheatmap)

# split matrix into "Name" and "Gender"
name_gender_matrix <- str_split_fixed(colnames(matrix), "_", 2)

# Data that maps to the heatmap should be set at the rownames
annot_col <- data.frame(row.names = name_gender_matrix[, 1], Gender = name_gender_matrix[, 2])

# Align the column name of your matrix and with the annotation
colnames(matrix) <- rownames(annot_col)

heatmap <- pheatmap(
  mat = matrix,
  cluster_rows = F,
  cluster_cols = F,  
  cellwidth = 30,
  cellheight = 30,
  annotation_col = annot_col
)

pheatmap_with_gender

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top