当输入是连续列数可变的数据框时,将 pch 添加到 rowAnnotation

问题描述 投票:0回答:1

跟进这个问题,我在一个函数中使用

ComplexHeatmap
,该函数将我想要研究的变量名称作为输入。

虽然之前我发现对列的离散分组存在问题,但现在问题不同了。我想绘制附加的连续列(存储在不同的数据框中),并具有与其关联的重要性

*
(作为 pch)。

我通常知道如何使用

rowAnnotation
anno_simple
执行此操作,但在这种情况下(函数内的
ComplexHeatmap
),我不知道要添加的额外列的名称(或数量!) ,我不知道。

我希望上一个问题的答案也适用于这个,我几乎得到了它,但还没有完全实现......

以下是我的MWE。

我首先定义我的输入、热图矩阵

heat_mat
、带有要添加的额外列的
extra_df
,以及要应用的调色板:

data(iris)
extra_cols <- c('zscore1', 'zscore2') #they can be 1, 2, 3... n
extra_pch <- c('pvalue1','pvalue2') #they can be 1, 2, 3... n
iris_sub <- rbind(iris[iris[,'Species']=='setosa',][1:5,],
                  iris[iris[,'Species']=='versicolor',][1:5,],
                  iris[iris[,'Species']=='virginica',][1:5,])
heat_mat <- as.matrix(iris_sub[,-ncol(iris_sub)])
#
extra_df <- data.frame(ID=paste0("id",rownames(iris_sub)))
for (i in 1:length(extra_cols)){
  edf <- data.frame(VAR=rnorm(15))
  names(edf) <- extra_cols[i]
  pdf <- data.frame(VAR=runif(15, 0, 0.2))
  pdf$VAR <- ifelse(pdf$VAR<0.05, '*', NA)
  names(pdf) <- extra_pch[i]
  extra_df <- cbind(extra_df, edf, pdf)
}
rownames(heat_mat) <- extra_df$ID
#
palette1 <- RColorBrewer::brewer.pal(n=9, name="RdYlGn")
palette2 <- RColorBrewer::brewer.pal(n=9, name="RdYlBu")
#
cols_list <- list()
for (nm in extra_cols){
  breaks <- seq(from=min(extra_df[,nm]), to=max(extra_df[,nm]), length.out=length(palette2))
  cols <- circlize::colorRamp2(breaks, palette2)
  cols_list <- append(cols_list, cols)
  names(cols_list)[length(cols_list)] <- nm
}

现在我知道如何使用

rowAnnotation
anno_simple
正常制作热图,如下所示:

row_ha <- ComplexHeatmap::rowAnnotation(
  zscore1 = ComplexHeatmap::anno_simple(
    extra_df[,extra_cols[1]], col = cols_list[[extra_cols[1]]],
    pch = extra_df[,extra_pch[1]]),
  zscore2 = ComplexHeatmap::anno_simple(
    extra_df[,extra_cols[2]], col = cols_list[[extra_cols[2]]],
    pch = extra_df[,extra_pch[2]]))
#
complex_heat <- ComplexHeatmap::Heatmap(heat_mat, name = "value", cluster_columns = FALSE,
                                        col = palette1,
                                        right_annotation = row_ha,
                                        border = TRUE)
grDevices::png(filename="test.png", height=600, width=400)
ComplexHeatmap::draw(complex_heat)
grDevices::dev.off()

这会产生以下图,这正是我所需要的(不担心这个例子中缺少图例):

但是,如前所述,在我当前的情况下(函数内的

ComplexHeatmap
),我不知道要添加的额外列的名称(或数量!)。

我想添加可变数量的连续列,其关联的 pch 为

rowAnnotation
,其名称存储在 2 个变量中。

这是我最好的尝试(应用上一个问题的答案),但是用这种方法我无法通过

pch

row_df <- extra_df[,extra_cols,drop=F]
row_pch <- extra_df[,extra_pch,drop=F]
row_ha <- ComplexHeatmap::rowAnnotation(
  df = row_df,
  col = cols_list,
  #pch = row_pch,
  show_legend = FALSE)
#
complex_heat <- ComplexHeatmap::Heatmap(heat_mat, name = "value", cluster_columns = FALSE,
                                        col = palette1,
                                        right_annotation = row_ha,
                                        border = TRUE)
grDevices::png(filename="test2.png", height=600, width=400)
ComplexHeatmap::draw(complex_heat)
grDevices::dev.off()

这会产生以下情节,与之前的情节相同,但没有我需要的

*
...知道如何继续吗?

r annotations heatmap pch complexheatmap
1个回答
0
投票

我认为在这种情况下,生成第二个热图并将其绘制在主图旁边可能会更容易。其代码如下所示:

data(iris)
extra_cols <- c('zscore1', 'zscore2') #they can be 1, 2, 3... n
extra_pch <- c('pvalue1','pvalue2') #they can be 1, 2, 3... n
iris_sub <- rbind(iris[iris[,'Species']=='setosa',][1:5,],
                  iris[iris[,'Species']=='versicolor',][1:5,],
                  iris[iris[,'Species']=='virginica',][1:5,])
heat_mat <- as.matrix(iris_sub[,-ncol(iris_sub)])
#
extra_df <- data.frame(ID=paste0("id",rownames(iris_sub)))
for (i in 1:length(extra_cols)){
  edf <- data.frame(VAR=rnorm(15))
  names(edf) <- extra_cols[i]
  pdf <- data.frame(VAR=runif(15, 0, 0.2))
  pdf$VAR <- ifelse(pdf$VAR<0.05, '*', NA)
  names(pdf) <- extra_pch[i]
  extra_df <- cbind(extra_df, edf, pdf)
}
rownames(heat_mat) <- extra_df$ID

palette1 <- RColorBrewer::brewer.pal(n=9, name="RdYlGn")
palette2 <- RColorBrewer::brewer.pal(n=9, name="RdYlBu")

cols_list <- list()
for (nm in extra_cols){
  breaks <- seq(from=min(extra_df[,nm]), to=max(extra_df[,nm]), length.out=length(palette2))
  cols <- circlize::colorRamp2(breaks, palette2)
  cols_list <- append(cols_list, cols)
  names(cols_list)[length(cols_list)] <- nm
}

secondary_heatmap_data <- as.matrix(extra_df[, extra_cols])
rownames(secondary_heatmap_data) <- extra_df$ID

main_heatmap <- ComplexHeatmap::Heatmap(heat_mat, name = "value", cluster_columns = FALSE,
                                        col = palette1,
                                        #right_annotation = row_ha,
                                        border = TRUE)
secondary_heatmap <- ComplexHeatmap::Heatmap(secondary_heatmap_data,
  cluster_columns = FALSE,
  layer_fun = function(j, i, x, y, width, height, fill) {
    # since grid.text can also be vectorized
    grid.text(ifelse(pindex(secondary_heatmap_data, i, j) < 0.05, "*", ""), x, y,
      gp = gpar(fontsize = 11, col = "black") # adjust as needed
    )
  },
  border = FALSE,
  width = unit(1, "cm"),
  show_heatmap_legend = FALSE
)

ht_list <- main_heatmap + secondary_heatmap
draw(ht_list, main_heatmap = "value")

这会生成以下热图,它看起来与使用

anno_simple
创建的热图非常相似:

© www.soinside.com 2019 - 2024. All rights reserved.