热图生成R中的cut.default错误

  • 本文关键字:cut default 错误 中的 r
  • 更新时间 :
  • 英文 :


我想从8*6数据帧生成热图。数据帧中的最后一行具有用于注释列的信息。数据帧的结构如下:

heatmap_try <-structure(list(BGC0000041 = structure(c(1L, 2L, 1L, 1L, 1L, 3L
), .Label = c("0", "0.447458977", "a"), class = "factor"), BGC0000128 = structure(c(1L, 
1L, 1L, 3L, 2L, 4L), .Label = c("0", "1.785875195", "4.093659107", 
"a"), class = "factor"), BGC0000287 = structure(c(1L, 1L, 1L, 
3L, 2L, 4L), .Label = c("0", "1.785875195", "4.456229186", "b"
), class = "factor"), BGC0000294 = structure(c(3L, 1L, 2L, 4L, 
1L, 5L), .Label = c("0", "2.035046947", "3.230553742", "3.286304185", 
"b"), class = "factor"), BGC0000295 = structure(c(1L, 1L, 1L, 
2L, 1L, 3L), .Label = c("0", "2.286304185", "c"), class = "factor"), 
BGC0000308 = structure(c(4L, 2L, 3L, 5L, 1L, 6L), .Label = c("6.277728291", 
"6.313707588", "6.607936616", "6.622871165", "6.64385619", 
"c"), class = "factor"), BGC0000323 = structure(c(1L, 2L, 
1L, 1L, 1L, 3L), .Label = c("0", "0.447458977", "c"), class = "factor"), 
BGC0000328 = structure(c(1L, 2L, 1L, 1L, 1L, 3L), .Label = c("0", 
"0.447458977", "c"), class = "factor")), class = "data.frame", row.names = c("Gut", 
"Oral", "Anterior_nares", "Retroauricular_crease", "Vagina", 
"AL"))

我的热图生成代码如下(我使用的是pheatmap库(:

library(pheatmap)
heatmap_data1 <- heatmap_try[ c(1:5), c(1:8) ]
anotation_data <- as.data.frame(t(heatmap_try[6, ]))
row.names(anotation_data) <- colnames(heatmap_data1)
pheatmap(heatmap_data1, annotation_col = anotation_data, color = colorRampPalette(c("white","blue"))(n=100),cellwidth = 40,cellheight = 6,fontsize_row = 5,cluster_rows = F,cluster_cols = F)

然而,我得到了以下错误:

Error in cut.default(x, breaks = breaks, include.lowest = T) : 
'x' must be numeric

我做错了什么?谢谢

这是因为heatmap_data1的列是因子,它们需要是数字。一种转换方式是:

heatmap_data1_num <- as.data.frame(lapply(heatmap_data1,
function(x) as.numeric(as.character(x))))
# then as before
pheatmap(heatmap_data1_num, annotation_col = anotation_data, color = colorRampPalette(c("white","blue"))(n=100),cellwidth = 40,cellheight = 6,fontsize_row = 5,cluster_rows = F,cluster_cols = F)

最新更新