Skip to the content.

PenguinoidRTools

R

A collection of useful R functions for statistical analysis and colour science.

Installation

# Install from GitHub
# install.packages("devtools")
devtools::install_github("GavinDuley/PenguinoidRTools")

Functions

ANOVA Summary Functions

aovSummaryTable(aov_data, group_var, output_file, return_raw, output_name)

Generates a comprehensive summary table of ANOVA results for a whole dataframe without interaction terms.

Parameters:

Output: A summary table containing:

Example:

library(agricolae)
library(PenguinoidRTools)

# Load example data
data(greenhouse, package = "agricolae")
aov_data <- greenhouse$greenhouse1

# Run analysis
result <- aovSummaryTable(
  aov_data = aov_data,
  group_var = "variety",
  output_file = "results.xlsx",
  return_raw = TRUE
)
print(result)

aovInteractSummaryTable(aov_data, group_vars, output_file, return_raw, output_name)

Generates a summary table of ANOVA results including interaction terms for two, three, or four-way factorial designs.

Parameters:

Output: A summary table containing:

Sums of squares and unbalanced designs: P-values and F-values use Type II sums of squares – each effect is tested after all other effects that do not contain it, against the residual mean square of the full model – so the results do not depend on the order of group_vars. In a balanced design (the same number of observations in every combination of the grouping variables) these are identical to the sequential (Type I) results of summary(aov()). In an unbalanced design they differ, and a warning names the affected variables. A design can be balanced overall but unbalanced for a particular variable, because observations with an NA in that variable are dropped for its ANOVA. Group means and Tukey letters are unaffected.

Example:

library(agricolae)
library(PenguinoidRTools)

# Load example data
data(greenhouse, package = "agricolae")
greenhouse1_data <- greenhouse$greenhouse1

# Two-way ANOVA with interaction
result <- aovInteractSummaryTable(
  aov_data = greenhouse1_data,
  group_vars = c("variety", "method"),
  output_file = "interaction_results.xlsx"
)
print(result)

CIELab Colour Functions

R functions for calculating CIELab colour coordinates from spectrophotometry data using the OIV-MA-AS2-11 (R2006) method with D65 illuminant and 10 degree standard observer.

cielab_spectroscopy_percentage_to_fraction(transmission_pct)

Converts spectrophotometry transmission data from percentage (0-100) to fraction (0-1) format.

Example:

# Convert 50% transmission to fraction
cielab_spectroscopy_percentage_to_fraction(50)
# Returns: 0.5

# Convert a vector of percentages
cielab_spectroscopy_percentage_to_fraction(c(10, 50, 90))
# Returns: c(0.1, 0.5, 0.9)

cielab_from_spectrum(data, sample_col, path_mm)

Converts spectrophotometry transmission data to CIELab colour values using the OIV method. Automatically detects data format - just pass your dataframe and get CIELab values back.

Input: A data.frame in either format:

  1. Wide format (typical from spectrophotometers):
    • Sample identifier column (e.g., WineName, Sample)
    • Wavelength columns named X780, X779, … X380
    • Values as transmission fraction (0-1)
  2. Long format:
    • Sample identifier column (e.g., WineName)
    • lambda - Wavelength in nm
    • T - Transmission as fraction (0-1)

Parameters:

Output: A data.frame with one row per unique value of sample_col:

Note: Transmission values slightly > 1 (e.g., 1.003) are normal instrument noise and ignored. Only values > 1.1 trigger a warning.

Example:

# Wide format - just pass your data directly
result <- cielab_from_spectrum(spectroscopy_data)
# Returns one row per WineName, replicates averaged

# Get individual results per replicate (no averaging)
result <- cielab_from_spectrum(spectroscopy_data, sample_col = "Replicate")

# Long format works too
result <- cielab_from_spectrum(spectroscopy_long)

# Use different path length
result <- cielab_from_spectrum(my_data, path_mm = 5)

lab_to_C(a, b)

Calculate chroma (C) from CIELab a and b* values.

Example:

lab_to_C(30, 40)  # Returns 50

hue_deg(a, b)

Calculate hue angle in degrees from CIELab a* and b* values.

Example:

hue_deg(1, 1)   # Returns 45
hue_deg(-1, 1)  # Returns 135

delta_h_signed(h2, h1)

Calculate signed hue difference, correctly handling wraparound at 360 degrees.

Example:

delta_h_signed(10, 350)   # Returns 20 (crossing 0)
delta_h_signed(350, 10)   # Returns -20

deltaE76(L1, a1, b1, L2, a2, b2)

Calculate CIE76 colour difference (Delta E) between two colours.

Delta E interpretation (rough):

CIE76 Delta E is unbounded and not perceptually uniform; for perceptually uniform differences use the CIE2000 method (calc_pairwise_dE(..., method = "CIE2000")).

Example:

deltaE76(50, 10, 20, 55, 15, 25)

deltaE00(L1, a1, b1, L2, a2, b2) / deltaE94(L1, a1, b1, L2, a2, b2)

Standalone CIE2000 and CIE94 colour differences between two colours, as peers of deltaE76(). Arguments are recycled to a common length and compared pairwise. Both delegate to farver::compare_colour(). Like all Delta E metrics, these always compare a pair of colours.

Example:

deltaE00(50, 10, 20, 55, 15, 25)
deltaE94(50, 10, 20, 55, 15, 25)

cielab_to_hex(L, a, b, fixup = TRUE)

Convert CIELab coordinates to hexadecimal sRGB colour strings (e.g. for plotting). Requires the colorspace package.

Example:

cielab_to_hex(c(30, 90), c(50, -5), c(30, 10))
# Returns e.g. c("#8F0F1B", "#E1E5CF")

calc_colour_diff(data, compare_by, reference_level, group_by)

Calculate centroid-to-centroid colour differences between groups. Computes the mean L, a, b* for each group and returns differences (dL, dC, dh, dE76, dE00) relative to a reference level.

Parameters:

Output: A data.frame with columns:

Example:

# Compare ethanol treatments to control
results <- calc_colour_diff(
  data = wine_colours,
  compare_by = "EtOH",
  reference_level = "CTRL"
)

# Compare within each wine variety
results <- calc_colour_diff(
  data = wine_colours,
  compare_by = "Treatment",
  reference_level = "Control",
  group_by = "WineType"
)

calc_pairwise_dE(data, compare_by, reference_level, target_levels, group_by, method)

Calculate mean pairwise colour differences between all observations in different groups. This gives a distribution of Delta E values rather than a single centroid-based value.

Parameters:

Output: A data.frame with columns:

Example:

# Compare all treatments to control using CIE76
results <- calc_pairwise_dE(
  data = wine_colours,
  compare_by = "EtOH",
  reference_level = "CTRL"
)

# Use CIE2000 for more perceptually uniform differences
results <- calc_pairwise_dE(
  data = wine_colours,
  compare_by = "Treatment",
  reference_level = "Control",
  method = "CIE2000"
)

# Compare specific treatments only
results <- calc_pairwise_dE(
  data = wine_colours,
  compare_by = "Treatment",
  reference_level = "Control",
  target_levels = c("Low", "High")
)

cielab_kinetics(data, time_col, group_by, comparison, time_levels)

Tracks how wine colour changes across an ordered series of timepoints. For each combination of grouping variables (e.g. wine and treatment), it computes the centroid (mean L*, a*, b*) at each timepoint and reports colour differences relative to the first timepoint ("baseline"), between consecutive timepoints ("consecutive"), or "both" (default). A time-ordered companion to calc_colour_diff().

Parameters:

Output: A data.frame with comparison_type, from_time, to_time, contrast, n_from, n_to, and the colour differences dL, dC, dh, dE76, dE00. Differences are (later − earlier), so positive dL means the wine lightened over time.

Example:

# Colour change of each wine x treatment over storage time
kin <- cielab_kinetics(
  data = wine_colours,
  time_col = "Months",
  group_by = c("Wine", "Treatment")
)

# Only cumulative change from the first timepoint
kin <- cielab_kinetics(wine_colours, "Months", comparison = "baseline")

cielab_swatch(CIELab, file, ...)

Create and save colour swatch visualisations to various formats (PNG, PDF, SVG, etc.).

Input: A data.frame with columns:

Optional parameters:

Example:

df <- data.frame(
  WineName = c("Red", "White", "Rose"),
  CIELab_L = c(30, 90, 70),
  CIELab_a = c(50, -5, 20),
  CIELab_b = c(30, 10, 15)
)
cielab_swatch(df, "swatches.png")
cielab_swatch(df, "swatches.pdf")
cielab_swatch(df, "swatches.svg")

process_spectrum(folder_path, ...)

Process a folder of spectrophotometer CSV files into a combined data frame.

Parameters:

Output: A data.frame with columns:

Example:

# Process all CSV files in a folder
spectra <- process_spectrum("path/to/spectrophotometer/data")

# Then calculate CIELab - just pass the result directly!
result <- cielab_from_spectrum(spectra)

Agilent MassHunter UV-DAD Peak Table Functions

read_agilent_dad_peaks(file_path, sep, col_peak, col_rt, col_height, col_area)

Parses a multi-sample UV-DAD peak table exported from Agilent MassHunter Quantitative Analysis. Returns a long-format data frame with one row per detected peak per sample, ready to pass to align_peaks_by_rt().

Parameters:

Output: A long-format data frame:

Notes:


align_peaks_by_rt(peaks_long, max_drift, rt_tol, drift_model)

Aligns peaks across samples by retention time in three steps:

  1. Drift correction – each sample’s retention-time drift relative to the reference sample (the first sample) is estimated and removed.
  2. Grouping – the drift-corrected retention times of all samples are grouped into peak groups, with at most one peak per sample in a group. Peaks absent from the reference form groups in exactly the same way as those present in it.
  3. Flagging – peaks whose assignment is a close call are flagged for manual review.

Parameters:

Output: A named list with four elements:

$aligned — Long-format table with one row for every combination of peak group and sample:

$summary — One row per peak group:

$drift_stats — One row per sample:

$params — The settings used: reference, max_drift, rt_tol, drift_model.

How it works:

Flags:

Limits: retention time alone cannot separate two compounds whose spacing is similar to the random peak-to-peak scatter remaining after drift correction. Such regions are flagged rather than resolved and should be checked by hand (e.g. against spectra). Drift is estimated relative to the first sample, so a reference with few peaks gives few anchors.

Example:

library(PenguinoidRTools)
library(tidyr)

peaks_long <- read_agilent_dad_peaks("polyphenols_peaktable.csv")
result     <- align_peaks_by_rt(peaks_long, max_drift = 0.25)

result$summary      # mean RT, SD, CV% per peak group
result$drift_stats  # per-sample drift vs reference — use to tune max_drift
result$aligned      # full table with flags for review
result$params       # settings used, including the estimated rt_tol

# Peaks to review by hand
subset(result$aligned, flag == "AMBIGUOUS")

# Pivot to wide format
area_wide <- pivot_wider(
  result$aligned,
  id_cols     = c(peak_group, ref_rt),
  names_from  = sample,
  values_from = area
)

Dependencies

For ANOVA functions

For CIELab colour difference functions (calc_colour_diff, calc_pairwise_dE)

For CIELab visualisation (cielab_swatch)

For data processing (process_spectrum)

For Agilent MassHunter peak table functions (read_agilent_dad_peaks, align_peaks_by_rt)


OIV Method Reference

The CIELab functions implement the colour calculation method specified in:

OIV-MA-AS2-11 (R2006): Determination of chromatic characteristics according to CIELab

The method uses:


License

GPL-3.0