我按照正常情况做了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()
这会产生以下热图:
...这是完全错误的.请注意,占位符名称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
变量中时,我希望重现该热图: