Search code examples
rheatmapcomplexheatmap

ComplexHeatmap change direction of segment/link in rowAnnotation


I am building a large heatmap and have added a row annotation. This particular annotation must be on the left side of the heatmap, but when I add labels, the segment/link is facing the wrong direction. Can someone help me figure out how to flip the segment to be in between the label and the annotation?

Reproducible example:

library(ComplexHeatmap)

mat <- matrix(sample(c(-1, 0, 1), 30, replace = T), nrow = 10, ncol = 3, dimnames = list(letters[1:10], c(LETTERS[1:3])))
labels <- c(1:10)

set.seed(123)
anno <- anno_mark(at = labels, 
                  labels = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), 
                  which = "row")
foo <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")

ha = rowAnnotation(mark = anno, 
                   foo = foo, 
                   col = list("a" = "#440154FF", "b" = "#471365FF", "c" = "#482374FF", 
                              "d" = "#46337EFF", "e" = "#424186FF", "f" = "#3C4F8AFF", 
                              "g" = "#365C8DFF", "h" = "#31688EFF", "i" = "#2C738EFF", 
                              "j" = "#277F8EFF"), 
                   show_legend = FALSE)

ht1 = Heatmap(mat, 
              name = "Example", 
              heatmap_legend_param = list(color_bar = "continuous", at = c(-1, 0, 1), 
                                          labels = c("-1", "0", "1"),
                                          legend_width = unit(15, "cm"), 
                                          labels_gp = gpar(fontsize = 22),
                                          title_gp = gpar(fontsize = 22),
                                          title_position = "topleft",
                                          direction = "horizontal"),
              cluster_rows = F, 
              cluster_columns = T, 
              show_row_names = F,
              show_column_names = F,
              use_raster = F,
              left_annotation = ha
)

png(paste0("./",format(Sys.Date(), "%Y%m%d"), "_example.png"), width = 8, height = 4, units = "in", res = 600)
draw(ht1, heatmap_legend_side = "bottom", padding = unit(c(1, 1, 1, 1), "cm"))
dev.off()

And this is the resulting figure: enter image description here

I'm just looking to move the segments on the far left to be in between the letters and the annotation boxes.

Thanks!


Solution

  • I figured out the answer to this; I had simply missed specifying the side argument in anno_mark(). Solution below:

    anno <- anno_mark(at = labels, 
                  labels = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), 
                  which = "row", side = "left")
    

    enter image description here