plot.princomp.BData: Plot Method for PCA of Distributional Data

View source: R/PCA.R

plot.princomp.BDataR Documentation

Plot Method for PCA of Distributional Data

Description

A versatile plotting function for visualizing principal component analysis results on distributional data. Provides multiple plot types for comprehensive exploration of the PCA results, including the unique ability to visualize loadings as functional perturbations of the mean distribution.

Usage

## S3 method for class 'princomp.BData'
plot(
  x,
  type = c("screeplot", "variance", "scores", "eigenfunctions", "loadings", "biplot",
    "covariance", "reconstruction", "boxplot"),
  choices = 1:2,
  scale = 1,
  pc.biplot = FALSE,
  main = NULL,
  col = NULL,
  lwd = 1,
  lty = 1,
  legend = TRUE,
  labels = FALSE,
  ...,
  npcs = min(10, length(x$sdev)),
  as = c("PDF", "density", "logPDF", "logDensity", "clr", "coef"),
  scale.sdev = 1
)

Arguments

x

An object of class 'princomp.BData' as returned by princomp.BData.

type

Character string specifying the type of plot to produce:

'"screeplot"'

Bar chart showing the percentage of variance explained by each principal component. Useful for deciding how many components to retain.

'"variance"'

Line plot showing cumulative variance explained as more components are included. Horizontal reference lines at 80%, 90%, and 95% help identify cutoff points.

'"scores"'

Scatter plot of observations in the space of two principal components. Useful for identifying clusters, outliers, or patterns in the data.

'"eigenfunctions"'

Key plot for interpretation. Shows how the distribution shape changes along each principal component direction. The mean distribution is shown along with perturbations in the positive and negative PC directions. This is the standard SFPCA terminology (Hron et al. 2016).

'"loadings"'

Alias for '"eigenfunctions"' (kept for backwards compatibility).

'"biplot"'

Combines scores and eigenfunctions in a single plot. Calls biplot.princomp.BData.

'"covariance"'

Heatmap showing the functional covariance surface. This visualizes which parts of the domain co-vary: high values indicate that when one part of the distribution is above average, the other part tends to be as well. Useful for understanding the correlation structure across the support of the distributions.

'"reconstruction"'

Shows the reconstructed distributions using only the first 'npcs' principal components. Useful for assessing how well a reduced number of components approximates the original data.

'"boxplot"'

Boxplots showing the distribution of scores for each principal component. Useful for visualizing the spread of observations along each PC direction and identifying potential outliers.

choices

Integer vector of length 2 specifying which principal components to plot (for 'type="scores"' and 'type="biplot"'). Default is 'c(1, 2)'.

scale

Scaling factor for loadings in biplot (passed to biplot.princomp.BData). Default is 1.

pc.biplot

Logical. If ‘TRUE', use Gabriel’s biplot scaling. Default is 'FALSE'.

main

Character string for plot title. If 'NULL', an appropriate default is generated based on the plot type.

col

Colors for plotting. For loadings plot: first color is for the mean, subsequent colors for each PC. For scores plot: color for points/labels.

lwd

Line width for functional plots (loadings). Default is 1.

lty

Line type (currently not directly used; solid for positive direction, dashed for negative).

legend

Logical. If 'TRUE' (default), add a legend to the loadings plot.

labels

For 'type="scores"': Controls labeling of observations:

'FALSE'

(Default) Show only points, no labels.

'TRUE'

Show observation numbers (1, 2, 3, ...) instead of points.

character vector

Use custom labels for each observation.

This helps identify which distribution each point represents.

...

Additional graphical parameters passed to underlying plot functions.

npcs

Number of principal components to display in screeplot, variance plot, or loadings plot. Default is 'min(10, length(x$sdev))'.

as

For loadings plot: how to display the distributions. One of:

'"PDF"'

Probability density function (default). Shows distributions as proper densities that integrate to 1.

'"density"'

Density with respect to the reference measure.

'"logPDF"'

Log of the probability density function.

'"logDensity"'

Log density with respect to reference measure.

'"clr"'

Centered log-ratio transformation. Shows the additive structure in Bayes space. The clr is centered around zero, making it easy to see positive and negative deviations from the geometric mean. This is the natural representation for Bayes space PCA.

'"coef"'

Raw spline coefficients (parallel coordinates plot). Useful for seeing which coefficients drive each PC.

scale.sdev

Numeric scaling factor for the loadings plot. Controls how far from the mean the perturbed distributions are shown:

'1'

Show distributions at ±1 standard deviation from mean (default).

'2'

Show distributions at ±2 standard deviations (more extreme).

'NA'

Show unit loadings without scaling by standard deviation.

Details

## Understanding the Eigenfunctions Plot

The 'type="eigenfunctions"' plot is the most important visualization for interpreting SFPCA results on distributional data. It answers the question: "How does the distribution shape change as we move along each principal component?"

The plot shows:

  • Black thick line: The mean distribution (center of the data).

  • Colored solid lines: Distributions at +'scale.sdev' standard deviations from the mean along each PC direction (positive perturbation).

  • Colored dashed lines: Distributions at -'scale.sdev' standard deviations from the mean along each PC direction (negative perturbation).

For example, if PC1 captures variation in the "location" of distributions, the PC1+ line will be shifted right and PC1- will be shifted left compared to the mean. If PC1 captures variation in "spread", PC1+ will be wider and PC1- will be narrower.

## Choosing the Number of Components

Use the screeplot and variance plot to decide how many components are meaningful:

  • Screeplot: Look for an "elbow" where the variance drops off. Components before the elbow are typically retained.

  • Variance plot: Choose enough components to explain a target percentage (e.g., 80% or 90%) of total variance.

Value

Invisibly returns the data used for plotting (varies by plot type).

Author(s)

Matthias Templ

See Also

princomp.BData for performing the PCA.

biplot.princomp.BData for detailed control over biplots.

screeplot.princomp.BData for the standalone screeplot function.

Other PCA: biplot.princomp.BData(), plot.princomp.BData.grouped(), plot.princomp.BData.multivariate(), predict.princomp.BData(), princomp.BData(), screeplot.princomp.BData(), summary.princomp.BData()

Examples

## Setup: Create PCA result for examples
meanDist <- BDataPDFs(list(dunif), basis = B2Unif01Basis7)
gaussProc <- B2GaussianDistribution(meanDist, function(x,y) exp(-4*abs(x-y)^1.7))
pcaResult <- princomp(as.BDataSplines(simulate(gaussProc, 30, seed = 42)))

## Screeplot: Decide how many components to keep
plot(pcaResult, type = "screeplot")
# Interpretation: The bars show the percentage of total variance explained by
# each PC. Here, PC1 and PC2 dominate, suggesting that the main structure in
# the data can be captured by just two components. The "elbow" after PC2 or
# PC3 indicates where additional components add little explanatory power.

## Cumulative variance: What percentage is explained?
plot(pcaResult, type = "variance")
# Interpretation: This shows how total explained variance accumulates as more
# components are included. The dashed reference lines help identify how many
# components are needed to reach common thresholds (80%, 90%, 95%). Here,
# approximately 3 components explain over 90% of the variance.

## Score plot: Explore structure in the data
plot(pcaResult, type = "scores")
# Interpretation: Each point is one distribution projected onto PC1 and PC2.
# The spread along PC1 (x-axis) shows variation in the first mode; spread
# along PC2 (y-axis) shows the second mode. Clusters indicate groups of
# similar distributions; outliers are distributions with unusual shapes.

## Score plot with labels: Identify individual distributions
plot(pcaResult, type = "scores", labels = TRUE)
# Using labels=TRUE replaces points with observation numbers (1, 2, 3, ...),
# allowing you to identify which specific distribution each position
# represents. This is useful for tracking outliers or interesting cases
# back to the original data.

## Eigenfunctions plot: THE KEY INTERPRETIVE PLOT
plot(pcaResult, type = "eigenfunctions", npcs = 2)
# Interpretation: The black curve is the mean (average) distribution. For
# each PC, solid lines show how distributions look when they score HIGH on
# that component, dashed lines show LOW scores. Compare the colored curves
# to the black mean to see what "high PC1" or "low PC2" means in terms of
# actual distribution shape (e.g., shifted location, changed spread, altered
# skewness, or different modality).

## More extreme perturbations (2 standard deviations)
plot(pcaResult, type = "eigenfunctions", npcs = 2, scale.sdev = 2)
# Using scale.sdev=2 shows more extreme deviations from the mean, making
# the differences between PC directions more visible.

## Show loadings as log-PDFs (useful for seeing tail behavior)
plot(pcaResult, type = "loadings", npcs = 2, as = "logPDF")
# The log-PDF representation emphasizes differences in the tails of the
# distributions, which can be important for detecting outlier behavior.

## Show loadings in CLR (centered log-ratio) representation
plot(pcaResult, type = "loadings", npcs = 2, as = "clr")
# The CLR representation shows the additive structure in Bayes space.
# Values are centered around zero, making it easy to see which parts of the
# distribution are above or below the geometric mean. This is the natural
# representation for PCA in Bayes space.

## Covariance surface: See which parts of the domain co-vary
plot(pcaResult, type = "covariance")
# Interpretation: This heatmap shows functional covariance Cov(f(s), f(t))
# across the domain. High values (red) indicate that when one part of the
# distribution is above average, the other tends to be as well. The diagonal
# shows variance at each point. Off-diagonal patterns reveal correlation
# structure (e.g., if left and right tails move together or oppositely).

## Reconstruction: How well do k PCs approximate the data?
plot(pcaResult, type = "reconstruction", npcs = 2)
# Shows all distributions reconstructed using only the first 2 PCs.
# Compare to the original data to assess information loss.
# If reconstructions look similar to originals, 2 PCs are sufficient.

plot(pcaResult, type = "reconstruction", npcs = 1)
# Using only 1 PC shows more loss of detail - useful for understanding
# what information each additional PC captures.

## Boxplot: Distribution of scores across PCs
plot(pcaResult, type = "boxplot", npcs = 4)
# Shows the spread of observations along each PC direction.
# Useful for identifying outliers (points beyond whiskers) and comparing
# the amount of variation captured by each component. The percentage
# labels show variance explained by each PC.


DistributionalDDA documentation built on Oct. 20, 2026, 5:06 p.m.