This is a test version of Biostars. For the public version, visit https://www.biostars.org.
RColorBrewer for annotations in ComplexHeatmap

Hello Biostar,

I wish to change the colors of a row annotation on a heatmap. I use ComplexHeatmap and would like to use RColorBrewer for colors which will map a vector with continuous values. I have difficulties to create a list of colors and and to have it map the rows of my mat on the heatmap.

A quick example would be much appreciated.

Best,

Franc


EDIT :

Example code:

set.seed(123)
mat <- data.frame(matrix(rnorm(100), 10), row.names = sample(letters, 10, replace = F))
base <- matrix(abs(rnorm(10,mean=10)), ncol = 1)
rownames(base) <- rownames(mat)
row_annot <- data.frame(Expression = log2(base))
Var <- setNames(hcl.colors(nrow(row_annot),"Heat"), rownames(mat))
row_ha <- HeatmapAnnotation(df = row_annot,
                            col = list(Expression = Var),
                            which = "row",
                            show_annotation_name = F,
                            annotation_name_side = "top",
                            annotation_legend_param = list()
)
scaled_mat <- t(scale(t(mat), center = T))
Heatmap(scaled_mat, name = "mat", right_annotation = row_ha)

sessionInfo()

R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

attached base packages:
 [1] grid      parallel  stats4    stats     graphics  grDevices utils     datasets  methods  
[10] base     

other attached packages:
 [1] GOSemSim_2.10.0             dplyr_0.8.3                 ComplexHeatmap_2.0.0       
 [4] viridis_0.5.1               viridisLite_0.3.0           PANTHER.db_1.0.4           
 [7] RSQLite_2.1.2               stringr_1.4.0               colorRamps_2.3             
[10] VennDiagram_1.6.20          futile.logger_1.4.3         clusterProfiler_3.12.0     
[13] reactome.db_1.68.0          org.Mm.eg.db_3.8.2          ggrepel_0.8.1              
[16] data.table_1.12.2           gageData_2.22.0             pathview_1.24.0            
[19] org.Hs.eg.db_3.8.2          AnnotationDbi_1.46.1        gage_2.34.0                
[22] biomaRt_2.40.4              ggplot2_3.2.1               pheatmap_1.0.12            
[25] RColorBrewer_1.1-2          gplots_3.0.1.1              DESeq2_1.24.0              
[28] SummarizedExperiment_1.14.1 DelayedArray_0.10.0         BiocParallel_1.18.1        
[31] matrixStats_0.55.0          Biobase_2.44.0              GenomicRanges_1.36.1       
[34] GenomeInfoDb_1.20.0         IRanges_2.18.2              S4Vectors_0.22.1           
[37] BiocGenerics_0.30.0        

loaded via a namespace (and not attached):
  [1] circlize_0.4.8         backports_1.1.4        Hmisc_4.2-0           
  [4] fastmatch_1.1-0        plyr_1.8.4             igraph_1.2.4.1        
  [7] lazyeval_0.2.2         splines_3.6.1          urltools_1.7.3        
 [10] digest_0.6.20          htmltools_0.3.6        GO.db_3.8.2           
 [13] gdata_2.18.0           magrittr_1.5           checkmate_1.9.4       
 [16] memoise_1.1.0          cluster_2.1.0          Biostrings_2.52.0     
 [19] annotate_1.62.0        graphlayouts_0.5.0     enrichplot_1.4.0      
 [22] prettyunits_1.0.2      colorspace_1.4-1       blob_1.2.0            
 [25] xfun_0.9               crayon_1.3.4           RCurl_1.95-4.12       
 [28] jsonlite_1.6           graph_1.62.0           genefilter_1.66.0     
 [31] zeallot_0.1.0          survival_2.44-1.1      glue_1.3.1            
 [34] polyclip_1.10-0        gtable_0.3.0           zlibbioc_1.30.0       
 [37] XVector_0.24.0         UpSetR_1.4.0           GetoptLong_0.1.7      
 [40] Rgraphviz_2.28.0       shape_1.4.4            scales_1.0.0          
 [43] DOSE_3.10.2            futile.options_1.0.1   DBI_1.0.0             
 [46] Rcpp_1.0.2             xtable_1.8-4           progress_1.2.2        
 [49] htmlTable_1.13.1       clue_0.3-57            gridGraphics_0.4-1    
 [52] foreign_0.8-72         bit_1.1-14             europepmc_0.3         
 [55] Formula_1.2-3          htmlwidgets_1.3        httr_1.4.1            
 [58] fgsea_1.10.1           acepack_1.4.1          pkgconfig_2.0.2       
 [61] XML_3.98-1.20          farver_1.1.0           nnet_7.3-12           
 [64] locfit_1.5-9.1         labeling_0.3           ggplotify_0.0.4       
 [67] tidyselect_0.2.5       rlang_0.4.0            reshape2_1.4.3        
 [70] munsell_0.5.0          tools_3.6.1            ggridges_0.5.1        
 [73] knitr_1.24             bit64_0.9-7            tidygraph_1.1.2       
 [76] caTools_1.17.1.2       purrr_0.3.2            KEGGREST_1.24.0       
 [79] ggraph_2.0.0           formatR_1.7            KEGGgraph_1.44.0      
 [82] DO.db_2.9              xml2_1.2.2             compiler_3.6.1        
 [85] rstudioapi_0.10        curl_4.0               png_0.1-7             
 [88] tibble_2.1.3           tweenr_1.0.1           geneplotter_1.62.0    
 [91] stringi_1.4.3          lattice_0.20-38        Matrix_1.2-17         
 [94] vctrs_0.2.0            pillar_1.4.2           lifecycle_0.1.0       
 [97] BiocManager_1.30.4     GlobalOptions_0.1.0    triebeard_0.3.0       
[100] cowplot_1.0.0          bitops_1.0-6           qvalue_2.16.0         
[103] R6_2.4.0               latticeExtra_0.6-28    KernSmooth_2.23-15    
[106] gridExtra_2.3          lambda.r_1.2.3         MASS_7.3-51.4         
[109] gtools_3.8.1           assertthat_0.2.1       rjson_0.2.20          
[112] withr_2.1.2            GenomeInfoDbData_1.2.1 hms_0.5.1             
[115] rpart_4.1-15           tidyr_1.0.0            rvcheck_0.1.3         
[118] ggforce_0.3.1          base64enc_0.1-3
rna-seq r complexheatmap

You could pass in a vector of colors to the col parameter. See: https://jokergoo.github.io/ComplexHeatmap-reference/book/heatmap-annotations.html#simple-annotation

col = RColorBrewer::brewer.pal(name = "Blues", n = 9)

should give you 9 colors, for example. If you need more colors than any of RColorBrewer's palettes can offer you, you can either use colorRamp* functions to interpolate colors (only makes sense for continuous/sequential values) or choose a different color package. See here on more information about interpolating colors.

Thanks for your help RamRS. I think that wouldn't work because the col argument of HeatmapAnnotation requires a named list of color not a vector.

There exists a function called setNames that can be helpful here:

setNames(RColorBrewer::brewer.pal(name = "Blues", n = 7), c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))
  Sunday    Monday   Tuesday Wednesday  Thursday    Friday  Saturday
"#EFF3FF" "#C6DBEF" "#9ECAE1" "#6BAED6" "#4292C6" "#2171B5" "#084594"

Of course, you can also get a named list instead of a named vector:

 setNames(RColorBrewer::brewer.pal(name = "Blues", n = 7), list("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))

Thanks RamRS. Sure but then, I have many more than 7 rows.

That was an example - it can be expanded to as many elements as you need. All that matters is that the first and second arguments have the same length (excluding possible special cases).

Yes I see. Nevertheless, I think the HeatmapAnnotation method has a bug because upon testing your example, I get the following error: Error: elements in col should be named vectors.

Can you please paste your code so we can have a look at what's going on?

Please use "edit" to edit your posts with more details, do not use "answer" box.

4 answers

Thanks again for your patience and help RamRS. Got my answer here: https://github.com/jokergoo/ComplexHeatmap/issues/359#issuecomment-531669275

Appreciate the update but please mention (or even better, avoid) cross-posting in the future. It annoys people in multiple communities.

Ideally, your question belonged on Bioconductor support as you’re seeking help with a Bioconductor package. We here are ok dealing with those questions too. GitHub is not the place to ask questions - it is for filing genuine bug reports and contributing to the source code.

Thanks RamRS. I will be sure to post my future inquiries to the best group.

Thanks for your help RamRS. I think that wouldn't work because the col argument of HeatmapAnnotation requires a named list of color not a vector.

set.seed(123)
mat <- data.frame(matrix(rnorm(100), 10), row.names = sample(letters, 10, replace = F))
base <- matrix(abs(rnorm(10,mean=10)), ncol = 1)
rownames(base) <- rownames(mat)
row_annot <- data.frame(Expression = log2(base))
Var <- setNames(hcl.colors(nrow(row_annot),"Heat"), rownames(mat))
row_ha <- HeatmapAnnotation(df = row_annot,
                            col = list(Expression = Var),
                            which = "row",
                            show_annotation_name = F,
                            annotation_name_side = "top",
                            annotation_legend_param = list()
)
scaled_mat <- t(scale(t(mat), center = T))
Heatmap(scaled_mat, name = "mat", right_annotation = row_ha)
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

attached base packages:
 [1] grid      parallel  stats4    stats     graphics  grDevices utils     datasets  methods  
[10] base     

other attached packages:
 [1] GOSemSim_2.10.0             dplyr_0.8.3                 ComplexHeatmap_2.0.0       
 [4] viridis_0.5.1               viridisLite_0.3.0           PANTHER.db_1.0.4           
 [7] RSQLite_2.1.2               stringr_1.4.0               colorRamps_2.3             
[10] VennDiagram_1.6.20          futile.logger_1.4.3         clusterProfiler_3.12.0     
[13] reactome.db_1.68.0          org.Mm.eg.db_3.8.2          ggrepel_0.8.1              
[16] data.table_1.12.2           gageData_2.22.0             pathview_1.24.0            
[19] org.Hs.eg.db_3.8.2          AnnotationDbi_1.46.1        gage_2.34.0                
[22] biomaRt_2.40.4              ggplot2_3.2.1               pheatmap_1.0.12            
[25] RColorBrewer_1.1-2          gplots_3.0.1.1              DESeq2_1.24.0              
[28] SummarizedExperiment_1.14.1 DelayedArray_0.10.0         BiocParallel_1.18.1        
[31] matrixStats_0.55.0          Biobase_2.44.0              GenomicRanges_1.36.1       
[34] GenomeInfoDb_1.20.0         IRanges_2.18.2              S4Vectors_0.22.1           
[37] BiocGenerics_0.30.0        

loaded via a namespace (and not attached):
  [1] circlize_0.4.8         backports_1.1.4        Hmisc_4.2-0           
  [4] fastmatch_1.1-0        plyr_1.8.4             igraph_1.2.4.1        
  [7] lazyeval_0.2.2         splines_3.6.1          urltools_1.7.3        
 [10] digest_0.6.20          htmltools_0.3.6        GO.db_3.8.2           
 [13] gdata_2.18.0           magrittr_1.5           checkmate_1.9.4       
 [16] memoise_1.1.0          cluster_2.1.0          Biostrings_2.52.0     
 [19] annotate_1.62.0        graphlayouts_0.5.0     enrichplot_1.4.0      
 [22] prettyunits_1.0.2      colorspace_1.4-1       blob_1.2.0            
 [25] xfun_0.9               crayon_1.3.4           RCurl_1.95-4.12       
 [28] jsonlite_1.6           graph_1.62.0           genefilter_1.66.0     
 [31] zeallot_0.1.0          survival_2.44-1.1      glue_1.3.1            
 [34] polyclip_1.10-0        gtable_0.3.0           zlibbioc_1.30.0       
 [37] XVector_0.24.0         UpSetR_1.4.0           GetoptLong_0.1.7      
 [40] Rgraphviz_2.28.0       shape_1.4.4            scales_1.0.0          
 [43] DOSE_3.10.2            futile.options_1.0.1   DBI_1.0.0             
 [46] Rcpp_1.0.2             xtable_1.8-4           progress_1.2.2        
 [49] htmlTable_1.13.1       clue_0.3-57            gridGraphics_0.4-1    
 [52] foreign_0.8-72         bit_1.1-14             europepmc_0.3         
 [55] Formula_1.2-3          htmlwidgets_1.3        httr_1.4.1            
 [58] fgsea_1.10.1           acepack_1.4.1          pkgconfig_2.0.2       
 [61] XML_3.98-1.20          farver_1.1.0           nnet_7.3-12           
 [64] locfit_1.5-9.1         labeling_0.3           ggplotify_0.0.4       
 [67] tidyselect_0.2.5       rlang_0.4.0            reshape2_1.4.3        
 [70] munsell_0.5.0          tools_3.6.1            ggridges_0.5.1        
 [73] knitr_1.24             bit64_0.9-7            tidygraph_1.1.2       
 [76] caTools_1.17.1.2       purrr_0.3.2            KEGGREST_1.24.0       
 [79] ggraph_2.0.0           formatR_1.7            KEGGgraph_1.44.0      
 [82] DO.db_2.9              xml2_1.2.2             compiler_3.6.1        
 [85] rstudioapi_0.10        curl_4.0               png_0.1-7             
 [88] tibble_2.1.3           tweenr_1.0.1           geneplotter_1.62.0    
 [91] stringi_1.4.3          lattice_0.20-38        Matrix_1.2-17         
 [94] vctrs_0.2.0            pillar_1.4.2           lifecycle_0.1.0       
 [97] BiocManager_1.30.4     GlobalOptions_0.1.0    triebeard_0.3.0       
[100] cowplot_1.0.0          bitops_1.0-6           qvalue_2.16.0         
[103] R6_2.4.0               latticeExtra_0.6-28    KernSmooth_2.23-15    
[106] gridExtra_2.3          lambda.r_1.2.3         MASS_7.3-51.4         
[109] gtools_3.8.1           assertthat_0.2.1       rjson_0.2.20          
[112] withr_2.1.2            GenomeInfoDbData_1.2.1 hms_0.5.1             
[115] rpart_4.1-15           tidyr_1.0.0            rvcheck_0.1.3         
[118] ggforce_0.3.1          base64enc_0.1-3

Log in to answer this question.