I am trying to plot a dendrogram from a hclust
object with ggtree
but I keep getting the same error message:
Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class hclust
I have been extensively looking for a solution, but found none. Furthermore, I have learned that ggtree
does support hclust
objects, which has confused me even more. From 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.
I have borrowed a reproducible example from the link above:
hc <- hclust(dist(mtcars))
p <- ggtree(hc, linetype='dashed')
Which, again, gives me the abovementioned error. If I use rlang::last_error()
to get a bit of context, I get:
<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, ...)
And if I use rlang::last_trace() to get a bit further information:
<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, ...)
But I can really see what is wrong...