This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to make a heatmap showing signatures of scores from 0 to 1 in R?

Hi,

I have a data matrix mat of 622 samples as columns and cell types as rows.

sample annotation:

Samples      Group
Sample1         A   
Sample2         A
Sample3         A
Sample4         A
Sample5         B
Sample6         B
Sample7         B
Sample8         B

Here I am showing a small example data which looks like below:

             Sample1    Sample2 Sample3 Sample4 Sample5 Sample6 Sample7 Sample8
Adipocytes      0          0       0       0     0.003  2.00E-04   0       0
Astrocytes      0          0       0    0.1343  0.1011  0.0562  0.0791  0.0089
B-cells.    0.0095      0.0059  0.0378  0.0016    0     0.0673  0.0361  0.0468
Basophils   0.0091      0.0229     0    0.0864    0     0.069   0.026   0.0994
CD4+ T-cells    0          0       0       0      0        0       0       0
CD4+ Tcm        0          0    0.013   0.0401  0.0053     0    0.0362     0
CD4+ Tem        0      0.0382   0.0268  0.0372    0        0    0.0451  0.0729
CD4+ m T-cells  0.046   0.027   0.0229     0    0.0109  0.0278     0    0.0258
CD4+ n T-cells  0          0       0       0      0     0.0037  0.0196     0
CD8+ T-cells    0.0195 5.00E-04 0.0198     0      0     0.0297  0.0036  0.0646
CD8+ Tcm    0.0108         0       0       0      0     0.0475     0    0.0746
CD8+ Tem        0          0       0       0      0     0.0012  0.0038  0.0483
CD8+ n T-cells  0.0216  0.0027  0.0124     0    0.0055  0.014      0    0.0132

I am using complex heatmap to generate heatmap showing signatures.

I tried like below, but I have an error:

ann <- data.frame(samples$Group)
colnames(ann) <- c("Type")
colours <- list("Type"=c("A"="orange","B"="darkgreen"))
colAnn <- HeatmapAnnotation(df=ann, which="col", col=colours, annotation_width=unit(c(1, 4), "cm"), gap=unit(1, "mm"))

myCol <- colorRampPalette(c("navyblue", "#002bff"))(100)
myBreaks <- seq(-1,1, length=100)
hmap <- Heatmap(mat, name = "xCell scores", col = colorRamp2(myBreaks, myCol),
                width = unit(100, "mm"),show_row_names = TRUE, show_column_names = FALSE, cluster_rows = FALSE,
                cluster_columns = FALSE, show_column_dend = FALSE, show_row_dend = FALSE,
                row_dend_reorder = FALSE, column_dend_reorder = FALSE)
draw(hmap, heatmap_legend_side="left", annotation_legend_side="right")

The Error I got:

Error in labels_mat[, i] : subscript out of bounds
In addition: Warning message:
In matrix(1:(nrow * ncol), nrow = nrow, ncol = ncol, byrow = by_row) :
  data length exceeds size of matrix

I want to plot to be looked like this which is from Figure 2a in this paperenter image description here

Any help is appreciated. thanq

rna-seq heatmap complexheatmap r signatures
  • Your draw specifies an annotation_legend_side, but you don't have any annotations. What's going on there?
  • cluster_rows=FALSE and clustering_method_rows="ward.D2": Why do you have a clustering method when clustering is disabled?

sorry that was a mistake. there is no annotation_legend_side I removed that in question.

What are the output to dim(mat) and class(mat)? Can you also paste the output of sessionInfo()?

dim(mat)
67 622

class(mat)
matrix

sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
 [1] fgsea_1.10.0           Rcpp_1.0.1             ReactomePA_1.28.0     
 [4] org.Hs.eg.db_3.8.2     AnnotationDbi_1.46.0   IRanges_2.18.1        
 [7] S4Vectors_0.22.0       Biobase_2.44.0         BiocGenerics_0.30.0   
[10] clusterProfiler_3.12.0 forcats_0.4.0          stringr_1.4.0         
[13] dplyr_0.8.1            purrr_0.3.2            readr_1.3.1           
[16] tidyr_0.8.3            tibble_2.1.3           ggplot2_3.1.1         
[19] tidyverse_1.2.1        RColorBrewer_1.1-2     circlize_0.4.6        
[22] ComplexHeatmap_2.0.0   gplots_3.0.1.1         matrixStats_0.54.0    
[25] edgeR_3.26.4           limma_3.40.2          

loaded via a namespace (and not attached):
 [1] colorspace_1.4-1    rjson_0.2.20        ellipsis_0.1.0      ggridges_0.5.1     
 [5] qvalue_2.16.0       GlobalOptions_0.1.0 clue_0.3-57         rstudioapi_0.10    
 [9] farver_1.1.0        urltools_1.7.3      ggrepel_0.8.1       bit64_0.9-7        
[13] lubridate_1.7.4     xml2_1.2.0          splines_3.6.0       GOSemSim_2.10.0    
[17] polyclip_1.10-0     jsonlite_1.6        broom_0.5.2         cluster_2.0.9      
[21] GO.db_3.8.2         png_0.1-7           graph_1.62.0        graphite_1.30.0    
[25] ggforce_0.2.2       compiler_3.6.0      httr_1.4.0          rvcheck_0.1.3      
[29] backports_1.1.4     assertthat_0.2.1    Matrix_1.2-17       lazyeval_0.2.2     
[33] cli_1.1.0           tweenr_1.0.1        prettyunits_1.0.2   tools_3.6.0        
[37] igraph_1.2.4.1      gtable_0.3.0        glue_1.3.1          reshape2_1.4.3     
[41] DO.db_2.9           rappdirs_0.3.1      fastmatch_1.1-0     enrichplot_1.4.0   
[45] cellranger_1.1.0    gdata_2.18.0        nlme_3.1-140        ggraph_1.0.2       
[49] rvest_0.3.4         gtools_3.8.1        statmod_1.4.32      DOSE_3.10.1        
[53] europepmc_0.3       MASS_7.3-51.4       scales_1.0.0        reactome.db_1.68.0 
[57] hms_0.4.2           yaml_2.2.0          memoise_1.1.0       gridExtra_2.3      
[61] UpSetR_1.4.0        triebeard_0.3.0     stringi_1.4.3       RSQLite_2.1.1      
[65] checkmate_1.9.3     caTools_1.17.1.2    BiocParallel_1.18.0 shape_1.4.4        
[69] rlang_0.3.4         pkgconfig_2.0.2     bitops_1.0-6        lattice_0.20-38    
[73] labeling_0.3        cowplot_0.9.4       bit_1.1-14          tidyselect_0.2.5   
[77] plyr_1.8.4          magrittr_1.5        R6_2.4.0            generics_0.0.2     
[81] DBI_1.0.0           pillar_1.4.1        haven_2.1.0         withr_2.1.2        
[85] modelr_0.1.4        crayon_1.3.4        KernSmooth_2.23-15  viridis_0.5.1      
[89] GetoptLong_0.1.7    progress_1.2.2      locfit_1.5-9.1      readxl_1.3.1       
[93] data.table_1.12.2   blob_1.1.1          digest_0.6.19       gridGraphics_0.4-1 
[97] munsell_0.5.0       viridisLite_0.3.0   ggplotify_0.0.3

Switch to github latest version of ComplexHeatmap: devtools::install_github('jokergoo/ComplexHeatmap'). If already installed through github, please use devtools::update_packages() and pick the number displayed against ComplexHeatmap. It's a rapidly evolving package and you are quite a few versions behind.

sure I will. But may I know something about the error and how to make a plot like above please

Try updating and see if the error persists. If it does and the error is the same, we can work on figuring out what's happening.

Yes, I did that and I see the same error again.

Can you check your sessionInfo() to be sure you're using ComplexHeatmap v 2.1.0? Your matrix looks fine, you should not be seeing this error.

If you're using the right version, try this:

mat.tmp <- mat
rownames(mat.tmp) <- NULL
hmap.tmp <- Heatmap(mat.tmp, name = "xCell scores", col = colorRamp2(myBreaks, myCol),
            width = unit(100, "mm"),show_row_names = TRUE, show_column_names = FALSE, cluster_rows = FALSE,
            cluster_columns = FALSE, show_column_dend = FALSE, show_row_dend = FALSE,
            row_dend_reorder = FALSE, column_dend_reorder = FALSE)
draw(hmap.tmp)

Also, try modifying the 100 in the colorRampPalette(c("navyblue", "#002bff"))(100) to 42000.

sorry, there is no change in the error. And in my question I also added the sample annotation now and will be using annotation_legend_side argument now.

The error still says same.

Error in labels_mat[, i] : subscript out of bounds
In addition: Warning message:
In matrix(1:(nrow * ncol), nrow = nrow, ncol = ncol, byrow = by_row) :
  data length exceeds size of matrix

1 answer

For the moment, omit the annotation as it only adds more variables to the problem. Try skipping the col= parameter. Next, just do a simple draw(Heatmap(mat)), see if the error persists. Add options one by one and see when the code breaks. That'll give you a lead to follow.

Ok. everything works now. But would like to know how I can bring the samples with values > 0 all together like in the plot given in the question.

In the above image you can see if it is 0 it is light white color and all the samples with values > 0 to 1 were all together for all the cell types. How to get that?

You'd either have to cluster the columns or do some sort of guided ordering. I'm not sure how to get to that exact picture.

ok np. will have a look. Thanks a lot.

Log in to answer this question.