r - 无法使用 ggtree 绘制 hclust 对象

标签 r hclust ggtree

我正在尝试使用 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 :

The ggtree package supports most of the hierarchical clustering objects defined in the R community, including hclust and dendrogram as well as agnes, diana and twins 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')

enter image description here

请注意,作为之前的解决方法,我成功地使用 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/

相关文章:

ggtree 2.4.2 错误 : Error in DataMask$new(. 数据,caller_env) : argument "caller_env" is missing, 无默认值

r - ggjoy 方面与 ggtree

r - 在 `facet_wrap` ed网格中,将子图居中于0,同时保持 `free_x`

r - 你如何在 ggplot 中将文本放在不同的行上

r - 更改 R 3.01 中聚类树状图的标签大小

r - 在 R 中使用 hclust 进行加权观察频率聚类

r - 在 r 中为带有彩色叶子的树状图创建图例

r - R 中的神经网络包

r - 如何在 R 中执行具有共享参数的非线性最小二乘法?