3

我正在使用包中的UpSet函数,ComplexHeatmap并且正在尝试向图形添加注释。在非常好的操作方法中,他们描述了在条形旁边绘制计数的额外步骤,并且他们还解释了为什么他们分开这个函数。

因此,文档中的最小示例如下所示(这是我能找到的最小示例):

library(ComplexHeatmap)
movies = read.csv(system.file("extdata", "movies.csv", package = "UpSetR"), 
                  header = TRUE, sep = ";")

m = make_comb_mat(movies, top_n_sets = 6)

m = m[comb_degree(m) > 0]

ss = set_size(m)
cs = comb_size(m)
ht = UpSet(m, 
           set_order = order(ss),
           comb_order = order(comb_degree(m), -cs),
           top_annotation = HeatmapAnnotation(
             "Genre Intersections" = anno_barplot(cs, 
                                                  ylim = c(0, max(cs)*1.1),
                                                  border = FALSE, 
                                                  gp = gpar(fill = "black"), 
                                                  height = unit(4, "cm")
             ), 
             annotation_name_side = "left", 
             annotation_name_rot = 90),
           left_annotation = rowAnnotation(
             "Movies Per Genre" = anno_barplot(-ss, 
                                               baseline = 0,
                                               axis_param = list(
                                                 at = c(0, -500, -1000, -1500),
                                                 labels = c(0, 500, 1000, 1500),
                                                 labels_rot = 0),
                                               border = FALSE, 
                                               gp = gpar(fill = "black"), 
                                               width = unit(4, "cm")
             ),
             set_name = anno_text(set_name(m), 
                                  location = 0.5, 
                                  just = "center",
                                  width = max_text_width(set_name(m)) + unit(4, "mm"))
           ), 
           right_annotation = NULL,
           show_row_names = FALSE)
ht = draw(ht)
od = column_order(ht)
decorate_annotation("Genre Intersections", {
  grid.text(cs[od], x = seq_along(cs), y = unit(cs[od], "native") + unit(2, "pt"), 
            default.units = "native", just = c("left", "bottom"), 
            gp = gpar(fontsize = 6, col = "#404040"), rot = 45)
})

使用最后一个函数decorate_annotation,我们将数字添加到已经绘制的图中,一切都很好。

但是,当我想比较例如 5 个地块并通过%v%垂直连接它们时,如何将它们添加decorate_annotation到所有地块?这里的示例代码:

genre = c("Action", "Romance", "Horror", "Children", "SciFi", "Documentary")
rating = cut(movies$AvgRating, c(0, 1, 2, 3, 4, 5))
m_list = tapply(seq_len(nrow(movies)), rating, function(ind) {
  m = make_comb_mat(movies[ind, genre, drop = FALSE])
  m[comb_degree(m) > 0]
})
m_list = normalize_comb_mat(m_list)

max_set_size = max(sapply(m_list, set_size))
max_comb_size = max(sapply(m_list, comb_size))


ht_list = NULL
for(i in seq_along(m_list)) {
  ht_list = ht_list %v%
    UpSet(m_list[[i]], row_title = paste0("rating in", names(m_list)[i]),
          set_order = NULL, comb_order = NULL,
          top_annotation = upset_top_annotation(m_list[[i]], ylim = c(0, max_comb_size)),
          right_annotation = upset_right_annotation(m_list[[i]], ylim = c(0, max_set_size)))
}
ht_list

有人已经给它添加了注释吗?

4

1 回答 1

1

下面显示的解决方案是基于包的命令as.ggplot。首先定义ggplotify一个函数;plot_upset它绘制了一个带有装饰热图注释的不安图。此函数生成的绘图在ggplot对象中转换as.ggplot并存储在ht_list.

library(ComplexHeatmap)
library(patchwork)
library(ggplotify)

movies <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"), 
                   header = TRUE, sep = ";")

genre <- c("Action", "Romance", "Horror", "Children", "SciFi", "Documentary")
rating <- cut(movies$AvgRating, c(0, 1, 2, 3, 4, 5))
m_list <- tapply(seq_len(nrow(movies)), rating, function(ind) {
  m <- make_comb_mat(movies[ind, genre, drop = FALSE])
  m[comb_degree(m) > 0]
})
m_list <- normalize_comb_mat(m_list)

max_set_size <- max(sapply(m_list, set_size))

plot_upset <- function(m, name, maxSetSize) {
  cs = comb_size(m)  
  ht <- UpSet(m, row_title = paste0("rating in", name),
              set_order = NULL, comb_order = NULL,
              top_annotation = HeatmapAnnotation(
                "Genre Intersections" = anno_barplot(cs, 
                                                     ylim = c(0, max(cs)*1.1),
                                                     border = FALSE, 
                                                     gp = gpar(fill = "black"), 
                                                     height = unit(4, "cm")
                ),
                annotation_name_side = "left", 
                annotation_name_rot = 90),
              right_annotation = upset_right_annotation(m, ylim = c(0, maxSetSize)))
  ht = draw(ht)
  od = column_order(ht)
  decorate_annotation("Genre Intersections", {
    grid.text(cs[od], x = seq_along(cs), y = unit(cs[od], "native") + unit(2, "pt"), 
              default.units = "native", just = c("left", "bottom"), 
              gp = gpar(fontsize = 6, col = "#404040"), rot = 45)
  })
}

ht_list <- vector(2, mode="list")
for(i in seq_along(m_list)) {
  m <- m_list[[i]]
  name_m <- names(m_list)[i]
  dht <- as.ggplot( ~ plot_upset(m, name_m, max_set_size))
  ht_list[[i]] <- dht
} 
patchwork::wrap_plots(ht_list, ncol=1)

这是最终的情节(仅报告了 5 个情节中的 3 个)。 在此处输入图像描述

于 2021-07-18T21:28:34.857 回答