我正在尝试使用 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 :
The ggtree package supports most of the hierarchical clustering objects defined in the R community, including
hclust
anddendrogram
as well asagnes
,diana
andtwins
that defined in the cluster package.
我从上面的链接借用了一个可重现的示例:
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, ...)
但我真的能看出问题所在......
最佳答案
我已经成功解决了我的问题。我会将其发布,以防将来对其他人有帮助。
显然,我运行的是旧版本的 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')
关于r - 无法使用 ggtree 绘制 hclust 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60922705/