Ggdendro
Tools to extract dendrogram plot data for use with 'ggplot2'
Install / Use
/learn @andrie/GgdendroREADME
ggdendro <img src='man/figures/logo.png' align="right" height="139" />
Provides functions for creating dendrograms and tree plots using
ggplot2.
The ggdendro package offers a generic function to extract data and
text from the various clustering models:
dendro_data()extracts cluster information from the model object, e.g. cluster allocation, line segment data or label data.
The dendro_data object has methods for the following classes:-
treehclustdendrogramrpart
These methods create an object of class dendro, which is essentially a
list of data frames. To extract the relevant data frames from the list,
use the three accessor functions:
segment()for the line segment datalabel()for the text for each end segmentleaf_label()for the leaf labels of a tree diagram
The results of these functions can then be passed to ggplot() for
plotting.
Examples
library(ggplot2)
library(ggdendro)
hc <- hclust(dist(USArrests), "ave")
hcdata <- dendro_data(hc, type = "rectangle")
ggplot() +
geom_segment(data = segment(hcdata),
aes(x = x, y = y, xend = xend, yend = yend)
) +
geom_text(data = label(hcdata),
aes(x = x, y = y, label = label, hjust = 0),
size = 3
) +
coord_flip() +
scale_y_reverse(expand = c(0.2, 0))
<img src="man/figures/README-unnamed-chunk-2-1.png" width="100%" />
### demonstrate plotting directly from object class hclust
ggdendrogram(hc)
<img src="man/figures/README-unnamed-chunk-2-2.png" width="100%" />
ggdendrogram(hc, rotate = TRUE)
<img src="man/figures/README-unnamed-chunk-2-3.png" width="100%" />
### demonstrate converting hclust to dendro using dendro_data first
hcdata <- dendro_data(hc)
ggdendrogram(hcdata, rotate = TRUE) +
labs(title = "Dendrogram in ggplot2")
<img src="man/figures/README-unnamed-chunk-2-4.png" width="100%" />
Use dendextend instead
Most of the functionality in ggdendro is included in the excellent
dendextend package. In most cases, if you need additional
functionality, please use the dendextend package instead.
The ggdendro package will only get minimal maintenance in future.
Refer to https://cran.r-project.org/web/packages/dendextend/index.html
Related Skills
node-connect
337.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
337.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.2kCommit, push, and open a PR
