jokergoo / ComplexHeatmap

Make Complex Heatmaps
https://jokergoo.github.io/ComplexHeatmap-reference/book/
Other
1.3k stars 228 forks source link

order of columns in top annotation not matching the heatmap #961

Open cristanchoa opened 2 years ago

cristanchoa commented 2 years ago

Hi,

Thanks for the wonderful and flexible package.

I created a heatmap object based on Complex Heatmap using ArchR. I need to re-order the columns and change the top annotation. The reordering is working as expected, but it seems like when I try to replace the top annotation that the order does not correspond.

original order

ATAC_p2g<-h_sub@ht_list[["ATAC Z-Scores 27253 P2GLinks"]]

original_order<-ATAC_p2g@column_order

ATAC_p2g

image

changed order I think you can see in the image below the pattern of the "group by" changes considerably.

Knames<-h_sub_mat@listData[["ATAC"]]@listData[["colData"]]@rownames
cellnames<-h_sub_mat@listData[["ATAC"]]@listData[["colData"]]@listData[["groupBy"]]

df<-data.frame(Knames, cellnames)

df[c('celltype', 'condition')] <- str_split_fixed(df$cellnames, '_', 2) #this is the meta data with cell type and condition. 

df<-df[with(df, order(condition, celltype)),]
df<-df[with(df, order(condition, decreasing = TRUE)),]

condition<-df$condition
celltype<-df$celltype

reorder<-as.numeric(row.names(df))

reorder

image

trying to add the more complex top annotation The above was created using "celltype.condition" to pull the cells out. The goal is to have the cells of the two different conditions plotted by the order of the same cell type. I had added this information into columns in the dataframe "df" above.

Below, I think you can see that see that the heatmap is in the write order, but something is off about the annotations.

mycolors <- colorRampPalette(brewer.pal(8, "Set1"))(17)
mycolors

colors <- list(
    condition = c('normoxia' = 'red', 'hypoxia' = 'blue'), 
    celltype = c('Astro1' = "#E41A1C", 'Astro2' = "#984560", 'Glut1' = "#4C71A4", "Glut2" = "#3D8D95", "Glut3" =  "#47A265", "Glut4" = "#5B9C5A", "Glut5" = "#7B7281", "In1" = "#9E5198", "In2" = "#CB6651", "In3" = "#F87B0A" , "In4" = "#FFAF13", "In5" = "#FFE729", "NPC" = "#E8D430", "NSC" ="#C18A2B", "Neuron1" = "#B05B3A", "OPC" = "#D36E7C", "RG" = "#D36E7C"))

df$Knames<-NULL

df$cellnames<-NULL

colAnn <- HeatmapAnnotation(
    df = df,
    which = 'col', # 'col' (samples) or 'row' (gene) annotation?
    na_col = 'white', # default colour for any NA values in the annotation data-frame, 'ann'
    col = colors,
    annotation_height = 0.6,
    annotation_width = unit(1, 'cm'),
    gap = unit(1, 'mm'),
    annotation_legend_param = list(
      condition = list(
        nrow = 2, # number of rows across which the legend will be arranged
        title = 'condition',
        title_position = 'topcenter',
        legend_direction = 'vertical',
        title_gp = gpar(fontsize = 12, fontface = 'bold'),
        labels_gp = gpar(fontsize = 12, fontface = 'plain'),
        at = c("normoxia", "hypoxia")),
      celltype = list(
        nrow = 17,
        title = 'celltype',
        title_position = 'topcenter',
        legend_direction = 'vertical',
        title_gp = gpar(fontsize = 12, fontface = 'bold'),
        labels_gp = gpar(fontsize = 12, fontface = 'plain'))))

ATAC_p2g@top_annotation<-colAnn

ATAC_p2g

image

Below is my session information in case that helps.

Thanks in advance! Ana

Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.1.2 (2021-11-01)
 os       Red Hat Enterprise Linux 8.2 (Ootpa)
 system   x86_64, linux-gnu
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2022-08-03
 rstudio  99.9.9 Juliet Rose (server)
 pandoc   2.11.4 @ /cm/shared/apps_chop/rstudio-server/1.4.1717/bin/pandoc/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package                      * version   date (UTC) lib source
 annotate                       1.72.0    2021-10-26 [1] Bioconductor
 AnnotationDbi                  1.56.2    2021-11-09 [1] Bioconductor
 ArchR                        * 1.0.2     2022-07-20 [1] Github (GreenleafLab/ArchR@6a0ec0c)
 assertthat                     0.2.1     2019-03-21 [1] CRAN (R 4.1.1)
 backports                      1.4.1     2021-12-13 [1] CRAN (R 4.1.2)
 Biobase                      * 2.54.0    2021-10-26 [1] Bioconductor
 BiocGenerics                 * 0.40.0    2021-10-26 [1] Bioconductor
 BiocIO                         1.4.0     2021-10-26 [1] Bioconductor
 BiocManager                    1.30.18   2022-05-18 [1] CRAN (R 4.1.2)
 BiocParallel                   1.28.3    2021-12-09 [1] Bioconductor
 Biostrings                   * 2.62.0    2021-10-26 [1] Bioconductor
 bit                            4.0.4     2020-08-04 [1] CRAN (R 4.1.1)
 bit64                          4.0.5     2020-08-30 [1] CRAN (R 4.1.1)
 bitops                         1.0-7     2021-04-24 [1] CRAN (R 4.1.1)
 blob                           1.2.3     2022-04-10 [1] CRAN (R 4.1.2)
 broom                          1.0.0     2022-07-01 [1] CRAN (R 4.1.2)
 BSgenome                     * 1.62.0    2021-10-26 [1] Bioconductor
 BSgenome.Mmusculus.UCSC.mm10 * 1.4.3     2022-01-04 [1] Bioconductor
 cachem                         1.0.6     2021-08-19 [1] CRAN (R 4.1.1)
 Cairo                          1.5-12.2  2020-07-07 [1] CRAN (R 4.1.2)
 callr                          3.7.1     2022-07-13 [1] CRAN (R 4.1.2)
 caTools                        1.18.2    2021-03-28 [1] CRAN (R 4.1.0)
 cellranger                     1.1.0     2016-07-27 [1] CRAN (R 4.1.0)
 chromVARmotifs               * 0.2.0     2021-10-25 [1] Github (GreenleafLab/chromVARmotifs@38bed55)
 circlize                     * 0.4.15    2022-05-10 [1] CRAN (R 4.1.2)
 cli                            3.3.0     2022-04-25 [1] CRAN (R 4.1.2)
 clue                           0.3-61    2022-05-30 [1] CRAN (R 4.1.2)
 cluster                        2.1.2     2021-04-17 [2] CRAN (R 4.1.2)
 CNEr                           1.30.0    2021-10-26 [1] Bioconductor
 codetools                      0.2-18    2020-11-04 [2] CRAN (R 4.1.2)
 colorspace                     2.0-3     2022-02-21 [1] CRAN (R 4.1.2)
 ComplexHeatmap               * 2.10.0    2021-10-26 [1] Bioconductor
 crayon                         1.5.1     2022-03-26 [1] CRAN (R 4.1.2)
 curl                           4.3.2     2021-06-23 [1] CRAN (R 4.1.1)
 data.table                   * 1.14.2    2021-09-27 [1] CRAN (R 4.1.1)
 DBI                            1.1.3     2022-06-18 [1] CRAN (R 4.1.2)
 dbplyr                         2.2.1     2022-06-27 [1] CRAN (R 4.1.2)
 DelayedArray                   0.20.0    2021-10-26 [1] Bioconductor
 devtools                     * 2.4.4     2022-07-20 [1] CRAN (R 4.1.2)
 digest                         0.6.29    2021-12-01 [1] CRAN (R 4.1.1)
 DirichletMultinomial           1.36.0    2021-10-26 [1] Bioconductor
 doParallel                     1.0.17    2022-02-07 [1] CRAN (R 4.1.2)
 dplyr                        * 1.0.9     2022-04-28 [1] CRAN (R 4.1.2)
 ellipsis                       0.3.2     2021-04-29 [1] CRAN (R 4.1.1)
 evaluate                       0.15      2022-02-18 [1] CRAN (R 4.1.2)
 fansi                          1.0.3     2022-03-24 [1] CRAN (R 4.1.2)
 fastmap                        1.1.0     2021-01-25 [1] CRAN (R 4.1.1)
 forcats                      * 0.5.1     2021-01-27 [1] CRAN (R 4.1.0)
 foreach                        1.5.2     2022-02-02 [1] CRAN (R 4.1.2)
 fs                             1.5.2     2021-12-08 [1] CRAN (R 4.1.2)
 gargle                         1.2.0     2021-07-02 [1] CRAN (R 4.1.0)
 gdata                        * 2.18.0.1  2022-05-10 [1] CRAN (R 4.1.2)
 generics                       0.1.3     2022-07-05 [1] CRAN (R 4.1.2)
 GenomeInfoDb                 * 1.30.1    2022-01-30 [1] Bioconductor
 GenomeInfoDbData               1.2.7     2021-12-21 [1] Bioconductor
 GenomicAlignments              1.30.0    2021-10-26 [1] Bioconductor
 GenomicRanges                * 1.46.1    2021-11-18 [1] Bioconductor
 GetoptLong                     1.0.5     2020-12-15 [1] CRAN (R 4.1.1)
 ggplot2                      * 3.3.6     2022-05-03 [1] CRAN (R 4.1.3)
 GlobalOptions                  0.1.2     2020-06-10 [1] CRAN (R 4.1.1)
 glue                           1.6.2     2022-02-24 [1] CRAN (R 4.1.2)
 GO.db                          3.14.0    2022-01-04 [1] Bioconductor
 googledrive                    2.0.0     2021-07-08 [1] CRAN (R 4.1.0)
 googlesheets4                  1.0.0     2021-07-21 [1] CRAN (R 4.1.0)
 gridExtra                    * 2.3       2017-09-09 [1] CRAN (R 4.1.1)
 gtable                       * 0.3.0     2019-03-25 [1] CRAN (R 4.1.1)
 gtools                       * 3.9.3     2022-07-11 [1] CRAN (R 4.1.2)
 haven                          2.5.0     2022-04-15 [1] CRAN (R 4.1.2)
 hms                            1.1.1     2021-09-26 [1] CRAN (R 4.1.1)
 htmltools                      0.5.3     2022-07-18 [1] CRAN (R 4.1.2)
 htmlwidgets                    1.5.4     2021-09-08 [1] CRAN (R 4.1.1)
 httpuv                         1.6.5     2022-01-05 [1] CRAN (R 4.1.2)
 httr                           1.4.3     2022-05-04 [1] CRAN (R 4.1.2)
 IRanges                      * 2.28.0    2021-10-26 [1] Bioconductor
 iterators                      1.0.14    2022-02-05 [1] CRAN (R 4.1.2)
 jsonlite                       1.8.0     2022-02-22 [1] CRAN (R 4.1.2)
 KEGGREST                       1.34.0    2021-10-26 [1] Bioconductor
 knitr                          1.39      2022-04-26 [1] CRAN (R 4.1.2)
 later                          1.3.0     2021-08-18 [1] CRAN (R 4.1.1)
 lattice                        0.20-45   2021-09-22 [1] CRAN (R 4.1.1)
 lifecycle                      1.0.1     2021-09-24 [1] CRAN (R 4.1.1)
 lubridate                      1.8.0     2021-10-07 [1] CRAN (R 4.1.1)
 magick                         2.7.3     2021-08-18 [1] CRAN (R 4.1.2)
 magrittr                     * 2.0.3     2022-03-30 [1] CRAN (R 4.1.2)
 Matrix                       * 1.4-1     2022-03-23 [1] CRAN (R 4.1.2)
 MatrixGenerics               * 1.6.0     2021-10-26 [1] Bioconductor
 matrixStats                  * 0.62.0    2022-04-19 [1] CRAN (R 4.1.2)
 memoise                        2.0.1     2021-11-26 [1] CRAN (R 4.1.1)
 mime                           0.12      2021-09-28 [1] CRAN (R 4.1.1)
 miniUI                         0.1.1.1   2018-05-18 [1] CRAN (R 4.1.1)
 modelr                         0.1.8     2020-05-19 [1] CRAN (R 4.1.0)
 munsell                        0.5.0     2018-06-12 [1] CRAN (R 4.1.1)
 pillar                         1.8.0     2022-07-18 [1] CRAN (R 4.1.2)
 pkgbuild                       1.3.1     2021-12-20 [1] CRAN (R 4.1.2)
 pkgconfig                      2.0.3     2019-09-22 [1] CRAN (R 4.1.1)
 pkgload                        1.3.0     2022-06-27 [1] CRAN (R 4.1.2)
 plyr                         * 1.8.7     2022-03-24 [1] CRAN (R 4.1.2)
 png                            0.1-7     2013-12-03 [1] CRAN (R 4.1.1)
 poweRlaw                       0.70.6    2020-04-25 [1] CRAN (R 4.0.4)
 pracma                         2.3.8     2022-03-04 [1] CRAN (R 4.1.2)
 prettyunits                    1.1.1     2020-01-24 [1] CRAN (R 4.1.1)
 processx                       3.7.0     2022-07-07 [1] CRAN (R 4.1.2)
 profvis                        0.3.7     2020-11-02 [1] CRAN (R 4.1.2)
 promises                       1.2.0.1   2021-02-11 [1] CRAN (R 4.1.1)
 ps                             1.7.1     2022-06-18 [1] CRAN (R 4.1.2)
 purrr                        * 0.3.4     2020-04-17 [1] CRAN (R 4.1.1)
 R.methodsS3                    1.8.2     2022-06-13 [1] CRAN (R 4.1.2)
 R.oo                           1.25.0    2022-06-12 [1] CRAN (R 4.1.2)
 R.utils                        2.12.0    2022-06-28 [1] CRAN (R 4.1.2)
 R6                             2.5.1     2021-08-19 [1] CRAN (R 4.1.1)
 RColorBrewer                 * 1.1-3     2022-04-03 [1] CRAN (R 4.1.2)
 Rcpp                         * 1.0.9     2022-07-08 [1] CRAN (R 4.1.2)
 RCurl                          1.98-1.7  2022-06-09 [1] CRAN (R 4.1.2)
 readr                        * 2.1.2     2022-01-30 [1] CRAN (R 4.1.2)
 readxl                         1.4.0     2022-03-28 [1] CRAN (R 4.1.2)
 remotes                        2.4.2     2021-11-30 [1] CRAN (R 4.1.2)
 reprex                         2.0.1     2021-08-05 [1] CRAN (R 4.1.0)
 reshape2                       1.4.4     2020-04-09 [1] CRAN (R 4.1.1)
 restfulr                       0.0.15    2022-06-16 [1] CRAN (R 4.1.2)
 rhdf5                        * 2.38.1    2022-03-10 [1] Bioconductor
 rhdf5filters                   1.6.0     2021-10-26 [1] Bioconductor
 Rhdf5lib                       1.16.0    2021-10-26 [1] Bioconductor
 rjson                          0.2.21    2022-01-09 [1] CRAN (R 4.1.2)
 rlang                          1.0.4     2022-07-12 [1] CRAN (R 4.1.2)
 rmarkdown                      2.14      2022-04-25 [1] CRAN (R 4.1.2)
 Rsamtools                      2.10.0    2021-10-26 [1] Bioconductor
 RSQLite                        2.2.15    2022-07-17 [1] CRAN (R 4.1.2)
 rstudioapi                     0.13      2020-11-12 [1] CRAN (R 4.1.1)
 rtracklayer                  * 1.54.0    2021-10-26 [1] Bioconductor
 rvest                          1.0.2     2021-10-16 [1] CRAN (R 4.1.1)
 S4Vectors                    * 0.32.4    2022-03-24 [1] Bioconductor
 scales                         1.2.0     2022-04-13 [1] CRAN (R 4.1.2)
 seqLogo                        1.60.0    2021-10-26 [1] Bioconductor
 sessioninfo                    1.2.2     2021-12-06 [1] CRAN (R 4.1.1)
 shape                          1.4.6     2021-05-19 [1] CRAN (R 4.1.1)
 shiny                          1.7.2     2022-07-19 [1] CRAN (R 4.1.2)
 stringi                        1.7.8     2022-07-11 [1] CRAN (R 4.1.2)
 stringr                      * 1.4.0     2019-02-10 [1] CRAN (R 4.1.1)
 SummarizedExperiment         * 1.24.0    2021-10-26 [1] Bioconductor
 TFBSTools                      1.32.0    2021-10-26 [1] Bioconductor
 TFMPvalue                      0.0.8     2018-05-16 [1] CRAN (R 4.0.5)
 tibble                       * 3.1.7     2022-05-03 [1] CRAN (R 4.1.2)
 tidyr                        * 1.2.0     2022-02-01 [1] CRAN (R 4.1.2)
 tidyselect                     1.1.2     2022-02-21 [1] CRAN (R 4.1.2)
 tidyverse                    * 1.3.2     2022-07-18 [1] CRAN (R 4.1.2)
 tzdb                           0.3.0     2022-03-28 [1] CRAN (R 4.1.2)
 urlchecker                     1.0.1     2021-11-30 [1] CRAN (R 4.1.2)
 usethis                      * 2.1.6     2022-05-25 [1] CRAN (R 4.1.2)
 utf8                           1.2.2     2021-07-24 [1] CRAN (R 4.1.1)
 vctrs                          0.4.1     2022-04-13 [1] CRAN (R 4.1.2)
 withr                          2.5.0     2022-03-03 [1] CRAN (R 4.1.2)
 xfun                           0.31      2022-05-10 [1] CRAN (R 4.1.2)
 XML                            3.99-0.10 2022-06-09 [1] CRAN (R 4.1.2)
 xml2                           1.3.3     2021-11-30 [1] CRAN (R 4.1.1)
 xtable                         1.8-4     2019-04-21 [1] CRAN (R 4.1.1)
 XVector                      * 0.34.0    2021-10-26 [1] Bioconductor
 yaml                           2.3.5     2022-02-21 [1] CRAN (R 4.1.2)
 zlibbioc                       1.40.0    2021-10-26 [1] Bioconductor

 [1] /home/cristanchoa/R/x86_64-pc-linux-gnu-library/4.1
 [2] /cm/shared/apps_chop/R/4.1.2/lib64/R/library
jokergoo commented 2 years ago

This is a little bit complicated because you are directly modifying the slots in the Heatmap object, which I think not a good idea.

Can you regenerate the heatmap by:

Heatmap(..., top_annotation = colAnn)