Circular dendrogram
Suggested enhancement by Ulrik Stervbo
Hi Andrie,
Thanks for taking the time with the ggdendro package.
I wanted to plot a circular dendrogram and was unhappy with the implementation in Ape (the leaf order is mirrored). I realized I could achieve my goal by modifying the segments of the dendro_data result and use the coord_polar().
The benefit is, that the dendrogram can then be "downwards" and "rightwards" (the two current variations with rotate = FALSE and rotate=TRUE respectively) but also "upwards" and "leftwards" (to stay in the terminology from ape).
To achieve the last two variants I simply modified the y-values:
hcdata$segments[["y"]] <- abs(hcdata$segments[["y"]] - max(hcdata$segments[["y"]]))
hcdata$segments[["yend"]] <- abs(hcdata$segments[["yend"]] - max(hcdata$segments[["yend"]]))
Example:
hc <- hclust(dist(USArrests), "ave")
plot(hc)
### demonstrate converting hclust to dendro using dendro_data first
hcdata <- dendro_data(hc)
ggdendrogram(hcdata, rotate=FALSE, size=2) + labs(title="Dendrogram in ggplot2 - Rotate=F; segments not modified; downwards")
ggdendrogram(hcdata, rotate=TRUE, size=2) + labs(title="Dendrogram in ggplot2 - Rotate=T; segments not modified; rightwards")
hcdata$segments[["y"]] <- abs(hcdata$segments[["y"]] - max(hcdata$segments[["y"]]))
hcdata$segments[["yend"]] <- abs(hcdata$segments[["yend"]] - max(hcdata$segments[["yend"]]))
ggdendrogram(hcdata, rotate=TRUE, size=2) + labs(title="Dendrogram in ggplot2- Rotate=T; segments modified; leftwards")
ggdendrogram(hcdata, rotate=FALSE, size=2) + labs(title="Dendrogram in ggplot2- Rotate=F; segments modified; upwards")
ggdendrogram(hcdata) + labs(title="Dendrogram in ggplot2 - Fan") + coord_polar()
May I suggest you deprecate the rotate and replace it with a direction = c("downwards", "upwards", "rightwards", "leftwards") and add a type = c("phylogram", "fan").
if the type is fan, then the above modification is performed and the returned plot is simply
ggdendrogram() + coord_polar()
Please let me know if something is not clear. I could also extend the code, but I dont want to make too much of a mess.
All the best, Ulrik
There are two problems in implementing this:
-
ggplot2doesn't allow for axes to be at top or right of plot area. Thus, as currently imlemented, the leaf labels get disconnected from the segments. - To implement the fan plot, one will have to (somehow) specify individual rotation of the leaf labels in the axis.
Thus an implementation requires a modification of ggdendrogram() to use text labels rather than axis labels.
Hi @andrie, Since ggdend (from the dendextend package) works with geom_text, it supports the fan plot, you can see it here: https://cran.r-project.org/web/packages/dendextend/vignettes/introduction.html#ggplot2-integration
Also, it is worth noting the wonderful package "circlize", which deals with creating fan plots (also of dendrograms). For more simpler cases, dendextend offers a wrapper called circlize_dendrogram, which gives a fan plot "out of the box" for a dendrogram (but in base R, not ggplot2). You can see an example for it here:
https://cran.r-project.org/web/packages/dendextend/vignettes/introduction.html#circlize
Cheers, Tal
Hi @andrie,
First of all nice package! I think I'll use your package to chart dendrograms in highcharter via the generic function hchart(http://jkunst.com/highcharter/hchart.html). What will you do with this issue? To know if I implement the Ulrik idea or wait some changes in ggdendro.
Finally, no phylo?
Regards.