PCoA plots
This submodule contains functions for visualising the Principal Coordinates Analysis (PCoA) of PAV matrices computed by
pm.compute_pcoa(). PCoA (classical multidimensional scaling) places samples in a low-dimensional space so that the
distances between them approximate their pairwise Jaccard distances (1 − Jaccard similarity), ignoring shared absences.
Unlike UMAP, PCoA is a linear, deterministic ordination, the proportion of variation in each group can be reported, and distances on the plot relate directly to the distance matrix used by PERMANOVA and dispersion tests.
Prerequisites
You must call pm.compute_pcoa() before using any function documented on this page. See the
PavMatrix API for details. Jaccard distance is not Euclidean, so only axes with positive
eigenvalues are kept, and explained variance is reported relative to the sum of the positive eigenvalues.
PCoA scatter plot
Description
Plots two PCoA axes as a 2D scatter plot, with the percentage of variation explained in each axis title. Each point represents a sample. Colouring works the same way as the UMAP scatter plot.
Function
scatter
Plot two PCoA axes of the samples as a 2D scatter plot.
Points represent samples. Axis titles show the percentage of variance each axis
explains. Colouring behaves identically to pavvis.plots.umap.scatter: discrete
columns produce a categorical legend; continuous columns use a Viridis colorscale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pm
|
PavMatrix
|
A PavMatrix instance with a cached PCoA (call pm.compute_pcoa() first). |
required |
color_by
|
str | None
|
An optional metadata column name to colour points by. Accepts both discrete and continuous columns. Defaults to None. |
None
|
components
|
tuple[int, int]
|
The two 1-based PCoA axes to plot on x and y. Defaults to (1, 2). |
(1, 2)
|
Returns:
| Type | Description |
|---|---|
Figure
|
A Plotly Figure. Call .show() to display or .write_html() to export. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If pm.compute_pcoa() has not been called. |
ValueError
|
If color_by is not a valid metadata column, or if a component exceeds the number of computed axes. |
Example
from pavvis import PavMatrix
import pavvis.plots.pcoa as pcoa
pm = PavMatrix(
matrix_path="path/to/pav.csv",
metadata_path="path/to/metadata.csv",
)
pm.compute_pcoa()
pcoa.scatter(pm, color_by="clade").show()
pcoa.scatter(pm, color_by="clade", components=(1, 3)).show()
Scree plot
Description
Bar chart of the percentage of variation explained by each computed axis. Use it to judge how much of the structure in the distance matrix the plotted axes capture.
Function
scree
Plot the percentage of variance explained by each computed PCoA axis.
Percentages are relative to the sum of all positive eigenvalues, so they sum to less than 100% when only the leading axes are kept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pm
|
PavMatrix
|
A PavMatrix instance with a cached PCoA (call pm.compute_pcoa() first). |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
A Plotly Figure. Call .show() to display or .write_html() to export. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If pm.compute_pcoa() has not been called. |
Example
Group centroids
Description
Plots samples on two PCoA axes with each group's centre (spatial median or mean) and a line from every sample to its
centre, similar in logic to the plot(betadisper(...)) function in the R vegan package. Shaded convex hulls outline
each group.
The plot does not run any permutation test itself. Run permanova and betadisper once
and pass their results: the plot then draws the ordination from the betadisper result and annotates the figure with
both tests, so the numbers on the figure are the same ones you report. Without results, the ordination is computed
without a test and no annotation is added.
Centres are computed in the full PCoA space, as in the dispersion test, and then projected onto the plotted axes.
Function
centroids
centroids(
pm,
column,
components=(1, 2),
hull=True,
betadisper_result=None,
permanova_result=None,
type=None,
)
Plot samples and their group centres in PCoA space, like plot(vegan::betadisper(...)).
Each sample is joined to its group's centre by a line ("spider"), so both the
separation between groups (centre positions) and the spread within groups (line
lengths) are visible. Sample coordinates and centres come from
pavvis.stats.permanova.betadisper, so the plot shows exactly what the dispersion
test measured. Centres are computed in the full PCoA space and projected onto the
two plotted axes, so a centre need not sit in the visual middle of its points.
The plot does not run any permutation test. Pass the results of
pavvis.stats.permanova.betadisper and pavvis.stats.permanova.permanova to
reuse their ordination and annotate the plot with their statistics, so the
figure shows exactly the numbers reported elsewhere.
Clicking a group in the legend toggles its points, lines, centre and hull together. Groups are coloured in sorted order; colours repeat after eight groups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pm
|
PavMatrix
|
A PavMatrix instance. A PCoA does not need to be computed first. |
required |
column
|
str
|
A discrete metadata column defining the groups. |
required |
components
|
tuple[int, int]
|
The two 1-based PCoA axes to plot on x and y. Defaults to (1, 2). |
(1, 2)
|
hull
|
bool
|
If True, shade the convex hull of each group with at least 3 samples. Defaults to True. |
True
|
betadisper_result
|
dict | None
|
The dict returned by |
None
|
permanova_result
|
DataFrame | None
|
The table returned by |
None
|
type
|
Literal['median', 'centroid'] | None
|
Group centre: 'median' (spatial median, vegan's default) or
'centroid' (mean). Defaults to the type of |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
A Plotly Figure. Call .show() to display or .write_html() to export. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If column is not a discrete metadata column with at least 2 groups, if a component exceeds the number of positive PCoA axes, or if a supplied result was computed for a different column, type or PavMatrix. |
Example
import pavvis.plots.pcoa as pcoa
from pavvis.stats.permanova import betadisper, permanova
bd = betadisper(pm, "clade", permutations=9999, random_state=42)
pa = permanova(pm, "clade", permutations=9999, random_state=42)
pcoa.centroids(pm, "clade", betadisper_result=bd, permanova_result=pa).show()
pcoa.centroids(pm, "clade", components=(1, 3), hull=False).show() # no tests, no annotation
Interpretation
Centres far apart with short spider lines indicate groups that differ in location. Long lines in one group and short lines in another indicate unequal dispersion, which the PERMDISP result in the annotation tests formally.
Dispersion boxplot
Description
Distance of each sample to its group centre, one box per group: the equivalent of the R package vegans function:
boxplot(betadisper(...)). Pass the betadisper result to reuse it; otherwise the distances are computed without a
test.
Function
dispersion_boxplot
Plot each sample's distance to its group centre as one box per group.
The equivalent of boxplot(vegan::betadisper(...)). Distances come from
pavvis.stats.permanova.betadisper; run that function for the PERMDISP test
and pass its result here to reuse it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pm
|
PavMatrix
|
A PavMatrix instance. |
required |
column
|
str
|
A discrete metadata column defining the groups. |
required |
betadisper_result
|
dict | None
|
The dict returned by |
None
|
type
|
Literal['median', 'centroid'] | None
|
Group centre: 'median' (spatial median, vegan's default) or
'centroid' (mean). Defaults to the type of |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
A Plotly Figure. Call .show() to display or .write_html() to export. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If column is not a discrete metadata column with at least 2
groups, or if |