Last active
September 5, 2022 06:44
-
-
Save tuhulab/099cb241be63859b7ede18a62e7bbd39 to your computer and use it in GitHub Desktop.
Plot a complex heatmap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ComplexHeatmap) | |
count_matrix # if your data is called count_matrix | |
# Read data | |
se <- readRDS("data/se.rds") | |
count_matrix <- assay(se) | |
# Data transformation | |
count_matrix_log2 <- log2(count_matrix + 1) | |
count_matrix_scale <- | |
count_matrix_log2 %>% t() %>% scale(center = TRUE, scale = TRUE) %>% t() # This scaling step is easy for an error. Be careful, you are scaling "rows" or "columns". | |
# Define annotation | |
sample_site_color <- | |
colData(se) %>% as_tibble() %>% pull(biopsy_area) %>% unique() | |
color <- RColorBrewer::brewer.pal(length(sample_site_color), "Pastel2") | |
names(color) <- sample_site_color | |
gene_theme_color <- RColorBrewer::brewer.pal(length(top100_g$theme), "Accent") | |
names(gene_theme_color) <- top100_g$theme %>% unique() | |
heatmap_annotation <- | |
HeatmapAnnotation( | |
`Tissue type` = colData(se) %>% as_tibble() %>% pull(skin_type), | |
`Anatomic region` = colData(se) %>% as_tibble() %>% pull(biopsy_area), | |
col = list(`Tissue type` = | |
c("LS" = "#eb2d0c", "NL" = "#eb8b9b", "HC" = "#91cf60"), | |
`Anatomic region` = color), | |
annotation_legend_param = | |
list(`Tissue type` = list(nrow = 1, title_gp = gpar(fontsize = 12), | |
labels_gp = gpar(fontsize = 10)), | |
`Anatomic region` = list(nrow = 1, title_gp = gpar(fontsize = 12), | |
labels_gp = gpar(fontsize = 10)) | |
)) | |
# Main body of heatmap | |
heatmap_top100 <- ComplexHeatmap::Heatmap(count_matrix, | |
name = "gene expression", | |
column_dend_reorder = TRUE, | |
# clustering_method_columns = "complete", | |
show_column_names = FALSE, | |
show_row_names = TRUE, | |
show_row_dend = FALSE, | |
row_names_gp = gpar(fontsize = 9), | |
top_annotation = heatmap_annotation, | |
heatmap_legend_param = list( | |
title_gp = gpar(fontsize = 12), | |
labels_gp = gpar(fontsize = 10) | |
), | |
column_split = se_heatmap_top100$skin_type, | |
row_split = top100_g$theme, | |
row_title = gt_render( | |
c("Noncoding<br>RNA", | |
"Skin<br>barrier<br>and<br>physiology", | |
"Immune<br>response", | |
"G protein<br>coupled<br>receptor") | |
), | |
row_title_rot = 0, | |
row_title_side = "right", | |
row_title_gp = gpar(fill = gene_theme_color)) | |
draw(heatmap_top100, heatmap_legend_side = "right", | |
annotation_legend_side = "top", merge_legend = FALSE) | |
## Save your heatmap | |
png("data/supplementary/figure_1b.png", width = 480*6.5, height = 480*6, res=300) | |
draw(heatmap_top100, heatmap_legend_side = "right", | |
annotation_legend_side = "top", merge_legend = FALSE) | |
dev.off() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment