Error in ComplexHeatmap while adding barplot as column annotations
0
1
Entering edit mode
24 months ago

Hi,

I am working with an log2 fold change expression matrix in R and trying to plot a heatmap annotated with bars on each column on the top from the counts data (or responsive genes) using library(ComplexHeatmap) package and getting an error. I have provided the input data and expected out below. Please advise on how to solve this.

Error: number of observations in top annotation should be as same as ncol of the matrix.

Thank you, Toufiq

dput(Expression)
structure(c(-0.883643926, -0.248614376, -0.518091486, -0.535221236, 
            -0.415450436, -0.940323826, -0.723796576, -0.824290276, -0.753322836, 
            -0.806255146, -0.747521326, NA, -0.320247786, 0.110402434, -1.005757776, 
            -0.706686256, -0.796173776, -0.574616976, -0.757579476, -0.651924848, 
            -0.044501908, -0.451601548, -0.384956008, -0.678620408, -0.706674058, 
            -0.572657338, -0.686018538, -0.514713298, -0.532390248, -0.462136378, 
            NA, -0.123749828, 0.343669412, -0.489804678, -0.447612948, -0.611873538, 
            -0.477327798, -0.590774288, -0.017798089, 0.192058061, -0.055408169, 
            -0.152159119, 0.263042701, -0.382693409, -0.063857699, -0.152939869, 
            -0.050295909, -0.337659179, -0.058902499, NA, -0.410700949, -0.079817359, 
            -0.313859499, 0.002403881, -0.562811849, -0.353911149, 0.006032581, 
            -0.141324408, 0.096038562, 0.171087962, -0.295921938, 0.482199712, 
            -0.317140358, -0.115931318, -0.264788018, -0.158997338, -0.476858598, 
            0.039648912, NA, -0.235496858, 0.065630452, -0.175745228, 0.083321692, 
            -0.712253838, -0.251200888, -0.004567068, -0.167945369, 0.805406021, 
            -0.591077759, 0.741171721, 2.494610191, -0.532343489, -0.358607189, 
            -0.442774239, -0.103589789, 0.213156301, -0.022826199, NA, 1.215920941, 
            3.377354481, 0.033402621, -0.183228609, 0.616853471, 1.260055211, 
            -0.518548419, -0.269887464, 0.651177556, -0.638584884, 0.637445626, 
            2.363311126, -0.518550644, -0.548715454, -0.656370694, -0.150589014, 
            0.061194616, -0.121604684, NA, 1.198130706, 3.132598336, -0.102222284, 
            -0.338918174, 0.407338406, 1.192401486, -0.629295034, 0.224167264, 
            0.181754044, 0.271578844, -0.357815996, 0.173898274, 0.097649164, 
            0.047596544, 0.204227434, 0.047299374, 0.225855454, 0.098997764, 
            NA, 0.316586394, 0.050782324, 0.253073784, 0.266165624, 0.079117194, 
            0.031888764, 0.231072844, 0.055545271, 0.059685561, 0.006279711, 
            -0.635848059, 0.297302061, -0.006876539, -0.050632179, 0.006454301, 
            -0.042377289, -0.020155689, -0.125523179, NA, 0.207185861, 0.298005181, 
            0.050015221, 0.096842111, -0.226106489, -0.312930109, 0.042911411
), .Dim = c(19L, 8L), .Dimnames = list(c("Gene_1", "Gene_2", 
                                         "Gene_3", "Gene_4", "Gene_5", "Gene_6", "Gene_7", "Gene_8", "Gene_9", 
                                         "Gene_10", "Gene_11", "Gene_12", "Gene_13", "Gene_14", "Gene_15", 
                                         "Gene_16", "Gene_17", "Gene_18", "Gene_19"), c("A", "B", "C", 
                                                                                        "D", "E", "F", "F1", "H")))
#>                  A           B            C            D           E
#> Gene_1  -0.8836439 -0.65192485 -0.017798089 -0.141324408 -0.16794537
#> Gene_2  -0.2486144 -0.04450191  0.192058061  0.096038562  0.80540602
#> Gene_3  -0.5180915 -0.45160155 -0.055408169  0.171087962 -0.59107776
#> Gene_4  -0.5352212 -0.38495601 -0.152159119 -0.295921938  0.74117172
#> Gene_5  -0.4154504 -0.67862041  0.263042701  0.482199712  2.49461019
#> Gene_6  -0.9403238 -0.70667406 -0.382693409 -0.317140358 -0.53234349
#> Gene_7  -0.7237966 -0.57265734 -0.063857699 -0.115931318 -0.35860719
#> Gene_8  -0.8242903 -0.68601854 -0.152939869 -0.264788018 -0.44277424
#> Gene_9  -0.7533228 -0.51471330 -0.050295909 -0.158997338 -0.10358979
#> Gene_10 -0.8062551 -0.53239025 -0.337659179 -0.476858598  0.21315630
#> Gene_11 -0.7475213 -0.46213638 -0.058902499  0.039648912 -0.02282620
#> Gene_12         NA          NA           NA           NA          NA
#> Gene_13 -0.3202478 -0.12374983 -0.410700949 -0.235496858  1.21592094
#> Gene_14  0.1104024  0.34366941 -0.079817359  0.065630452  3.37735448
#> Gene_15 -1.0057578 -0.48980468 -0.313859499 -0.175745228  0.03340262
#> Gene_16 -0.7066863 -0.44761295  0.002403881  0.083321692 -0.18322861
#> Gene_17 -0.7961738 -0.61187354 -0.562811849 -0.712253838  0.61685347
#> Gene_18 -0.5746170 -0.47732780 -0.353911149 -0.251200888  1.26005521
#> Gene_19 -0.7575795 -0.59077429  0.006032581 -0.004567068 -0.51854842
#>                   F           F1            H
#> Gene_1  -0.26988746  0.22416726  0.055545271
#> Gene_2   0.65117756  0.18175404  0.059685561
#> Gene_3  -0.63858488  0.27157884  0.006279711
#> Gene_4   0.63744563 -0.35781600 -0.635848059
#> Gene_5   2.36331113  0.17389827  0.297302061
#> Gene_6  -0.51855064  0.09764916 -0.006876539
#> Gene_7  -0.54871545  0.04759654 -0.050632179
#> Gene_8  -0.65637069  0.20422743  0.006454301
#> Gene_9  -0.15058901  0.04729937 -0.042377289
#> Gene_10  0.06119462  0.22585545 -0.020155689
#> Gene_11 -0.12160468  0.09899776 -0.125523179
#> Gene_12          NA          NA           NA
#> Gene_13  1.19813071  0.31658639  0.207185861
#> Gene_14  3.13259834  0.05078232  0.298005181
#> Gene_15 -0.10222228  0.25307378  0.050015221
#> Gene_16 -0.33891817  0.26616562  0.096842111
#> Gene_17  0.40733841  0.07911719 -0.226106489
#> Gene_18  1.19240149  0.03188876 -0.312930109
#> Gene_19 -0.62929503  0.23107284  0.042911411


dput(Counts)
structure(list(A = 3L, B = 3L, C = 15L, D = 12L, E = 5L, F = 1L, 
               F1 = 7L, H = 5L), class = "data.frame", row.names = "counts")
#>        A B  C  D E F F1 H
#> counts 3 3 15 12 5 1 7 5

all(colnames(Expression) == colnames(Counts))

# Plot heatmap
column_ha = HeatmapAnnotation(bar = anno_barplot(Counts))
Heatmap(Expression, name = "log2FC", top_annotation = column_ha, cluster_columns = F)

> Error: number of observations in top annotation should be as same as
> ncol of the matrix.

Expected plot

Created on 2022-04-21 by the [reprex package](https://reprex.tidyverse.org) (v2.0.1)

ComplexHeatmap heatmap barplot R annotation • 1.2k views
ADD COMMENT
1
Entering edit mode

The problem comes from the fact Counts is not numeric but a data.frame.

column_ha = HeatmapAnnotation(bar = anno_barplot(as.numeric(Counts)))

But next problem, you'll have to deal with is the presence of NA in your dataframe...

ADD REPLY
0
Entering edit mode

Hi Basti, thank you for the prompt response. This resolved my query.

ADD REPLY

Login before adding your answer.

Traffic: 2537 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6