https://andrie.github.io/ggdendro/
Tools to extract dendrogram plot data for use with 'ggplot2'
https://andrie.github.io/ggdendro/
ggplot2 r r-package r-stats
Last synced: about 11 hours ago
JSON representation
Tools to extract dendrogram plot data for use with 'ggplot2'
- Host: GitHub
- URL: https://andrie.github.io/ggdendro/
- Owner: andrie
- Created: 2011-08-06T20:35:39.000Z (over 14 years ago)
- Default Branch: main
- Last Pushed: 2025-11-19T13:10:19.000Z (7 days ago)
- Last Synced: 2025-11-19T15:15:00.890Z (7 days ago)
- Topics: ggplot2, r, r-package, r-stats
- Language: R
- Homepage: http://andrie.github.io/ggdendro/
- Size: 13.4 MB
- Stars: 86
- Watchers: 7
- Forks: 12
- Open Issues: 4
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
Awesome Lists containing this project
- awesome-ggplot2 - ggdendro
README
---
output: github_document
format: gfm
default-image-extension: ""
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# ggdendro 
Provides functions for creating dendrograms and tree plots using `ggplot2`.
[](https://CRAN.R-project.org/package=ggdendro)
[](http://www.r-pkg.org/pkg/ggdendro)
[](https://github.com/andrie/ggdendro/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/andrie/ggdendro?branch=main)
[](https://www.repostatus.org/#inactive)
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:-
* `tree`
* `hclust`
* `dendrogram`
* `rpart`
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 data
* `label()` for the text for each end segment
* `leaf_label()` for the leaf labels of a tree diagram
The results of these functions can then be passed to `ggplot()` for plotting.
## Examples
```{r}
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))
### demonstrate plotting directly from object class hclust
ggdendrogram(hc)
ggdendrogram(hc, rotate = TRUE)
### demonstrate converting hclust to dendro using dendro_data first
hcdata <- dendro_data(hc)
ggdendrogram(hcdata, rotate = TRUE) +
labs(title = "Dendrogram in ggplot2")
```
# 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