jokergoo / ComplexHeatmap

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

Cannot add manual legend to heatmap annotation #1184

Open katimbach opened 6 months ago

katimbach commented 6 months ago

Please provide example datasets and the code you use. It will help me to understand your problem and help you!

I have made a HeatmapAnnotation with the following code, that I show below: hm<- HeatmapAnnotation(Patient= anno_simple(info_tbl$PATIENT, pch=info_tbl$SLICE_TYPE_SYMBOL, col = patient_cols), Location= info_tbl$LOCATION_SIMPLE, Tissue= info_tbl$TYPE_OF_TISSUE, BRCAstatus= anno_simple(info_tbl$BRCA_STATUS_SIMPLE, pch= info_tbl$HRD_TEST_SYMBOL, col= brca_col), Timepoint= anno_simple(info_tbl$CLINICAL_TIMEPOINT_SPEC, pch = info_tbl$FIRST_PT_STATUS_SYMBOL, col= tmpt_col), col= list(Location= loc_cols, Tissue = tiss_cols) ) hm

Because I have three anno_simple with symbols, I need to add those legends manually, both for the colors and the meanings of the symbols itself (similar to the reference 3.2 with the p value colors & pch legend). However, it seems because this is a HeatmapAnnotation, and not a heatmap, I cannot manually add the legend for some reason. For example, if I make a legend just for Patient colors:

pat_leg<- Legend(title= "Patient", legend_gp = gpar(fill =patient_cols), at= names(patient_cols))

It looks fine using draw(pat_leg), but I am unable to do the following:

draw(hm, annotation_legend_list = list(pat_leg))

Error in (function (x = unit(0.5, "npc"), y = unit(0.5, "npc"), width = unit(1,  : 
  unused argument (annotation_legend_list = list(new("Legends", name = NULL, grob = list("GRID.legend.2441", NULL, list(0.5, 0.5, 17.8023055555555, 40.5329444444444, c(0.5, 0.5), list(), FALSE, c(0, 1), c(0, 1), 0, NULL, NULL, NULL, c(0.5, 0.5), NULL, NULL, "GRID.VP.645", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, TRUE, NULL), list(list("Patient", 0, 1, c("left", "top"), NULL, NULL, 0, FALSE, "GRID.text.2442", list(10, 2, 0.9), NULL), list("GRID.legend_body.2439", NULL, 
    list(0, 0, 17.8023055555555, 36, c(0, 0), list(), FALSE, c(0, 1), c(0, 1), 0, NULL, NULL, NULL, c(0, 0), NULL, NULL, "GRID.VP.644", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, TRUE, NULL), list(list(c("Patient1", "Patient2", "Patient3", "Patient4", "Patient5", "Patient6", "Patient7", "Patient8", "Patient9"), 5, c(34, 30, 26, 22, 18, 14, 10, 6, 2), "left", NULL, NULL, 0, FALSE, "GRID.text.2437", list(10, 0.9), NULL), list(c(0, 0,

I also tried to add the legend to the HeatmapAnnotation function itself, with annotation_legend_param, but I get an error for that as well, though the names clearly match:

hm<- HeatmapAnnotation(Patient= anno_simple(info_tbl$PATIENT, 
                                            pch=info_tbl$SLICE_TYPE_SYMBOL, col = patient_cols),
                       Location= info_tbl$LOCATION_SIMPLE,
                       Tissue= info_tbl$TYPE_OF_TISSUE,
                       BRCAstatus= anno_simple(info_tbl$BRCA_STATUS_SIMPLE, 
                                               pch= info_tbl$HRD_TEST_SYMBOL, col= brca_col), 
                       Timepoint= anno_simple(info_tbl$CLINICAL_TIMEPOINT_SPEC, 
                                              pch = info_tbl$FIRST_PT_STATUS_SYMBOL, col= tmpt_col),
     col= list(Location= loc_cols, Tissue = tiss_cols),
     annotation_legend_param = list(Patient= list(
       title= "Patient", legend_gp = gpar(fill =patient_cols), at= names(patient_cols)
     )
     )
     )

Error: Some names in 'annotation_legend_param' are not in names of simple annotations.

How might I be able to add manual legends to the HeatmapAnnotation in this case? I haven't seen anything in the documentation about this being done.

Yunuuuu commented 5 months ago

without heatmap, it's better to use another package like ggplot2.