Jump to content

File:Annotation legend param.png

From 太極

Annotation_legend_param.png (777 × 442 pixels, file size: 17 KB, MIME type: image/png)

Summary

library(RColorBrewer)
library(ComplexHeatmap)

set.seed(123)
n <- 100
df <- data.frame(
  Subtype = sample(c("Hyperdiploid", "Ph-like", "DUX4", "Ph"), n, replace = TRUE),
  Sex = sample(c("Male", "Female"), n, replace = TRUE),
  Age_Group = sample(c("Childhood", "Adult"), n, replace = TRUE)
)

# 1. Define distinct palettes
# Subtype: Using "Set3" or "Paired" for many categories
subtype_cols <- setNames(
  colorRampPalette(brewer.pal(12, "Paired"))(length(unique(df$Subtype))),
  unique(df$Subtype)
)

# Sex: Using high-contrast "Set1" (often Pink/Blue or Red/Blue)
sex_cols <- c("Female" = "#e41a1c", "Male" = "#377eb8")

# Age Group: Using "Set2" (soft but professional)
age_cols <- c("Childhood" = "#66c2a5", "Adult" = "#fc8d62", "NA" = "#d9d9d9")

# 2. Combine into the col_list
col_list = list(
  Subtype = subtype_cols,
  Sex = sex_cols,
  Age_Group = age_cols
)

# 3. Apply to Annotation
ha = HeatmapAnnotation(
      df = df, 
      col = col_list,
      annotation_legend_param = list(
          Subtype = list(
              nrow = 2, # Force Subtype legend into 2 rows if it has many levels
              direction = "horizontal"
          ),
          Sex = list(
              nrow = 1, 
              direction = "horizontal"
          )
      )
  )
 
ht = Heatmap(matrix(nc = 100, nr = 0), top_annotation = ha)
draw(ht, annotation_legend_side = "bottom")

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current10:08, 22 December 2025Thumbnail for version as of 10:08, 22 December 2025777 × 442 (17 KB)Brb (talk | contribs)<syntaxhighlight lang='r'> library(RColorBrewer) library(ComplexHeatmap) set.seed(123) n <- 100 df <- data.frame( Subtype = sample(c("Hyperdiploid", "Ph-like", "DUX4", "Ph"), n, replace = TRUE), Sex = sample(c("Male", "Female"), n, replace = TRUE), Age_Group = sample(c("Childhood", "Adult"), n, replace = TRUE) ) # 1. Define distinct palettes # Subtype: Using "Set3" or "Paired" for many categories subtype_cols <- setNames( colorRampPalette(brewer.pal(12, "Paired"))(length(unique(df$S...

The following page uses this file: