我正在按照正常方式执行
ComplexHeatmap
,现在唯一的区别是我将它放在一个函数内,该函数将我想要研究的变量名称作为输入(在下面的MWE中:Species
)。
请参阅 MWE 中的
my_var
变量。从字面上看,我唯一想做的就是能够将此变量作为名称分配给 HeatmapAnnotation
对象(定义列注释)。
我无法执行以下操作,因为变量的名称(在本例中为
Species
)存储在my_var
中:
column_ha <- ComplexHeatmap::HeatmapAnnotation(
Species = meta_df$Species,
col = list(Species = col_vector))
相反,我尝试做的是使用占位符名称,然后尝试用
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,并且物种的颜色都乱了。
这是我的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 仍然存在,并且顶部列注释中的物种颜色全部错误(不是
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
变量中时,我想重现该热图:
你们非常亲密。您可以将数据框传递给
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)