Search code examples
rannotationsheatmap

Display two R heatmaps with the same annotation side by side


Let me start by saying I am very novice to R and am using some code created by a previous colleague of mine. My code currently creates 2 heatmaps with an annotation row. These were presented separately until now, like this:

First heatmapSecond heatmap

What I'm trying to do is to assemble them on the same figure, with a single annotation, in this form:

Combined

What I am trying to do and can't seem to, is having the sorting done per the C-terminus and not the N-terminus, which effects the annotation as well. If I replace the order between them, it sorts according to the C-terminus as I want it to, but I really want to have the C-terminus on the right.

My code:

cterm = Heatmap(as.matrix(c_term_agg_norm1 %>% mutate_if(is.numeric, function(x) ifelse(is.infinite(x), log2(0.00001), x))),
              show_row_names = TRUE,
              show_column_names = TRUE,
              show_row_dend =  FALSE,
              show_column_dend = FALSE,
              cluster_columns = FALSE,
              col = col_fun,
              rect_gp = gpar(col = "white", lwd = 0.5),
              width = w*unit(5, "mm"), 
              height = h*unit(5, "mm"),
              name = "normalized percentage",
              column_title = "C Terminus Cleavage",
              right_annotation = MAP_annot,
              column_split=factor(str_extract(names(c_term_agg_norm1),"[^_]+")))

nterm = Heatmap(as.matrix(n_term_agg_norm1[-1,] %>% mutate_if(is.numeric, function(x) ifelse(is.infinite(x), log2(0.00001), x))),
              show_row_names = TRUE,
              show_column_names = TRUE,
              show_row_dend =  FALSE,
              show_column_dend = FALSE,
              cluster_columns = FALSE,
              col = col_fun,
              rect_gp = gpar(col = "white", lwd = 0.5),
              width = w*unit(5, "mm"), 
              height = h*unit(5, "mm"),
              name = "normalized percentage",
              column_title = "N Terminus Cleavage",
              #right_annotation = MAP_annot,
              column_split=factor(str_extract(names(n_term_agg_norm1),"[^_]+")))
combined_heatmap = nterm + cterm
draw(combined_heatmap)

Solution

  • looks like the ComplexHeatmap package...

    you can state the name of the main heatmap to make it the dominant layout controller... its position is not important anymore...

    • delete/comment-out #name = "normalized percentage", in the nterm heatmap
    • then state the main heatmap name:

    draw(combined_heatmap, main_heatmap = "normalized percentage")