plot_apportionment: Plot Apportionment Results

View source: R/plot_apportionment.R

plot_apportionmentR Documentation

Plot Apportionment Results

Description

Displays the relative contribution of sediment sources to each mixture as density or violin plots. Designed to take the output of unmixing functions directly.

Usage

plot_apportionment(
  data,
  violin = FALSE,
  bounds = c(0, 1),
  scaled = TRUE,
  y_high = 1,
  colors = NULL,
  vlines = NULL,
  title = NULL,
  bw = "nrd"
)

plotResults(
  data,
  violin = FALSE,
  bounds = c(0, 1),
  scaled = TRUE,
  y_high = 1,
  colors = NULL,
  vlines = NULL,
  title = NULL,
  bw = "nrd"
)

plot_results(
  data,
  violin = FALSE,
  bounds = c(0, 1),
  scaled = TRUE,
  y_high = 1,
  colors = NULL,
  vlines = NULL,
  title = NULL,
  bw = "nrd"
)

Arguments

data

A data frame, typically the output from unmix(), containing the relative source contributions. The first two columns must be the mixture identifier (e.g. id) and the goodness-of-fit (gof), followed by one column per source. Only the source columns are plotted.

violin

Logical. If TRUE, violin + boxplot charts are used instead of density plots. Default is FALSE.

bounds

A numeric vector of length 2 specifying the lower and upper display bounds (the camera window). Default is c(0.0, 1.0).

scaled

Logical. If TRUE, density curves are area-scaled to a common maximum. Only used when violin = FALSE. Default is TRUE.

y_high

Maximum value for the y-axis. Only used when violin = FALSE and scaled = FALSE. Default is 1.

colors

An optional named character vector of colors, one per source. Default is NULL.

vlines

An optional named numeric vector specifying true/expected values for specific sources. Draws a dashed vertical line to allow visual validation of the model's accuracy (e.g., c("SourceA" = 0.45)). Default is NULL.

title

Optional character string to add a main title to the plot. Default is NULL.

bw

The smoothing bandwidth to be used. Can be a numeric value (e.g., 0.06) or a character string like "nrd" (the default automatic method).

Value

A ggplot object visualizing the source contribution distributions.

Examples

# Generate mock unmixing output
set.seed(123)
mock_unmix <- data.frame(
  id = 1:100,
  gof = runif(100, 85, 95),
  SourceA = rnorm(100, mean = 0.45, sd = 0.05),
  SourceB = rnorm(100, mean = 0.35, sd = 0.04),
  SourceC = rnorm(100, mean = 0.20, sd = 0.03)
)

# Density Plot
plot_apportionment(mock_unmix, title = "Source Contributions (Density)")

# Violin Plot with true values
true_vals <- c("SourceA" = 0.45, "SourceB" = 0.35, "SourceC" = 0.20)
plot_apportionment(
  mock_unmix, violin = TRUE, 
  vlines = true_vals, 
  title = "Source Contributions (Violin)"
)


OceanTracer documentation built on Oct. 15, 2026, 5:09 p.m.