| vismat | R Documentation |
vismat provides visualization tools for relationship matrices (A, D, AA),
supporting individual-level heatmaps and relationship coefficient histograms.
This function is useful for exploring population genetic structure, identifying
inbred individuals, and analyzing kinship between families.
vismat(
mat,
ped = NULL,
type = "heatmap",
ids = NULL,
reorder = TRUE,
by = NULL,
grouping = NULL,
labelcex = NULL,
...
)
mat |
A relationship matrix. Can be one of the following types:
Note: Inverse matrices (Ainv, Dinv, AAinv) are not supported for visualization because their elements do not represent meaningful relationship coefficients. |
ped |
Optional. A tidied pedigree object ( |
type |
Character, type of visualization. Supported options:
|
ids |
Character vector specifying individual IDs to display. Used to
filter and display a submatrix of specific individuals. If |
reorder |
Logical. If Clustering principle: Based on relationship profile distance (Euclidean distance between rows). Full-sibs have nearly identical relationship profiles with the whole population, so they cluster tightly together and appear as contiguous blocks in the heatmap. |
by |
Optional. Column name in
Useful for visualizing population structure in large pedigrees. |
grouping |
|
labelcex |
Numeric. Manual control for font size of individual labels.
If |
... |
Additional arguments passed to the underlying plotting function:
|
When mat is a compact pedmat object (created with
pedmat(..., compact = TRUE)):
With by: Group-level mean relationships are computed
algebraically from the K×K compact matrix, including a correction for
sibling off-diagonal values. This avoids expanding to the full N×N
matrix, making family-level or generation-level visualization feasible
even for pedigrees with hundreds of thousands of individuals.
Without by, N > VISMAT_EXPAND_MAX (5 000): The compact K\timesK matrix
is plotted directly using representative individuals. Labels show the
number of individuals each representative stands for, e.g.,
"ID (\u00d7350)". This avoids memory-intensive full expansion.
Without by, N \le 5 000: The compact matrix is
expanded via expand_pedmat to restore full dimensions.
Uses a Nature Genetics style color palette (white to orange to red to dark red).
Hierarchical clustering reordering (Ward.D2) is enabled by default.
Grid lines shown when N \le VISMAT_GRID_MAX (100);
labels shown when N \le VISMAT_LABEL_MAX (500).
mat[1,1] is displayed at the top-left corner.
Shows the distribution of lower-triangular elements (pairwise kinship).
X-axis: relationship coefficient values; Y-axis: frequency percentage.
The following automatic thresholds are defined as package-internal
constants (VISMAT_*) at the top of R/vismat.R:
VISMAT_EXPAND_MAX (5 000): compact matrices with original
N above this are shown in representative view instead of expanding.
VISMAT_REORDER_MAX (2 000): hierarchical clustering is
automatically skipped.
VISMAT_LABEL_MAX (500): individual labels are hidden.
VISMAT_GRID_MAX (100): cell grid lines are hidden.
by grouping uses vectorized rowsum() algebra — suitable
for large matrices.
For the additive relationship matrix A:
Diagonal elements = 1 + F (F = inbreeding coefficient).
Off-diagonal elements = 2 × kinship coefficient.
0: unrelated; 0.25: half-sibs / grandparent–grandchild; 0.5: full-sibs / parent–offspring; 1.0: same individual.
Invisibly returns the lattice plot object. The plot is
rendered on the current graphics device.
pedmat for computing relationship matrices,
expand_pedmat for manually restoring compact matrix dimensions,
query_relationship for querying individual pairs,
tidyped for tidying pedigree data,
visped for visualizing pedigree structure graphs,
levelplot, histogram
library(visPedigree)
data(small_ped)
ped <- tidyped(small_ped)
# ============================================================
# Basic Usage
# ============================================================
# Method 1: from tidyped object (auto-computes A)
vismat(ped)
# Method 2: from pedmat object
A <- pedmat(ped)
vismat(A)
# Method 3: from plain matrix
vismat(as.matrix(A))
# ============================================================
# Compact Pedigree (auto-expanded before plotting)
# ============================================================
# For pedigrees with large full-sib families, compute a compact matrix
# first for efficiency, then pass directly to vismat() — it automatically
# expands back to full dimensions.
A_compact <- pedmat(ped, compact = TRUE)
vismat(A_compact) # prints: "Expanding compact matrix (N -> M individuals)"
# For very large pedigrees, aggregate to a group-level view instead
vismat(A, ped = ped, by = "Gen",
main = "Mean Relationship Between Generations")
# ============================================================
# Heatmap Customization
# ============================================================
# Custom title and axis labels
vismat(A, main = "Additive Relationship Matrix",
xlab = "Individual", ylab = "Individual")
# Preserve original pedigree order (no clustering)
vismat(A, reorder = FALSE)
# Custom label font size
vismat(A, labelcex = 0.5)
# Custom color palette (blue-white-red)
vismat(A, col.regions = colorRampPalette(c("blue", "white", "red"))(100))
# ============================================================
# Display a Subset of Individuals
# ============================================================
target_ids <- rownames(A)[1:8]
vismat(A, ids = target_ids)
# ============================================================
# Histogram of Relationship Coefficients
# ============================================================
vismat(A, type = "histogram")
vismat(A, type = "histogram", nint = 30)
# ============================================================
# Group-level Aggregation
# ============================================================
# Group by generation
vismat(A, ped = ped, by = "Gen",
main = "Mean Relationship Between Generations")
# Group by full-sib family (founders without a family are excluded)
vismat(A, ped = ped, by = "Family")
# ============================================================
# Other Relationship Matrices
# ============================================================
# Dominance relationship matrix
D <- pedmat(ped, method = "D")
vismat(D, main = "Dominance Relationship Matrix")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.