如何调整ComplexHeatmap左侧rowAnnotation的线段/连接方向,使其位于标签与注释框之间
I see exactly what's happening here—your current annotation order is placing the anno_mark closer to the heatmap, which makes the segments connect directly to the heatmap rows instead of sitting between the labels and color boxes. Here's how to fix this:
Key Fix: Reorder Annotations in rowAnnotation
In ComplexHeatmap, annotations in rowAnnotation are arranged from closest to the heatmap to farthest left in the order you define them. By swapping the positions of your foo color annotation and mark annotation, you’ll make the color boxes sit right next to the heatmap, with the labeled segments connecting the boxes to the outer labels—exactly the layout you want.
Modified Reproducible Code
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) # Keep your anno_mark definition unchanged 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") # --- Critical Change: Swap foo and mark in rowAnnotation --- ha = rowAnnotation( foo = foo, # Color boxes now sit closest to the heatmap mark = anno, # Labels + segments sit to the left of the color boxes 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()
Optional Tweaks for Appearance
If you want to refine the look of the segments or spacing, you can add these parameters to anno_mark:
link_width = unit(2, "mm"): Make segments thickergap = unit(5, "mm"): Adjust the space between color boxes and segmentslabels_gp = gpar(fontsize = 12): Resize label text
Example of adjusted anno_mark:
anno <- anno_mark( at = labels, labels = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), which = "row", link_width = unit(2, "mm"), gap = unit(5, "mm"), labels_gp = gpar(fontsize = 12) )
内容的提问来源于stack exchange,提问作者kholden

