我按照正常情况做了ComplexHeatmap,现在唯一的区别是我将它放在一个函数中,该函数将我想要研究的变量名称作为输入(在下面的MWE中:Species).

请参阅MWE中的my_var变量.从字面上看,我唯一想做的就是能够将这个变量作为名称分配给HeatmapAnnotation对象(定义列注释).

我无法执行以下操作,因为变量的名称(在本例中是Species)存储在my_var中:

column_ha <- ComplexHeatmap::HeatmapAnnotation(
  Species = meta_df$Species,
  col = list(Species = col_vector))

相反,我try 使用占位符名称,然后try 将column_ha名称更改为my_var,如下所示:

col_list <- list(VAR = col_vector)
names(col_list) <- my_var
column_ha <- ComplexHeatmap::HeatmapAnnotation(
  VAR = meta_df[,my_var],
  col = col_list)
names(column_ha) <- my_var

这没有任何作用.我可以在热图中看到VAR,物种的 colored颜色 都混乱了.

这是我的MWE:

data(iris)
my_var <- 'Species'
iris_sub <- rbind(iris[iris[,my_var]=='setosa',][1:5,],
                  iris[iris[,my_var]=='versicolor',][1:5,],
                  iris[iris[,my_var]=='virginica',][1:5,])
heat_mat <- t(as.matrix(iris_sub[,-ncol(iris_sub)]))
#
meta_df <- data.frame(ID=paste0("id",rownames(iris_sub)), VAR=iris_sub[,ncol(iris_sub)])
names(meta_df)[2] <- my_var
colnames(heat_mat) <- meta_df$ID
#
palette1 <- RColorBrewer::brewer.pal(n=9, name="RdYlGn")
col_vector <- RColorBrewer::brewer.pal(3, 'Set1')
col_vector <- stats::setNames(col_vector, levels(meta_df[,my_var]))
#
col_list <- list(VAR = col_vector)
names(col_list) <- my_var
column_ha <- ComplexHeatmap::HeatmapAnnotation(
  VAR = meta_df[,my_var],
  col = col_list)
names(column_ha) <- my_var
#
complex_heat <- ComplexHeatmap::Heatmap(heat_mat, name = "value", cluster_columns = FALSE,
                                        col = palette1,
                                        top_annotation = column_ha,
                                        border = TRUE)
grDevices::png(filename="test.png", height=400, width=600)
ComplexHeatmap::draw(complex_heat)
grDevices::dev.off()

这会产生以下热图:

test1

...这是完全错误的.请注意,占位符名称VAR仍然存在,并且顶部列注释中的物种 colored颜色 全部错误(不是col_vector,应该是Set1红、蓝、绿).

所以问题是:如何为HeatmapAnnotation对象分配新名称?

请注意,如果我没有将名称Species存储在my_var变量中,那么它工作得很好,所以我在代码中没有看到问题.

请参阅此MWE,了解最终热图的外观(这里它有效,因为我没有将名称Species存储在my_var变量中):

data(iris)
iris_sub <- rbind(iris[iris[,'Species']=='setosa',][1:5,],
                  iris[iris[,'Species']=='versicolor',][1:5,],
                  iris[iris[,'Species']=='virginica',][1:5,])
heat_mat <- t(as.matrix(iris_sub[,-ncol(iris_sub)]))
#
meta_df <- data.frame(ID=paste0("id",rownames(iris_sub)), Species=iris_sub[,ncol(iris_sub)])
colnames(heat_mat) <- meta_df$ID
#
palette1 <- RColorBrewer::brewer.pal(n=9, name="RdYlGn")
col_vector <- RColorBrewer::brewer.pal(3, 'Set1')
col_vector <- stats::setNames(col_vector, levels(meta_df$Species))
#
column_ha <- ComplexHeatmap::HeatmapAnnotation(
  Species = meta_df$Species,
  col = list(Species = col_vector))
#
complex_heat <- ComplexHeatmap::Heatmap(heat_mat, name = "value", cluster_columns = FALSE,
                                        col = palette1,
                                        top_annotation = column_ha,
                                        border = TRUE)
grDevices::png(filename="test.png", height=400, width=600)
ComplexHeatmap::draw(complex_heat)
grDevices::dev.off()

这将生成以下正确的热图,当我将名称Species存储在my_var变量中时,我希望重现该热图:

test2

推荐答案

你们非常接近.您可以将收件箱传递为HeatmapAnnotation,对于收件箱,将列名更改为my_var非常容易.

library(ComplexHeatmap)
data(iris)
my_var <- 'Species'
iris_sub <- rbind(iris[iris[,my_var]=='setosa',][1:5,],
                  iris[iris[,my_var]=='versicolor',][1:5,],
                  iris[iris[,my_var]=='virginica',][1:5,])
heat_mat <- t(as.matrix(iris_sub[,-ncol(iris_sub)]))
#
meta_df <- data.frame(ID=paste0("id",rownames(iris_sub)), VAR=iris_sub[,ncol(iris_sub)])
names(meta_df)[2] <- my_var
colnames(heat_mat) <- meta_df$ID
#
palette1 <- RColorBrewer::brewer.pal(n=9, name="RdYlGn")
col_vector <- RColorBrewer::brewer.pal(3, 'Set1')
col_vector <- stats::setNames(col_vector, levels(meta_df[,my_var]))
#
col_list <- list(VAR = col_vector)
names(col_list) <- my_var
column_ha_df <- data.frame("place_holder" = meta_df[,my_var])
colnames(column_ha_df) <- my_var
column_ha <- ComplexHeatmap::HeatmapAnnotation(
  df = column_ha_df,
  col = col_list
)

complex_heat <- ComplexHeatmap::Heatmap(heat_mat, name = "value", cluster_columns = FALSE,
                                        col = palette1,
                                        top_annotation = column_ha,
                                        border = TRUE)

draw(complex_heat)

R相关问答推荐

在R中使用自定义函数时如何删除该函数的一部分?

导入到固定列宽的R中时出现问题

如何根据包含相同值的某些列获取总额

使用spatVector裁剪网格数据时出现的问题

使用R的序列覆盖

带有叠加饼图系列的Highmap

如何改变时间图R中的悬停信息?

如何在观测缺失的地方添加零

如何从R ggplot图片中获取SVG字符串?

如何在分组条形图中移动相关列?

仅 Select 超过9行的CSV文件

如何识别倒排的行并在R中删除它们?

根据列表中项目的名称合并数据框和列表

正在导出默认的RStudio主题,还是设置括号 colored颜色 ?

R -如何分配夜间GPS数据(即跨越午夜的数据)相同的开始日期?

如何在使用箭头R包(箭头::OPEN_DATASSET)和dplyr谓词时编写具有整齐计算的函数?

远离理论值的伽马密度曲线下面积的近似

删除字符串R中的重复项

计算使一组输入值最小化的a、b和c的值

数据集上的R循环和存储模型系数