【发布时间】:2020-07-10 08:36:43
【问题描述】:
我正在尝试使用 ggtree 从 hclust 对象绘制树状图,但我不断收到相同的错误消息:
Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class hclust
我一直在广泛寻找解决方案,但没有找到。此外,我了解到ggtree does support hclust 对象,这让我更加困惑。来自here:
ggtree 包支持大部分层次聚类 在 R 社区中定义的对象,包括
hclust和dendrogram为 以及集群包中定义的agnes、diana和twins。
我从上面的链接中借用了一个可重现的例子:
hc <- hclust(dist(mtcars))
p <- ggtree(hc, linetype='dashed')
这又给了我上述错误。如果我使用rlang::last_error() 来获取一些上下文,我会得到:
<error/rlang_error>
`data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class hclust
Backtrace:
1. ggtree::ggtree(hc, linetype = "dashed")
3. ggplot2:::ggplot.default(...)
5. ggplot2:::fortify.default(data, ...)
如果我使用 rlang::last_trace() 来获取更多信息:
<error/rlang_error>
`data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class hclust
Backtrace:
x
1. \-ggtree::ggtree(hc, linetype = "dashed")
2. +-ggplot2::ggplot(...)
3. \-ggplot2:::ggplot.default(...)
4. +-ggplot2::fortify(data, ...)
5. \-ggplot2:::fortify.default(data, ...)
但我真的可以看出哪里不对了……
【问题讨论】:
-
所以,最终,你的
data是一个data.frame,因为这似乎是可以通过的。