This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Need help in mapping color to levels in ComplexHeatmap

Hello all,

I have been plotting a heatmap through ComplexHeatmap, everything is fine but when trying to map color to some levels, I got an error.

Here is the code, I used

h.col.study <-
  HeatmapAnnotation(
    Study=meta %>%
      arrange(match(Sample, colnames(hm.data))) %>% 
      filter(Sample %in% colnames(hm.data)) %>% 
      pull(Study),
    Soil_origin=meta %>% 
      arrange(match(Sample, colnames(hm.data))) %>% 
      filter(Sample %in% colnames(hm.data)) %>% 
      pull(Soil_origin), Plant=meta%>% 
      arrange(match(Sample, colnames(hm.data))) %>% 
      filter(Sample %in% colnames(hm.data)) %>% 
      pull(Plant), col=list('Study'=unlist(parameters$plotting$study.cols),
             'Soil_origin'=unlist(parameters$plotting$soil.cols),
             'Plant'=unlist(parameters$plotting$plant.cols)))

Here meta is sample meta data containing information of samples, hm.data is the matrix table containing correlation values in 1 or 0, where 1 shows correlation and 0 shows no correlation.

meta is like this

head(meta)
         Sample Developmental_stage Soil_origin Rhizosphere_compartments
C_1b_N   C_1b_N          Vegetative     Corsica                        N
C_1a_S   C_1a_S          Vegetative     Corsica                        S
C_1a_R   C_1a_R          Vegetative     Corsica                        R
C_1a_N   C_1a_N          Vegetative     Corsica                        N
F_96e_S F_96e_S          Vegetative      France                        S
F_96e_R F_96e_R          Vegetative      France                        R
                      Plant Stress_condition                  Study
C_1b_N  Medicago truncatula           Normal Brown_SP_et_al._(2020)
C_1a_S  Medicago truncatula           Normal Brown_SP_et_al._(2020)
C_1a_R  Medicago truncatula           Normal Brown_SP_et_al._(2020)
C_1a_N  Medicago truncatula           Normal Brown_SP_et_al._(2020)
F_96e_S Medicago truncatula           Normal Brown_SP_et_al._(2020)
F_96e_R Medicago truncatula           Normal Brown_SP_et_al._(2020)

hm.data is like this

               C_27a_R C_1c_S MRF_CELRK_V5_2_1_rhizoplane SDR3 C_27e_N
Ramlibacter        1      1                           0    0       1
Massilia           1      1                           0    0       1
Arenimonas         1      1                           1    0       1
Fimbriimonas       1      1                           0    0       1
Chryseolinea       1      1                           0    0       1
unlist(parameters$plotting$study.cols)

Brown_SP_et_al._(2020) 
                              "lightcoral" 
                     Yi_Zhou_et_al._(2020) 
                               "lawngreen" 
    Caroline_Fadeke_Ajilogba_et_al._(2022) 
                              "orangered2" 
             Ankit_T._Hinsu_et_al.__(2021) 
                              "lightblue2" 
              Liangxiang_Dai_et_al._(2019) 
                                 "hotpink" 
                Kyle_Hartman_et_al._(2017) 
                              "olivedrab4" 
     Juan_E._Pérez-Jaramillo_et_al._(2019) 
                               "honeydew2" 
                    Fang_Liu_et_al._(2019) 
                                   "snow1" 
              Stopnisek,_N.,_et_al._(2021) 
                              "slategray3" 
Stopnisek,_N.,_Shade,_A._Persistent_(2021) 
                                 "yellow1" 
            Akifumi_Sugiyama_et_al._(2014) 
                                    "peru"

unlist(parameters$plotting$plant.cols)

Medicago truncatula                Cicer arietinum 
                  "lightpink2"                      "grey100" 
             Vigna subterranea            Arachis hypogaea L. 
                    "moccasin"                        "plum1" 
Trifolium pratense var. Milvus             Phaseolus vulgaris 
                        "blue"                   "powderblue" 
                  Glycine soja                    Glycine max 
                       "green"                       "brown3" 

unlist(parameters$plotting$soil.cols) 

Corsica           France        Australia     South Africa 
    "cadetblue3"     "burlywood2"         "brown2"       "darkcyan" 
           India            China      Switzerland   Colombia (USA) 
      "darkgrey"       "cornsilk"    "aquamarine3"         "coral4" 
 Tennessee (USA)   Nebraska (USA)   Michigan (USA)   Colorado (USA) 
 "darkslategray"  "darkseagreen4"      "darkkhaki"     "firebrick4" 
Washington (USA)            Japan 
          "gold"    "deepskyblue"

Further codes used are below

h.row.pf <- 
      rowAnnotation(
        Pos = row_anno_barplot(rowSums(hm.data)/ncol(hm.data), border=TRUE, 
                               ylim=c(0,.4),
                               gp=gpar(fill='grey20', col='grey20', lwd=1.5), 
                               axis_param=list(gp=gpar(ylim=c(0,0.4), fontsize=8)),
                               axis=TRUE), width=unit(1, 'inches'))

ht.main <- Heatmap(hm.data, cluster_rows = jacc.dend,
                   cluster_columns = FALSE,
                   top_annotation = h.col.study,
                   bottom_annotation = h.col.pred,
                   column_order = order(colSums(hm.data)), 
                   show_column_names = FALSE,
                   show_row_names = TRUE, row_names_side="left",
                   col=c('0'='snow', '1'='grey57'), width=2,
                   name='Positivity Heatmap')

Now the error I got is

draw(ht.main + h.row.pf, heatmap_legend_side='right', 
             annotation_legend_side='right')

    Error: Plant: cannot map colors to some of the levels: Glycine soja ,
    Trifolium pratense var. Milvus, Glycine max

Any help would be really helpful

r complexheatmap

Sometimes named vectors throw errors in packages. If you add use.names = FALSE to the unlist() command, does the error persist?

Adding "use.names = FALSE" to unlist() shows this error

Error: elements in `col` should be named vectors

1 answer

Thank you for your time. I resolved the issue. Actually the problem was this:

levels(meta$Plant)
NULL

I didn't check this before then I set the levels

meta$Plant <- factor(meta$Plant, levels = c('Medicago truncatula','Cicer arietinum','Vigna subterranea','Arachis hypogaea L.','Trifolium pratense var. Milvus','Phaseolus vulgaris','Glycine soja','Glycine max'))
> levels(meta$Plant)
[1] "Medicago truncatula"            "Cicer arietinum"               
[3] "Vigna subterranea"              "Arachis hypogaea L."           
[5] "Trifolium pratense var. Milvus" "Phaseolus vulgaris"            
[7] "Glycine soja"                   "Glycine max"

Log in to answer this question.