jokergoo / ComplexHeatmap

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

Moving annotation legend to the top #1188

Open mxposed opened 5 months ago

mxposed commented 5 months ago

Hello and thank you for the majestic package!

I cannot figure out how to pass through HeatmapAnnotation annotation_legend_param parameter that the legend should not be aligned to the center of the heatmap, but justified to the top. Can you please guide through the best options to do this?

My approximate code:

ha = HeatmapAnnotation(
  df = data,
  col = list(
    Diagnosis = DIAGNOSIS_PALETTE,
    Outcome = OUTCOME_PALETTE
  ),
  na_col = "white",
  annotation_name_gp = gpar(fontsize = 14),
  annotation_legend_param = list(
    Diagnosis = list(
      title_gp = gpar(fontsize = 14),
      labels_gp = gpar(fontsize = 12),
      just = c('left', 'top'),   # My attempts
      legend_gp = list(        # More attempts
        just = c('left', 'top'),
        x = unit(0, 'npc'),
        y = unit(1, 'npc')
      )
    ),
    Outcome = list(
      title_gp = gpar(fontsize = 14),
      labels_gp = gpar(fontsize = 12),
      just = c('left', 'top')
    )
  )
)
hmap <- Heatmap(mat = data,
               name = "Normalized expression",
               col = col_fun,
               cluster_rows = FALSE,
               cluster_columns = TRUE,
               clustering_distance_columns = "euclidean",
               clustering_distance_rows = "euclidean",
               clustering_method_columns = 'ward.D2',
               clustering_method_rows = 'ward.D2',
               show_column_names = FALSE,
               show_row_names = FALSE,
               column_split = 2,
               split = split,
               column_title = expression(CD8^'+' ~ T ~ cells ~ samples),
               row_title = "Differentially expressed genes (975)",
               row_gap = unit(1.5, "mm"),
               column_gap = unit(1.5, "mm"),
               row_names_gp = gpar(fontsize = 14),
               column_title_gp = gpar(fontsize = 16),
               row_title_gp = gpar(fontsize = 16),
               top_annotation = ha, 
               heatmap_legend_param = list(title = "Normalized expression",
                                           at = c(-2,0,2), labels = c(-3,0,3),
                                           legend_direction = "horizontal",
                                           legend_width = unit(4, "cm"),
                                           heatmap_legend_side = "bottom",
                                           annotation_legend_side = "right",
                                           title_gp = gpar(fontsize = 14),
                                           labels_gp = gpar(fontsize = 12),
                                           legend_gp = gpar(fontsize = 12)))
Yunuuuu commented 5 months ago

Maybe this can be helpful: https://jokergoo.github.io/ComplexHeatmap-reference/book/legends.html?q=heatmap_legend_sid#the-side-of-legends