【问题标题】:Unable to plot hclust object with ggtree无法使用 ggtree 绘制 hclust 对象
【发布时间】:2020-07-10 08:36:43
【问题描述】:

我正在尝试使用 ggtreehclust 对象绘制树状图,但我不断收到相同的错误消息:

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 社区中定义的对象,包括 hclustdendrogram 为 以及集群包中定义的agnesdianatwins

我从上面的链接中借用了一个可重现的例子:

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,因为这似乎是可以通过的。

标签: r hclust ggtree


【解决方案1】:

我已经设法解决了我的问题。我会发布它以防将来对其他人有帮助。

显然,我运行的是旧版本的 ggtree,当我从 Bioconductor 重新安装 ggtree 时,它没有正确更新,我不知道为什么。无论如何,我尝试通过在安装调用中明确设置version 参数来重新安装它:

BiocManager::install("ggtree", version = "3.10")

然后我可以成功运行我的可重现示例:

hc <- hclust(dist(mtcars))
p <- ggtree(hc, linetype='dashed')

请注意,作为以前的解决方法,我设法用ggtree 绘制hcluster 对象,方法是将其转换为phylo 对象,并使用ape 包中的as.phylo() 函数:

hc <- hclust(dist(mtcars))
hc <- ape::as.phylo(hc)
p <- ggtree(hc, linetype='dashed')

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 2018-10-28
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    相关资源
    最近更新 更多