使用pheatmap按行注释对数据进行排序?

问题描述

我正在尝试创建一个热图,其中包含测试数据列和个别研究参与者的行。参与者可以分为三个不同的组。我想用三组注释该图,然后将每组中的数据聚类以了解它们之间的差异。

我是创建热图的新手,但是我无法使行注释起作用。我也不确定一旦注释生效,如何仅在每个组中进行聚类。我当时以为“ pheatmap.type”包可以工作,但是不幸的是,它不适用于R版本4.0.2。

我无法发布确切的数据(机密信息),但是我已经附加了示例文件,我将描述到目前为止所做的工作并发布代码。我有一个数据框,第一列为标签,其中包括参与者ID和组(使用row.names = 1进行了此设置),然后是12列,包含数字数据(无NA)。然后,我按行名称对数据进行排序,并使用scale函数缩放数据并生成矩阵。然后,我尝试通过以几种不同方式将组信息添加到数据框中来创建注释行。到目前为止,我已经尝试过以下操作:

connection.query(
    'INSERT INTO audits (client,short) VALUES (?,?)',[ client,short ],function ...

任何与此有关的帮助都很棒!

以下是示例数据:

#dataframe with Group and ID as row names and 12 numerical columns  

df_1_HM <- data.frame(df_1$Group_ID,df_1$Test1,df_1$Test2,df_1$Test3,df_1$Test4,df_1$Test5,df_1$Test6,df_1$Test7,df_1$Test8,df_1$Test9,df_1$Test10,df_1$Test11,df_1$Test12,row.names=1)

#ordering the dataframe so that the groups are in order 
df_1_HM_ordered <- df_1_HM[ order(row.names(df_1_HM)),]

#Z-scoring (scaling) data 
df_HM_matrix_1 <- scale(df_1_HM)

#creating a color palette 
my_palette <- colorRampPalette(c("white","grey","black"))(n = 100)


#Plotting heatmap 
install.packages("gplots")
library(gplots)

#trying to plot the heatmap with annotation_row data 
#The method below does not work for me. The plot will run with no errors but does not actually plot - it ends up becoming a list of 4 with no data.

pheatmap(df_HM_matrix_1,scale="none",color=my_palette,fontsize=14,annotation_row=annotation_row)

annotation_row = data.frame(
  df_Group = factor(rep(c("Group 1","Group 2","Group 3"),c(11,10,7)))
)

rownames(annotation_row) = paste("df_Group",1:28,sep = "")

rownames(annotation_row) = rownames(df_HM_matrix_1) # name matching

#I also tried to use a dataframe with just the groups as column 1 to get row annotation 
pheatmap(df_HM_matrix_1,annotation_row=df_Group)

df_Group <- data.frame(df_1$Group,df_1$ID)

#Also tried using the select function to create a dataframe for the row annotation 
df_Group_1 <- select(df_1,Group) 

#When I use either of the data frame methods above I get the following error: Error in cut.default(a,breaks = 100) : 'x' must be numeric

解决方法

要使注释与pheatmap配合使用,必须对因子进行排序。为此,请将ordered = TRUE添加到factor()

annotation_row = data.frame(df_Group = factor(rep(c("Group 1","Group 2","Group 3"),c(11,10,7)),ordered = TRUE))

您还可以使用as.ordered()完成同一件事。

要按注释组对热图行进行排序,只需将参数cluster_rows = F添加到pheatmap()

pheatmap(df_HM_matrix_1,scale="none",color=my_palette,fontsize=14,annotation_row=annotation_row,cluster_rows = F)

这是现在的样子: enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...