Server Info & Course Info
https://jokergoo.github.io/ComplexHeatmap-reference/book/
# Create sample matrix
set.seed(123)
mat <- matrix(rnorm(100), nrow=10,
dimnames = list(paste0("Gene",1:10),
paste0("Sample",1:10)))
# Basic heatmap
heatmap(mat,
col = heat.colors(256),
scale = "row",
main = "Basic Heatmap Example")
col: Color palettescale: “none”/”row”/”column”Rowv/Colv: Clustering (TRUE/FALSE)margins: Plot marginscexRow/cexCol: Label sizes#install.packages("BiocManager")
#BiocManager::install("ComplexHeatmap")
library(ComplexHeatmap)
library(circlize) # For color gradients
# Simulate gene expression data
expr_mat <- matrix(rnorm(50*20), nrow=50)
rownames(expr_mat) <- paste0("Gene", 1:50)
colnames(expr_mat) <- paste0("Patient", 1:20)
# Add simple row annotations
gene_type <- sample(c("TypeA","TypeB"), 50, replace=TRUE)
row_ha <- rowAnnotation(Type = gene_type)
Heatmap(expr_mat,
name = "Expression",
col = colorRamp2(c(-2, 0, 2),
c("blue", "white", "red")),
top_annotation = HeatmapAnnotation(
Bar = anno_barplot(colMeans(expr_mat))),
left_annotation = row_ha,
show_row_names = FALSE)
# Create clinical metadata
clinical_data <- data.frame(
Age = rnorm(20, 50, 5),
Stage = sample(c("I","II","III"), 20, replace=TRUE)
)
col_ha <- HeatmapAnnotation(
df = clinical_data,
col = list(Stage = c("I" = "green",
"II" = "yellow",
"III" = "red")))
Heatmap(expr_mat,
name = "Expression",
top_annotation = col_ha,
column_title = "Patient Samples with Clinical Data",
show_row_names = FALSE)
# Create dummy data
methyl_mat <- matrix(runif(50*20), nrow=50)
expr_mat2 <- matrix(rnorm(50*20), nrow=50)
ht1 <- Heatmap(methyl_mat,
name = "Methylation",
col = colorRamp2(c(0,1),
c("white", "purple")))
ht2 <- Heatmap(expr_mat2,
name = "Expression",
col = colorRamp2(c(-2,0,2),
c("blue","white","red")))
ht1 + ht2
name= parameter)show_heatmap_legend to control legendsanno_* functions for annotationspdf("plot.pdf"); draw(ht); dev.off()Heatmap(expr_mat,
name = "Expression",
top_annotation = col_ha,
left_annotation = row_ha,
column_split = clinical_data$Stage,
row_title = "Gene Clusters",
column_names_rot = 45)
?Heatmap
browseVignettes("ComplexHeatmap")
Zero to Hero in R Visualization
Two approaches for beginners:
# Method 1: Create test data
fake_data <- matrix(rnorm(100), ncol=10)
# Method 2: Read CSV file
real_data <- read.csv("your_data.csv")
Always start with small test data before using real datasets!
Let’s make playful sample data:
# Create a 10x10 matrix with random numbers
set.seed(123)
play_matrix <- matrix(rnorm(100), ncol=10,
dimnames = list(paste0("Gene",1:10),
paste0("Sample",1:10)))
Why 123? It makes random numbers repeatable!
Simple CSV import demonstration:
# Create sample CSV file first
write.csv(play_matrix, "demo_data.csv")
# Now read it back
my_data <- read.csv("demo_data.csv", row.names=1)
Protip: Always check first 3 lines with head(my_data, 3)
Basic template to remember:
your_matrix=play_matrix
library(ComplexHeatmap)
Heatmap(your_matrix,
name = "Legend Title",
col = colorRamp2(c(-1, 0, 1), c("green", "white", "red")),
show_row_names = FALSE)
Three key parameters control 80% of basic plots
What’s this magic?
# Without name parameter
Heatmap(play_matrix)
# With name parameter
Heatmap(play_matrix, name = "Expression Level")
Try both versions to see the legend title difference!
Make rainbow heatmaps:
# Create color gradient
my_colors <- colorRamp2(c(-2, 0, 2),
c("blue", "white", "red"))
# Apply colors
Heatmap(play_matrix, name = "Fun Colors",
col = my_colors)
colorRamp2() needs at least 2 color stops
When labels get messy:
# Original version
Heatmap(play_matrix)
# Clean version
Heatmap(play_matrix,
show_row_names = FALSE,
show_column_names = TRUE)
Perfect for data with many rows!
Combine all we learned:
set.seed(123)
final_data <- matrix(rnorm(50), ncol=5)
Heatmap(final_data,
name = "Demo Values",
col = colorRamp2(c(-2,0,2),
c("green","black","yellow")),
show_row_names = FALSE,
column_names_side = "top")
Now you’re ready for basic heatmaps!
Add extra information to rows/columns using:
Example use cases:
# Create simple group annotation
ha <- HeatmapAnnotation(
group = c(rep("Control",5), rep("Case",5))
)
# Draw heatmap with annotation
Heatmap(matrix(rnorm(100), ncol=10), top_annotation = ha)
Create age trends with points:
ha <- HeatmapAnnotation(
group = c(rep("Control",5), rep("Case",5)),
age = anno_points(runif(10)) # Random values 0-1
)
Heatmap(matrix(rnorm(100), ncol=10), top_annotation = ha)
Parameters:
runif(10) generates 10 random numbersanno_points() creates point plot annotationAdd gene type information:
row_ha <- rowAnnotation(
gene_type = c(rep("Protein", 30), rep("RNA", 20))
)
Heatmap(matrix(rnorm(1000), nrow=50),
left_annotation = row_ha)
Features:
Highlight important genes:
row_ha <- rowAnnotation(
mark = anno_mark(
at = c(5, 15, 25), # Row positions
labels = c("GeneA", "GeneB", "GeneC"))
)
Heatmap(matrix(rnorm(100), nrow=25),
left_annotation = row_ha)
Effect: Shows labeled markers next to heatmap
Create visual separation:
Heatmap(matrix(rnorm(100), ncol=10),
column_split = c(rep("A",5), rep("B",5)))
Parameters:
Automated grouping:
ht=Heatmap(matrix(rnorm(1000), nrow=50),
row_km = 3) # Create 3 clusters
ht_out = draw(ht)
row_order(ht_out)
Features:
ha <- HeatmapAnnotation(
group = rep(c("Young","Old"), each=5),
age = anno_points(20:29))
row_ha <- rowAnnotation(
type = sample(c("A","B"), 50, replace=TRUE))
Heatmap(matrix(rnorm(500), nrow=50),
top_annotation = ha,
left_annotation = row_ha,
column_split = rep(1:2, each=5),
row_km = 2)
| Column Annotations | Row Annotations | Splitting |
|---|---|---|
top_annotation |
left_annotation |
column_split |
anno_points() |
anno_mark() |
row_km |
column_names_side |
row_names_side |
cluster_rows |
Remember: Type ?anno_points to see all options!
Let’s create a simple gene expression matrix:
# Create sample data
expr_matrix <- matrix(rnorm(100), nrow=10,
dimnames=list(paste0("Gene",1:10),
paste0("Patient",1:10)))
clinical_stage <- data.frame(
Patient=paste0("Patient",1:10),
Stage=sample(c("I","II","III"), 10, replace=TRUE)
)
library(ComplexHeatmap)
# Create color mapping
stage_colors <- c("I"="blue", "II"="green", "III"="red")
# Create top annotation
top_anno <- HeatmapAnnotation(
Stage=clinical_stage$Stage,
col=list(Stage=stage_colors)
)
Heatmap(expr_matrix, name="Expression",
top_annotation=top_anno)
# Add survival data
survival_status <- sample(c(0,1), 10, replace=TRUE)
# Create bottom annotation
bottom_anno <- HeatmapAnnotation(
Survival=survival_status,
col=list(Survival=c("0"="gray", "1"="black"))
)
Heatmap(expr_matrix, name="Expression",
top_annotation=top_anno,
bottom_annotation=bottom_anno)
# Create p-value matrix
p_values <- matrix(runif(100), 10)
signif_stars <- ifelse(p_values < 0.05, "*", "")
Heatmap(expr_matrix, name="Expression",
cell_fun=function(j, i, x, y, w, h, col) {
grid.text(signif_stars[i,j], x, y)
})
Prepare methylation data:
methyl_matrix <- matrix(runif(100), nrow=10,
dimnames=list(paste0("Gene",1:10),
paste0("Sample",1:10)))
ht1 <- Heatmap(expr_matrix, name="Expression",
column_title="Gene Expression")
ht2 <- Heatmap(methyl_matrix, name="Methylation",
column_title="DNA Methylation",
col=colorRamp2(c(0,1), c("white", "purple")))
ht1 %v% ht2 # Simple vertical combination
draw(ht1 %v% ht2,
heatmap_legend_side="right",
annotation_legend_side="left")
%v% operator enables vertical layoutReady for hands-on practice! 🚀
Bad default plot:
heatmap(matrix(rnorm(100), nrow=10))
Problems:
library(ComplexHeatmap)
basic_heatmap <- Heatmap(
matrix(rnorm(100),nrow=10),
name = "values")
draw(basic_heatmap)
Make heatmap 8 inches wide:
adjusted_heatmap <- Heatmap(
matrix(rnorm(100),nrow=10),
name = "values",
row_title= 'row_title',
width = unit(2, "in"),
height = unit(2, "in")
)
draw(adjusted_heatmap)
Rotate title 45 degrees:
rotated_heatmap <- Heatmap(
matrix(rnorm(100),nrow=10),
name = "values",
row_title= 'row_title',
row_title_rot = 45,
width = unit(2, "in"),
height = unit(2, "in")
)
draw(rotated_heatmap)
final_heatmap <- Heatmap(
matrix(rnorm(100),nrow=10),
name = "My Data",
width = unit(2, "cm"),
height = unit(2, "cm"),
row_title = "Genes",
row_title_rot = 30,
column_title = "Samples"
)
draw(final_heatmap)
Save to 8x8 inch PDF:
pdf("my_plot.pdf", width=8, height=8)
draw(final_heatmap)
dev.off()
300 dpi export:
png("heatmap.png",
width=2400,
height=1600,
res=300)
draw(final_heatmap)
dev.off()
Compare outputs:
Key parameters:
width: Controls overall plot size*_title_rot: Rotates text labelsname: Sets legend titleExport commands:
pdf(), png(), tiff()dev.off()Combine what we learned:
custom_plot <- Heatmap(
matrix(rnorm(400), nrow=20),
name = "Expression",
width = unit(15, "cm"),
row_title_rot = 60,
column_title = "Patient Cells"
)
pdf("final_figure.pdf", width=10, height=7)
draw(custom_plot)
dev.off()
dev.off()Always: