https://github.com/tdaverse/ggtda
ggplot2 extension to visualize persistent homology
https://github.com/tdaverse/ggtda
ggplot-extension ggplot2 persistence-data persistent-homology r rstats simplicial-complex tda tidyverse topological-data-analysis visualization
Last synced: 15 days ago
JSON representation
ggplot2 extension to visualize persistent homology
- Host: GitHub
- URL: https://github.com/tdaverse/ggtda
- Owner: tdaverse
- License: gpl-3.0
- Created: 2018-08-08T03:15:13.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2025-04-24T20:57:39.000Z (27 days ago)
- Last Synced: 2025-04-24T21:50:58.345Z (27 days ago)
- Topics: ggplot-extension, ggplot2, persistence-data, persistent-homology, r, rstats, simplicial-complex, tda, tidyverse, topological-data-analysis, visualization
- Language: R
- Homepage: https://tdaverse.github.io/ggtda/
- Size: 45.4 MB
- Stars: 23
- Watchers: 3
- Forks: 6
- Open Issues: 9
-
Metadata Files:
- Readme: README.Rmd
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
dev = "png", dpi = 300
)
```# ggtda
[](https://codecov.io/github/tdaverse/ggtda?branch=main)
[](https://www.gnu.org/licenses/gpl-3.0)[](https://CRAN.R-project.org/package=ggtda)
[](https://CRAN.R-project.org/package=ggtda)## Overview
The **ggtda** package provides **ggplot2** layers for the visualization of constructions and statistics arising from topological data analysis.
## Installation
The development version can be installed used the **remotes** package:
```r
# install from GitHub
remotes::install_github("tdaverse/ggtda", vignettes = TRUE)
```For an introduction to package functionality, read the vignettes:
```r
# read vignettes
vignette(topic = "visualize-persistence", package = "ggtda")
vignette(topic = "illustrate-constructions", package = "ggtda")
vignette(topic = "grouped-list-data", package = "ggtda")
```We aim to submit to [CRAN](https://CRAN.R-project.org) in Spring 2024!
## Example
```{r}
# attach {ggtda}
library(ggtda)
```### Sample data set
This example illustrates **ggtda** features using an artificial point cloud $X$ sampled with noise from a circle:
```{r, fig.height=3}
# generate a noisy circle
n <- 36
set.seed(0)
t <- stats::runif(n = n, min = 0, max = 2*pi)
d <- data.frame(
x = cos(t) + stats::rnorm(n = n, mean = 0, sd = .2),
y = sin(t) + stats::rnorm(n = n, mean = 0, sd = .2)
)
# plot the data
ggplot(d, aes(x, y)) + geom_point() + coord_equal() + theme_bw()
```### Topological constructions
**ggtda** provides stat and geom layers for common TDA constructions.
To illustrate, pick a proximity, or threshold, to consider points in the cloud to be neighbors:```{r}
# choose a proximity threshold
prox <- 2/3
```The homology $H_k(X)$ of a point cloud is uninteresting ($H_0(X) = \lvert X \rvert$ and $H_k(X) = 0$ for $k > 0$). The most basic space of interest to the topological data analyst is the union of a _ball cover_ $B_r(X)$ of $X$---a ball of common radius $r$ around each point. The common radius will be $r =$ `prox / 2`.
The figure below compares the ball cover (left) with the _Vietoris_ (or _Rips_) _complex_ ${VR}_r(X)$ constructed using the same proximity (right).
The complex comprises a simplex at each subset of points having diameter at most `prox`---that is, each pair of which are within `prox` of each other.
A key result in TDA is that the homology of the ball union is "very close" to that of the complex.```{r, fig.height=3}
# visualize disks of fixed radii and the Vietoris complex for this proximity
p_d <- ggplot(d, aes(x = x, y = y)) +
coord_fixed() +
geom_disk(radius = prox/2, fill = "aquamarine3") +
geom_point() +
theme_bw()
p_sc <- ggplot(d, aes(x = x, y = y)) +
coord_fixed() +
stat_simplicial_complex(diameter = prox, fill = "darkgoldenrod") +
theme_bw() +
theme(legend.position = "none")
# combine the plots
gridExtra::grid.arrange(
p_d, p_sc,
layout_matrix = matrix(c(1, 2), nrow = 1)
)
```This cover and simplex clearly contain a non-trivial 1-cycle (loop), which makes $H_1(B_r(X)) = H_1({VR}_r(X)) = 1$.
But detecting this feature depended crucially on the choice of `prox`, and there's no guarantee with new data that this choice will be correct or even that a single best choice exists.
Instead, we tend to be interested in considering those features that persist across many values of `prox`.
The GIF below[^TDAvis] illustrates this point: Observe how features appear and disappear as the disk covers grow:[^TDAvis]: The GIF and many features of **ggtda** were originally developed in the separate package [**TDAvis**](https://github.com/jamesotto852/TDAvis).
### Persistent homology
Persistent homology (PH) encodes the homology group ranks across the full range $0 \leq r < \infty$, corresponding to the full filtration of simplicial complexes constructed on the point cloud.
We use [**ripserr**](https://cran.r-project.org/package=ripserr) to compute the PH of the point cloud $X$:```{r}
# compute the persistent homology
ph <- ripserr::vietoris_rips(as.matrix(d), dim = 1)
print(ph)
```The loop is detected, though we do not yet know whether its persistence stands out from that of other features.
To prepare for `ggplot()`, we convert the result to a data frame and its numeric `dimension` column to a factor:```{r}
pd <- as.data.frame(ph)
pd <- transform(pd, dimension = as.factor(dimension))
head(pd)
tail(pd)
```### Persistence plots
**ggtda** also provides stat and geom layers for common visualizations of persistence data.
We visualize these data using a barcode (left) and a persistence diagram (right).
In the barcode, the dashed line indicates the cutoff at the proximity `prox`; in the persistence diagram, the fundamental box contains the features that are detectable at this cutoff.```{r, fig.height=3}
# visualize the persistence data, indicating cutoffs at this proximity
p_bc <- ggplot(pd, aes(start = birth, end = death)) +
geom_barcode(linewidth = 1, aes(color = dimension, linetype = dimension)) +
labs(x = "Diameter", y = "Homological features",
color = "Dimension", linetype = "Dimension") +
geom_vline(xintercept = prox, color = "darkgoldenrod", linetype = "dotted") +
theme_barcode()
max_prox <- max(pd$death)
p_pd <- ggplot(pd) +
coord_fixed() +
stat_persistence(aes(start = birth, end = death,
colour = dimension, shape = dimension)) +
geom_abline(slope = 1) +
labs(x = "Birth", y = "Death", color = "Dimension", shape = "Dimension") +
lims(x = c(0, max_prox), y = c(0, max_prox)) +
geom_fundamental_box(
t = prox,
fill = "darkgoldenrod", color = "transparent"
) +
theme_persist()
# combine the plots
gridExtra::grid.arrange(
p_bc, p_pd,
layout_matrix = matrix(c(1, 2), nrow = 1)
)
```The barcode lines are color- and linetype-coded by feature dimension: the 0-dimensional features, i.e. the gaps between connected components, versus the 1-dimensional feature, i.e. the loop.
These groups of lines do not overlap, which means that the loop exists only in the persistence domain where all the data points are part of the same connected component. Our choice of `prox` is between the birth and death of the loop, which is why the complex above recovers it.The persistence diagram shows that the loop persists for longer than any of the gaps. This is consistent with the gaps being artifacts of the sampling procedure but the loop being an intrinsic property of the underlying space.
### Multiple data sets
TDA usually involves comparisons of topological data between spaces.
To illustrate such a comparison, we construct a larger sample and examine the persistence of its cumulative subsets:```{r}
# larger point cloud sampled from a noisy circle
set.seed(0)
n <- 180
t <- stats::runif(n = n, min = 0, max = 2*pi)
d <- data.frame(
x = cos(t) + stats::rnorm(n = n, mean = 0, sd = .2),
y = sin(t) + stats::rnorm(n = n, mean = 0, sd = .2)
)
# list of cumulative point clouds
ns <- c(12, 36, 60, 180)
dl <- lapply(ns, function(n) d[seq(n), ])
```First we construct a nested data frame containing these subsets and plot their Vietoris complexes.
(We specify the [**simplextree**](https://github.com/peekxc/simplextree) engine and restrict to 2-simplices to reduce runtime.)```{r, fig.height=5}
# formatted as grouped data
dg <- do.call(rbind, dl)
dg$n <- rep(ns, vapply(dl, nrow, 0L))
# faceted plots of cumulative simplicial complexes
ggplot(dg, aes(x, y)) +
coord_fixed() +
facet_wrap(facets = vars(n), labeller = label_both) +
stat_simplicial_complex(
diameter = prox, dimension_max = 2L,
engine = "simplextree",
fill = "darkgoldenrod"
) +
theme_bw() +
theme(legend.position = "none")
```The Vietoris complexes on these subsets for the fixed proximity are not a filtration; instead they show us how increasing the sample affects the detection of homology at that threshold.
Notice that, while a cycle exists at $n = 36$, the "true" cycle is only detected at $n = 60$.We can also conveniently plot the persistence diagrams from all four cumulative subsets, this time using a list-column of data sets passed to the `dataset` aesthetic:
```{r, fig.height=5}
# nested data frame of samples of different cumulative sizes
ds <- data.frame(n = ns, d = I(dl))
print(ds)
# faceted plot of persistence diagrams
ggplot(ds, aes(dataset = d)) +
coord_fixed() +
facet_wrap(facets = vars(n), labeller = label_both) +
stat_persistence(aes(colour = after_stat(factor(dimension)),
shape = after_stat(factor(dimension)))) +
geom_abline(slope = 1) +
labs(x = "Birth", y = "Death", color = "Dimension", shape = "Dimension") +
lims(x = c(0, max_prox), y = c(0, max_prox)) +
theme_persist()
```The diagrams reveal that a certain sample is necessary to distinguish bona fide features from noise, as only occurs here at $n = 36$.
While the true feature retains about the same persistence (death value less birth value) from diagram to diagram, the persistence of the noise gradually lowers.## Contribute
To contribute to **ggtda**, you can create issues for any bugs you find or any suggestions you have on the [issues page](https://github.com/tdaverse/ggtda/issues).
If you have a feature in mind you think will be useful for others, you can also [fork this repository](https://help.github.com/en/articles/fork-a-repo) and [create a pull request](https://help.github.com/en/articles/creating-a-pull-request).