ComplexHeatmap:如何以不同的方式放置热图图例和注释图例?

Posted

技术标签:

【中文标题】ComplexHeatmap:如何以不同的方式放置热图图例和注释图例?【英文标题】:ComplexHeatmap: How to place heatmap legend and annotation legend differently? 【发布时间】:2021-09-29 15:56:42 【问题描述】:

我用library(ComplexHeatmap)制作了这个情节

我希望Z-score 位于底部位置,而类别变量显示在右侧。 This post 接近了,但我无法使用 rowAnnotation 使其工作,如下面的脚本所示。

预期输出

有了这些数据:

set.seed(123)
library(ComplexHeatmap)
mat = matrix(rnorm(96, 2), 8, 12)
mat = rbind(mat, matrix(rnorm(48, -2), 4, 12))
hmap <- as.data.frame(t(mat))

hmap$type <- rep(c("Ctrl", "Cell_type1", "Cell_type2"), 4)
hmap$malig <- ifelse(hmap$type == "Ctrl", "Ctrl", "Tumor")

hmap_bt <- scale(as.matrix(hmap[, -c(13:14)]))

使用这个脚本

draw(Heatmap(hmap_bt, 
             name = "Z-score",    
             col = colorRamp2(c(-2, 0, 2), c("#6DBCC3", "white", "#8B3A62")),

             show_column_names = FALSE,
             show_column_dend = FALSE,
             column_km = 3,
             
             left_annotation = rowAnnotation(Case = hmap[, c(13:14)]$malig,
                                             Type = hmap[, c(13:14)]$type,
                                             col = list(Case = c("Ctrl" = "#D1B551", "Tumor" = "#678F53"),
                                                        Type = c("Ctrl" = "#D1B551", "Cell_type1" = "green", "Cell_type2" = "blue")),
                                             annotation_legend_param = list(
                                               Case = list( 
                                                 title_gp = gpar(fontsize = 16, 
                                                                 fontface = "bold"), 
                                                 labels_gp = gpar(fontsize = 16)),
                                               Type = list( 
                                                 title_gp = gpar(fontsize = 16,
                                                                 fontface = "bold"), 
                                                 labels_gp = gpar(fontsize = 16)))),
             heatmap_legend_param = list(
               legend_direction = "horizontal", 
               legend_width = unit(6, "cm")), 
    ),

  heatmap_legend_side = "bottom"
)

【问题讨论】:

【参考方案1】:

尝试使用draw命令的选项legend_grouping = "original"

library(ComplexHeatmap)
library(circlize)

set.seed(123)
mat = matrix(rnorm(96, 2), 8, 12)
mat = rbind(mat, matrix(rnorm(48, -2), 4, 12))
hmap <- as.data.frame(t(mat))

hmap$type <- rep(c("Ctrl", "Cell_type1", "Cell_type2"), 4)
hmap$malig <- ifelse(hmap$type == "Ctrl", "Ctrl", "Tumor")

hmap_bt <- scale(as.matrix(hmap[, -c(13:14)]))
And using this script

df <- data.frame(Case = hmap[, c(13:14)]$malig,
                 Type = hmap[, c(13:14)]$type)

annot <-  rowAnnotation(
  df = data.frame(Case = hmap[, c(13:14)]$malig,
                  Type = hmap[, c(13:14)]$type),
  col = list(Case = c("Ctrl" = "#D1B551", "Tumor" = "#678F53"),
             Type = c("Ctrl" = "#D1B551", "Cell_type1" = "green", 
                      "Cell_type2" = "blue")),
  annotation_legend_param = list(
    Case = list( 
      title_gp = gpar(fontsize = 16, 
                      fontface = "bold"), 
      labels_gp = gpar(fontsize = 16)),
    Type = list( 
      title_gp = gpar(fontsize = 16,
                      fontface = "bold"), 
      labels_gp = gpar(fontsize = 16)))
)

htmp <- Heatmap(
  hmap_bt, 
  name = "Z-score",    
  col = colorRamp2(c(-2, 0, 2), c("#6DBCC3", "white", "#8B3A62")),
  
  show_column_names = FALSE,
  show_column_dend = FALSE,
  column_km = 3,
  
  left_annotation = annot,
  heatmap_legend_param = list(
    legend_direction = "horizontal", 
    legend_width = unit(6, "cm")), 
)

draw(htmp, heatmap_legend_side="bottom", annotation_legend_side="right",
           legend_grouping = "original")

【讨论】:

以上是关于ComplexHeatmap:如何以不同的方式放置热图图例和注释图例?的主要内容,如果未能解决你的问题,请参考以下文章

如何在视图中放置以编程方式生成的位图?

SwiftUI:如何使用语义放置呈现多个 ToolbarItem?

如何向放置在 UIStackView 中的以编程方式创建的 UIButton 添加约束

如何以编程方式将矢量图像放入 iOS 资产目录

如何移动 UIView 并使用 Swift 以编程方式放置新的 UIView?

ios - 如何使用ios swift中的自动布局以编程方式将两个标签并排放置在中心位置?