简体   繁体   English

如何修复 R heatmaply() 中的“因子级别重复”错误?

[英]How to fix “Factor level duplicated” Error in R heatmaply()?

I'm trying to make a heatmap based on that matrix:我正在尝试根据该矩阵制作热图:

  1     2     3     4     5     6     7
C  6211  7608  8089 10514  7363  5375  7268
L  2459  2904  2573  3049  2221  1652  2311
N  3173  4213  3025  4324  2864  1524  2363
S    37    74   141    94    68    48    88
W  1223  1259   914  1691   874   607   912

I made it by:我做到了:

c1 <- table(kat_data$delay_code, kat_data$DayOfWeek)
c1 <- as.matrix(c1)
c1

And now I'm trying to make a heatmap using heatmaply() , but I got an error:现在我正在尝试使用heatmaply()制作热图,但出现错误:

Error in levels<-( tmp `, value = as.character(levels)): factor level [6] is duplicated级别错误<-( tmp `, value = as.character(levels)):因子级别 [6] 重复

Part of the heatmap code below:部分热图代码如下:

p<-heatmaply(c1, 
             dendogram = "none", 
             xlab = "", ylab = "", 
             main = "",
             scale = "column", 
             margins =c(60,100,40,20),............

What should I do to make it work?我应该怎么做才能让它工作? I read a lot of questions with that error and I see that I need to provide unique data, but I don't know where and how to do this.我阅读了很多关于该错误的问题,我发现我需要提供独特的数据,但我不知道在哪里以及如何做到这一点。 Could you please help me?请你帮助我好吗?

We can convert it to data.frame and the error would go away as it is a case of duplicated row names which are not allowed in data.frame我们可以将其转换为data.frame并且错误会 go 消失,因为这是data.frame中不允许的重复行名的情况

library(heatmaply)
heatmaply(as.data.frame.matrix(table(mtcars[c('cyl', 'vs')])))

在此处输入图像描述

Also, to mention that by wrapping with as.matrix , the table class still remains另外,提到通过包装as.matrixtable class 仍然存在

m1 <- as.matrix(table(mtcars[c('cyl', 'vs')]))
str(m1)
# 'table' int [1:3, 1:2] 1 3 14 10 4 0
# - attr(*, "dimnames")=List of 2
#  ..$ cyl: chr [1:3] "4" "6" "8"
#  ..$ vs : chr [1:2] "0" "1"

and that is creating the issue as the ?heatmaply suggests the 'x' to be这正在造成问题,因为?heatmaply建议“x”是

x- can either be a heatmapr object, or a numeric matrix Defaults to TRUE unless x contains any NAs. x- 可以是热图 object,也可以是数字矩阵 默认为 TRUE,除非 x 包含任何 NA。

So, we can convert the class to matrix因此,我们可以将class转换为matrix

class(m1) <- "matrix"

Now, it should work现在,它应该工作

heatmaply(m1)

Note that, either the table or matrix object can result in a similar error as in the OP's post请注意, tablematrix object 都可能导致与 OP 帖子中类似的错误

heatmaply(table(mtcars[c('cyl', 'vs')]))

Error in levels<- ( *tmp* , value = as.character(levels)): factor level [4] is duplicated levels<- ( *tmp* , value = as.character(levels)):因子级别 [4] 重复

heatmaply(as.matrix(table(mtcars[c('cyl', 'vs')])))

Error in levels<- ( *tmp* , value = as.character(levels)): factor level [4] is duplicated levels<- ( *tmp* , value = as.character(levels)):因子级别 [4] 重复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM