| Title: | Plots for Compositional Data |
|---|---|
| Description: | Provides a collection of easy-to-use functions for creating visualizations of compositional data using 'ggplot2'. Includes support for common plotting techniques in compositional data analysis. |
| Authors: | Marc Comas-Cufí [aut, cre] (ORCID: <https://orcid.org/0000-0001-9759-0622>) |
| Maintainer: | Marc Comas-Cufí <[email protected]> |
| License: | GPL |
| Version: | 0.2.2 |
| Built: | 2026-05-12 08:34:14 UTC |
| Source: | https://github.com/cran/coda.plot |
Add a ternary grid layer
add_ternary_grid(p, ticks = seq(0.1, 0.9, 0.1), n = 300, eps = 1e-06, ...)add_ternary_grid(p, ticks = seq(0.1, 0.9, 0.1), n = 300, eps = 1e-06, ...)
p |
A |
ticks |
Numeric vector of grid levels. |
n |
Number of sampled points per grid line. |
eps |
Small positive offset used before log-ratio transformation. |
... |
Further arguments passed to |
A ggplot2 object.
Add a compositional path to a ternary plot
add_ternary_path(p, X, group = NULL, transform = TRUE, ...)add_ternary_path(p, X, group = NULL, transform = TRUE, ...)
p |
A |
X |
A numeric matrix or data frame with exactly three columns. |
group |
Optional grouping variable of length |
transform |
Logical. If |
... |
Further arguments passed to |
A ggplot2 object.
Add principal component paths to a ternary plot
add_ternary_pc( p, X, group = NULL, pcs = 1:2, basis = coda.base::ilr_basis(3), n = 600, eps = 0.001, ... )add_ternary_pc( p, X, group = NULL, pcs = 1:2, basis = coda.base::ilr_basis(3), n = 600, eps = 0.001, ... )
p |
A |
X |
A numeric matrix or data frame with exactly three columns. |
group |
Optional grouping variable of length |
pcs |
Integer vector indicating which principal components to draw. |
basis |
An ilr basis. Default is |
n |
Number of sampled points per PC path. |
eps |
Small positive threshold used to keep the path inside the simplex. |
... |
Further arguments passed to |
A ggplot2 object.
Add compositional points to a ternary plot
add_ternary_points(p, X, group = NULL, transform = TRUE, ...)add_ternary_points(p, X, group = NULL, transform = TRUE, ...)
p |
A |
X |
A numeric matrix or data frame with exactly three columns. |
group |
Optional grouping variable of length |
transform |
Logical. If |
... |
Further arguments passed to |
A ggplot2 object.
Plots a balance dendrogram based on a compositional data set and a corresponding balance matrix. This visualization helps interpret the structure of balances in compositional data analysis.
balance_dendrogram(X, B, group = NULL)balance_dendrogram(X, B, group = NULL)
X |
A numeric matrix or data frame representing the compositional data. Rows are observations and columns are components (must be strictly positive). |
B |
A numeric matrix representing the balance basis (e.g., an isometric log-ratio (ilr) balance matrix). |
group |
Optional. If provided, show grouped box summaries under each node. |
A ggplot2 object representing the balance dendrogram.
# Simulated compositional data and balances X = matrix(runif(50, 1, 10), ncol = 5) colnames(X) = LETTERS[1:5] B = coda.base::pb_basis(X, method = 'exact') balance_dendrogram(X, B)# Simulated compositional data and balances X = matrix(runif(50, 1, 10), ncol = 5) colnames(X) = LETTERS[1:5] B = coda.base::pb_basis(X, method = 'exact') balance_dendrogram(X, B)
Generates a centered log-ratio (CLR) biplot for compositional data.
clr_biplot( X, group = NULL, biplot_type = "covariance", alpha = NULL, shape_group = NULL, return_data = FALSE, repel = TRUE, repel_force = 1, repel_max_overlaps = Inf )clr_biplot( X, group = NULL, biplot_type = "covariance", alpha = NULL, shape_group = NULL, return_data = FALSE, repel = TRUE, repel_force = 1, repel_max_overlaps = Inf )
X |
A matrix or data frame containing compositional data (strictly positive). |
group |
Optional factor/character used to color the observations. |
biplot_type |
Character string specifying the type of biplot. Either
|
alpha |
Optional numeric in [0,1]. If provided, overrides
|
shape_group |
Optional factor/character used to assign shapes to observations. |
return_data |
Logical. If TRUE, returns a list with data frames for observations, variables, and the ggplot object. |
repel |
Logical. If TRUE (default), use ggrepel for variable labels when available. |
repel_force |
Numeric. Repulsion force passed to |
repel_max_overlaps |
Numeric. Maximum overlaps allowed (ggrepel). |
A ggplot2 object. If return_data = TRUE, a list with elements
obs, vars, and plot.
# Basic example (no groups) set.seed(1) X <- matrix(runif(120, 0.1, 10), ncol = 6) colnames(X) <- paste0("p", 1:6) clr_biplot(X) # Grouped example (color) grp <- factor(sample(c("A", "B"), nrow(X), replace = TRUE)) clr_biplot(X, group = grp) # Color + shape shp <- factor(sample(c("S1", "S2", "S3"), nrow(X), replace = TRUE)) clr_biplot(X, group = grp, shape_group = shp) # Form biplot (alpha = 1) with repelled variable labels (requires ggrepel) clr_biplot(X, group = grp, biplot_type = "form", repel = TRUE) # Covariance biplot (alpha = 0) and custom repel settings clr_biplot(X, group = grp, alpha = 0, repel = TRUE, repel_force = 1.5, repel_max_overlaps = 30)# Basic example (no groups) set.seed(1) X <- matrix(runif(120, 0.1, 10), ncol = 6) colnames(X) <- paste0("p", 1:6) clr_biplot(X) # Grouped example (color) grp <- factor(sample(c("A", "B"), nrow(X), replace = TRUE)) clr_biplot(X, group = grp) # Color + shape shp <- factor(sample(c("S1", "S2", "S3"), nrow(X), replace = TRUE)) clr_biplot(X, group = grp, shape_group = shp) # Form biplot (alpha = 1) with repelled variable labels (requires ggrepel) clr_biplot(X, group = grp, biplot_type = "form", repel = TRUE) # Covariance biplot (alpha = 0) and custom repel settings clr_biplot(X, group = grp, alpha = 0, repel = TRUE, repel_force = 1.5, repel_max_overlaps = 30)
Easy to run function to built visualisations by means of 'ggplot2' objects.
Marc Comas-Cufí
Generates a barplot based on the geometric mean of compositional parts. Optionally, it can compare groups, display the parts on the x-axis, overlay boxplots, or use centered log-ratio (clr) transformation.
geometric_mean_barplot( X, group, x_show_parts = TRUE, include_boxplot = FALSE, clr_scale = FALSE )geometric_mean_barplot( X, group, x_show_parts = TRUE, include_boxplot = FALSE, clr_scale = FALSE )
X |
A numeric matrix or data frame representing compositional data. Each row is an observation and each column is a part (must be strictly positive). |
group |
A factor or character vector indicating group membership for each observation.
Must have length |
x_show_parts |
Logical. If |
include_boxplot |
Logical. If |
clr_scale |
Logical. If |
For each part, the function computes (within each group) the mean of either log(X) (default)
or clr(X) (clr_scale = TRUE), and subtracts the overall mean across all observations.
Therefore, bars represent deviations from the overall (global) mean on the chosen scale.
Overlaying a boxplot can help visualize within-group variability.
A ggplot2 object representing the geometric mean barplot.
set.seed(1) X <- matrix(runif(30, 1, 10), ncol = 3) colnames(X) <- c("A", "B", "C") group <- rep(c("G1", "G2"), each = 5) geometric_mean_barplot(X, group, include_boxplot = TRUE) geometric_mean_barplot(X, group, clr_scale = TRUE)set.seed(1) X <- matrix(runif(30, 1, 10), ncol = 3) colnames(X) <- c("A", "B", "C") group <- rep(c("G1", "G2"), each = 5) geometric_mean_barplot(X, group, include_boxplot = TRUE) geometric_mean_barplot(X, group, clr_scale = TRUE)
Convert a compositional dataset into ternary plotting coordinates under a given
ternary_frame.
ternary_coords(frame, X, transform = TRUE, group = NULL)ternary_coords(frame, X, transform = TRUE, group = NULL)
frame |
A |
X |
A numeric matrix or data frame with exactly three columns. |
transform |
Logical. If |
group |
Optional grouping variable of length |
A data frame with compositional columns c1, c2, c3,
ternary coordinates .x, .y, and optionally group.
Create a ternary diagram from compositional data with exactly three parts. Optionally center and/or scale the data in log-ratio coordinates, color points by group, and overlay the first two principal component directions computed in ilr coordinates.
ternary_diagram( X, group = NULL, center = FALSE, scale = FALSE, show_pc = FALSE )ternary_diagram( X, group = NULL, center = FALSE, scale = FALSE, show_pc = FALSE )
X |
A numeric matrix or data frame with exactly three columns (the parts of the composition). Values should be positive. Column names (if present) are used as corner labels. |
group |
Optional. A factor or character vector of length |
center |
Logical. If |
scale |
Logical or numeric. If |
show_pc |
Logical. If |
This function is kept as a convenient wrapper around the modular ternary API:
ternary_frame(), ternary_plot(), add_ternary_points(), and
add_ternary_pc().
A ggplot2 object.
ternary_frame, ternary_plot,
add_ternary_points, add_ternary_pc
X <- milk_cows[, 5:7] group <- milk_cows$group ternary_diagram(X, group = group) ternary_diagram(X, group = group, center = TRUE, scale = TRUE) ternary_diagram(X, group = group, center = TRUE, scale = 1.5) ternary_diagram(X, show_pc = TRUE)X <- milk_cows[, 5:7] group <- milk_cows$group ternary_diagram(X, group = group) ternary_diagram(X, group = group, center = TRUE, scale = TRUE) ternary_diagram(X, group = group, center = TRUE, scale = 1.5) ternary_diagram(X, show_pc = TRUE)
Build a ternary plotting frame for compositional data with exactly three parts. The returned object stores the geometric and log-ratio transformation machinery needed to add multiple data layers consistently to the same ternary diagram.
ternary_frame(X, center = FALSE, scale = FALSE, labels = NULL)ternary_frame(X, center = FALSE, scale = FALSE, labels = NULL)
X |
A numeric matrix or data frame with exactly three columns. This data defines the reference frame used for centering/scaling in log-ratio space. |
center |
Logical. If |
scale |
Logical or numeric. If |
labels |
Optional character vector of length 3 used as corner labels.
If |
An object of class "ternary_frame".
Create the base ggplot object associated with a ternary_frame.
ternary_plot( frame = NULL, show_grid = TRUE, show_outline = TRUE, show_labels = TRUE, grid_ticks = seq(0.1, 0.9, 0.1) )ternary_plot( frame = NULL, show_grid = TRUE, show_outline = TRUE, show_labels = TRUE, grid_ticks = seq(0.1, 0.9, 0.1) )
frame |
Optional |
show_grid |
Logical. If |
show_outline |
Logical. If |
show_labels |
Logical. If |
grid_ticks |
Numeric vector of grid levels. |
A ggplot2 object with the ternary_frame attached as attribute.