| Title: | Spectroscopy Analysis Using the Tidy Data Philosophy |
|---|---|
| Description: | Enables the analysis of spectroscopy data such as infrared ('IR'), Raman, and nuclear magnetic resonance ('NMR') using the tidy data framework from the 'tidyverse'. The 'tidyspec' package provides functions for data transformation, normalization, baseline correction, smoothing, derivatives, and both interactive and static visualization. It promotes structured, reproducible workflows for spectral data exploration and preprocessing. Implemented methods include Savitzky and Golay (1964) "Smoothing and Differentiation of Data by Simplified Least Squares Procedures" <doi:10.1021/ac60214a047>, Sternberg (1983) "Biomedical Image Processing" <https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=1654163>, Zimmermann and Kohler (1996) "Baseline correction using the rolling ball algorithm" <doi:10.1016/0168-583X(95)00908-6>, Beattie and Esmonde-White (2021) "Exploration of Principal Component Analysis: Deriving Principal Component Analysis Visually Using Spectra" <doi:10.1177/0003702820987847>, Wickham et al. (2019) "Welcome to the tidyverse" <doi:10.21105/joss.01686>, and Kuhn, Wickham and Hvitfeldt (2024) "recipes: Preprocessing and Feature Engineering Steps for Modeling" <https://CRAN.R-project.org/package=recipes>. |
| Authors: | Marcel Ferreira [aut, cre] (ORCID: <https://orcid.org/0000-0002-3445-0945>), Willian Zambuzzi [dtc] (ORCID: <https://orcid.org/0000-0002-4149-5965>), Julia Moraes [dtc, ctb] (ORCID: <https://orcid.org/0000-0003-4241-5389>), Emerson Araujo Alves dos Santos [ctb] (ORCID: <https://orcid.org/0000-0002-8200-0906>) |
| Maintainer: | Marcel Ferreira <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.4.0 |
| Built: | 2026-05-13 09:36:40 UTC |
| Source: | https://github.com/marceelrf/tidyspec |
This function checks which column has been set as the default wavenumber column. If no column has been set, it prints a message prompting the user to define one.
check_wn_col(show_val = FALSE)check_wn_col(show_val = FALSE)
show_val |
Logical. If 'TRUE', the function returns the name of the current wavenumber column. Default is 'FALSE'. |
If 'show_val = TRUE', returns the name of the wavenumber column (character). Otherwise, it only prints a message.
set_spec_wn("Wavenumber") # Set the wavenumber column check_wn_col() # Check which column is set check_wn_col(show_val = TRUE) # Check and return the column nameset_spec_wn("Wavenumber") # Set the wavenumber column check_wn_col() # Check which column is set check_wn_col(show_val = TRUE) # Check and return the column name
A dataset containing spectral absorbance measurements for different concentrations of CoHA.
CoHAspecCoHAspec
A data frame with 6 rows and 5 columns:
Numeric. The spectral wavenumber (cm-1).
Numeric. Absorbance values for CoHA at 1 mM Cobalt concentration.
Numeric. Absorbance values for CoHA at 2.5 mM Cobalt concentration.
Numeric. Absorbance values for CoHA at 5 mM Cobalt concentration.
Numeric. Absorbance values for CoHA at 10 mM Cobalt concentration.
de Almeida GS, Ferreira MR, da Costa Fernandes CJ, et al. Development of cobalt (Co)-doped monetites for bone regeneration. J Biomed Mater Res. 2024; 112(1):e35319. <doi:10.1002/jbm.b.35319>
data(CoHAspec) head(CoHAspec)data(CoHAspec) head(CoHAspec)
Function to demonstrate the use of implemented functions
demo_rolling_ball(verbose = TRUE)demo_rolling_ball(verbose = TRUE)
verbose |
Logical indicating whether to print progress messages |
A list containing two elements:
Results from the simple rolling ball method
Results from the mathematical morphology method
# Run the demonstration demo_results <- demo_rolling_ball() # Access results simple_method <- demo_results$simple morphology_method <- demo_results$morphology# Run the demonstration demo_results <- demo_rolling_ball() # Access results simple_method <- demo_results$simple morphology_method <- demo_results$morphology
Convenience function to visualize the results
plot_rolling_ball( result, title = "Rolling Ball Baseline Correction", x_values = NULL )plot_rolling_ball( result, title = "Rolling Ball Baseline Correction", x_values = NULL )
result |
Result from rolling_ball or rolling_ball_morphology function |
title |
Plot title |
x_values |
X-axis values (optional) |
No return value, called for side effects (creates a plot)
Implements the rolling ball baseline correction method
rolling_ball(x, wm, ws = 0)rolling_ball(x, wm, ws = 0)
x |
Numeric vector containing the spectrum/signal values |
wm |
Window width (ball radius). Larger values = smoother baseline |
ws |
Smoothing window width (optional) |
A list with three components:
Numeric vector containing the estimated baseline values
Numeric vector containing the baseline-corrected signal (original - baseline)
Numeric vector containing the original input signal
# Example with simulated data x <- seq(1, 100, by = 1) y <- sin(x/10) + 0.1*x + rnorm(100, 0, 0.1) result <- rolling_ball(y, wm = 10) plot(x, y, type = "l", col = "blue", main = "Rolling Ball Correction") lines(x, result$baseline, col = "red", lwd = 2) lines(x, result$corrected, col = "green") legend("topright", c("Original", "Baseline", "Corrected"), col = c("blue", "red", "green"), lty = 1)# Example with simulated data x <- seq(1, 100, by = 1) y <- sin(x/10) + 0.1*x + rnorm(100, 0, 0.1) result <- rolling_ball(y, wm = 10) plot(x, y, type = "l", col = "blue", main = "Rolling Ball Correction") lines(x, result$baseline, col = "red", lwd = 2) lines(x, result$corrected, col = "green") legend("topright", c("Original", "Baseline", "Corrected"), col = c("blue", "red", "green"), lty = 1)
More sophisticated version of rolling ball using mathematical morphology concepts
rolling_ball_morphology(x, radius, smooth = TRUE)rolling_ball_morphology(x, radius, smooth = TRUE)
x |
Numeric vector containing the spectrum/signal values |
radius |
Radius of the structuring ball |
smooth |
Apply additional smoothing (logical) |
A list with three components:
Numeric vector containing the estimated baseline values using morphological operations
Numeric vector containing the baseline-corrected signal (original - baseline)
Numeric vector containing the original input signal
Defines a default wavenumber column name to be used by other functions in the package when 'wn_col' is not explicitly provided.
set_spec_wn(col_name)set_spec_wn(col_name)
col_name |
A string specifying the name of the wavenumber column. |
Invisibly returns the assigned column name.
[spec_select()]
set_spec_wn("Wavenumber")set_spec_wn("Wavenumber")
Auxiliary function to smooth the baseline using moving average
smooth_baseline(baseline, ws)smooth_baseline(baseline, ws)
baseline |
Vector with the baseline |
ws |
Smoothing window width |
Smoothed numeric vector of the same length as input
This function converts absorbance data to transmittance using the formula , where is the absorbance and is the transmittance.
spec_abs2trans(.data, wn_col = NULL)spec_abs2trans(.data, wn_col = NULL)
.data |
A 'data.frame' or 'tibble' containing spectral data in absorbance. |
wn_col |
A character string specifying the column name for the wavelength data. Default is '"Wn"'. |
A 'tibble' with the converted transmittance data, containing the wavelength column and the numeric transmittance columns. Any rows with infinite values are removed.
This function extracts the rolling ball baseline from spectral data within a specified wavelength range. It returns only the baseline, not the corrected data.
spec_bl_rollingBall( .data, wn_col = NULL, wn_min = NULL, wn_max = NULL, wm, ws = 0, is_abs = TRUE )spec_bl_rollingBall( .data, wn_col = NULL, wn_min = NULL, wn_max = NULL, wm, ws = 0, is_abs = TRUE )
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. Default is '"Wn"'. |
wn_min |
A numeric value specifying the minimum wavelength to consider for the baseline correction. |
wn_max |
A numeric value specifying the maximum wavelength to consider for the baseline correction. |
wm |
A numeric value for the window size of the rolling ball algorithm. |
ws |
A numeric value for the smoothing factor of the rolling ball algorithm. |
is_abs |
A logical value indicating whether the data is already in absorbance. If 'TRUE', absorbance is used directly; if 'FALSE', the data is converted to absorbance before extracting the baseline. |
A 'tibble' with the baseline data, containing the wavelength column and the baseline for each numeric column.
Baseline estimation performed using a custom rolling ball implementation.
This function extracts the rubberband baseline from spectral data within a specified wavelength range. The rubberband method fits a baseline by connecting local minima points in the spectrum. It returns only the baseline, not the corrected data.
spec_bl_rubberband( .data, wn_col = NULL, wn_min = NULL, wn_max = NULL, segment_length = 50, smooth_baseline = TRUE, is_abs = TRUE )spec_bl_rubberband( .data, wn_col = NULL, wn_min = NULL, wn_max = NULL, segment_length = 50, smooth_baseline = TRUE, is_abs = TRUE )
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. Default is '"Wn"'. |
wn_min |
A numeric value specifying the minimum wavelength to consider for the baseline correction. |
wn_max |
A numeric value specifying the maximum wavelength to consider for the baseline correction. |
segment_length |
A numeric value specifying the length of segments for finding local minima. Default is 50. |
smooth_baseline |
A logical value indicating whether to smooth the baseline using spline interpolation. Default is TRUE. |
is_abs |
A logical value indicating whether the data is already in absorbance. If 'TRUE', absorbance is used directly; if 'FALSE', the data is converted to absorbance before extracting the baseline. |
A 'tibble' with the baseline data, containing the wavelength column and the baseline for each numeric column.
Rubberband baseline estimation connects local minima to estimate baseline.
This function applies a rolling ball baseline correction to spectral data within a specified wavelength range. It allows for correction of either absorbance or transmittance data.
spec_blc_rollingBall( .data, wn_col = NULL, wn_min = NULL, wn_max = NULL, wm, ws = 0, is_abs = TRUE )spec_blc_rollingBall( .data, wn_col = NULL, wn_min = NULL, wn_max = NULL, wm, ws = 0, is_abs = TRUE )
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. Default is '"Wn"'. |
wn_min |
A numeric value specifying the minimum wavelength to consider for the baseline correction. |
wn_max |
A numeric value specifying the maximum wavelength to consider for the baseline correction. |
wm |
A numeric value for the window size of the rolling ball algorithm. |
ws |
A numeric value for the smoothing factor of the rolling ball algorithm. |
is_abs |
A logical value indicating whether the data is already in absorbance. If 'TRUE', absorbance is used directly; if 'FALSE', the data is converted to absorbance before applying the baseline correction. |
A 'tibble' with the baseline-corrected spectral data, containing the wavelength column and the corrected numeric columns.
Baseline estimation performed using a custom rolling ball implementation.
This function applies a rubberband baseline correction to spectral data within a specified wavelength range. The rubberband method fits a baseline by connecting local minima points in the spectrum. It allows for correction of either absorbance or transmittance data.
spec_blc_rubberband( .data, wn_col = NULL, wn_min = NULL, wn_max = NULL, segment_length = 50, smooth_baseline = TRUE, is_abs = TRUE )spec_blc_rubberband( .data, wn_col = NULL, wn_min = NULL, wn_max = NULL, segment_length = 50, smooth_baseline = TRUE, is_abs = TRUE )
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. Default is '"Wn"'. |
wn_min |
A numeric value specifying the minimum wavelength to consider for the baseline correction. |
wn_max |
A numeric value specifying the maximum wavelength to consider for the baseline correction. |
segment_length |
A numeric value specifying the length of segments for finding local minima. Default is 50. |
smooth_baseline |
A logical value indicating whether to smooth the baseline using spline interpolation. Default is TRUE. |
is_abs |
A logical value indicating whether the data is already in absorbance. If 'TRUE', absorbance is used directly; if 'FALSE', the data is converted to absorbance before applying the baseline correction. |
A 'tibble' with the baseline-corrected spectral data, containing the wavelength column and the corrected numeric columns.
Rubberband baseline correction connects local minima to estimate baseline.
This function applies numerical differentiation to spectral data, allowing for the calculation of the first or higher-order differences.
spec_diff(.data, wn_col = NULL, degree = 1)spec_diff(.data, wn_col = NULL, degree = 1)
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. Default is '"Wn"'. |
degree |
A numeric value specifying the degree of differentiation. If 'degree' is 0, the original data is returned without any changes. |
A 'tibble' with the differentiated spectral data, containing the wavelength column and the differentiated numeric columns. If 'degree' is 0, the original data is returned.
This function filters the spectral dataset based on a specified wavenumber ('wn') range. It requires the wavenumber column to be previously set using [set_spec_wn()]. If 'wn_min' and/or 'wn_max' are provided, the data will be filtered accordingly. If neither is provided, the original dataset is returned unchanged.
spec_filter(.data, wn_min = NULL, wn_max = NULL)spec_filter(.data, wn_min = NULL, wn_max = NULL)
.data |
A data frame containing spectral data. |
wn_min |
Optional numeric value. Minimum wavenumber value to keep. |
wn_max |
Optional numeric value. Maximum wavenumber value to keep. |
A filtered data frame based on the wavenumber column.
set_spec_wn("Wavenumber") spec_filter(CoHAspec, wn_min = 500, wn_max = 1800)set_spec_wn("Wavenumber") spec_filter(CoHAspec, wn_min = 500, wn_max = 1800)
This function interpolates spectral data to match the wavenumber grid of a reference dataset. Works like a left join - keeps all wavenumbers from the reference data and interpolates the spectral data to match those wavenumbers.
spec_interpolate_left( .data, reference, wn_col = NULL, method = "pchip", extrapolate = FALSE, ... )spec_interpolate_left( .data, reference, wn_col = NULL, method = "pchip", extrapolate = FALSE, ... )
.data |
A 'data.frame' or 'tibble' containing spectral data to be interpolated. |
reference |
A 'data.frame' or 'tibble' containing the reference wavenumber grid. |
wn_col |
A character string specifying the column name for the wavenumber data. If NULL, uses the default set with set_spec_wn(). |
method |
A character string specifying the interpolation method. Options: "linear", "pchip", "spline", "akima". Default is "pchip". |
extrapolate |
Logical. Should values be extrapolated outside the original range? Default is FALSE (returns NA for out-of-range values). |
... |
Additional arguments passed to the interpolation method. |
A 'tibble' with interpolated spectral data using the reference wavenumber grid.
Creates a regular wavenumber grid and interpolates spectral data to match it.
spec_interpolate_regular( .data, resolution = 1, wn_min = NULL, wn_max = NULL, wn_col = NULL, method = "pchip", ... )spec_interpolate_regular( .data, resolution = 1, wn_min = NULL, wn_max = NULL, wn_col = NULL, method = "pchip", ... )
.data |
A 'data.frame' or 'tibble' containing spectral data. |
resolution |
Numeric. The step size for the regular grid. |
wn_min, wn_max
|
Numeric. Range for the regular grid. If NULL, uses data range. |
wn_col |
A character string specifying the column name for the wavenumber data. |
method |
A character string specifying the interpolation method. |
... |
Additional arguments passed to the interpolation method. |
A 'tibble' with spectral data interpolated to a regular grid.
This function interpolates reference data to match the wavenumber grid of the main dataset. Works like a right join - keeps all wavenumbers from .data and interpolates reference data to match those wavenumbers.
spec_interpolate_right( .data, reference, wn_col = NULL, method = "pchip", extrapolate = FALSE, suffix = c(".x", ".y"), ... )spec_interpolate_right( .data, reference, wn_col = NULL, method = "pchip", extrapolate = FALSE, suffix = c(".x", ".y"), ... )
.data |
A 'data.frame' or 'tibble' containing the main spectral data. |
reference |
A 'data.frame' or 'tibble' containing spectral data to be interpolated. |
wn_col |
A character string specifying the column name for the wavenumber data. |
method |
A character string specifying the interpolation method. |
extrapolate |
Logical. Should values be extrapolated outside the original range? |
suffix |
Character vector of length 2 specifying suffixes for overlapping column names. |
... |
Additional arguments passed to the interpolation method. |
A 'tibble' combining .data with interpolated reference data.
'spec_join()' combines two or more spectral tibbles that share the same wavenumber column. The function performs a full join operation, keeping all wavenumber values from all input tibbles.
spec_join(..., wn_col = NULL, join_type = "full", suffix = c(".x", ".y"))spec_join(..., wn_col = NULL, join_type = "full", suffix = c(".x", ".y"))
... |
Two or more tibbles containing spectral data |
wn_col |
Character string specifying the wavenumber column name. If NULL, uses the globally set wavenumber column from 'set_spec_wn()'. |
join_type |
Character string specifying the type of join operation. Options: "full" (default), "inner", "left". See Details. |
suffix |
Character vector of length 2 specifying suffixes to add to non-wavenumber columns when there are naming conflicts. Default: c(".x", ".y") |
Join types: - "full": Keep all wavenumber values from all tibbles (default) - "inner": Keep only wavenumber values present in all tibbles - "left": Keep wavenumber values from the first tibble
The function will show warnings about the wavenumber column being used (similar to other tidyspec functions) and will handle missing values appropriately.
A tibble containing the joined spectral data
## Not run: # Set wavenumber column set_spec_wn("Wavenumber") # Join two spectral datasets joined_data <- spec_join(CoHAspec, other_spec_data) # Join multiple datasets with inner join joined_data <- spec_join(spec1, spec2, spec3, join_type = "inner") # Specify wavenumber column explicitly joined_data <- spec_join(spec1, spec2, wn_col = "Wavenumber") ## End(Not run)## Not run: # Set wavenumber column set_spec_wn("Wavenumber") # Join two spectral datasets joined_data <- spec_join(CoHAspec, other_spec_data) # Join multiple datasets with inner join joined_data <- spec_join(spec1, spec2, spec3, join_type = "inner") # Specify wavenumber column explicitly joined_data <- spec_join(spec1, spec2, wn_col = "Wavenumber") ## End(Not run)
This function normalizes the numeric spectral data in each column to the [0, 1] range, preserving the wavelength column.
spec_norm_01(.data, wn_col = NULL)spec_norm_01(.data, wn_col = NULL)
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. Default is '"Wn"'. |
A 'tibble' with the normalized spectral data, containing the wavelength column and the normalized numeric columns.
This function normalizes the numeric spectral data in each column so that the area under the curve equals a specified value (default is 1), while preserving the wavelength column.
spec_norm_area(.data, wn_col = NULL, target_area = 1)spec_norm_area(.data, wn_col = NULL, target_area = 1)
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. If NULL, uses the default set by 'set_spec_wn()'. |
target_area |
A numeric value specifying the target area under the curve. Default is 1. |
A 'tibble' with the area-normalized spectral data, containing the wavelength column and the normalized numeric columns.
This function normalizes the numeric spectral data in each column so that the intensity at a specified wavenumber equals a target value (default is 1), while preserving the wavelength column.
spec_norm_by_wn( .data, wn_col = NULL, target_wn, target_value = 1, method = "linear" )spec_norm_by_wn( .data, wn_col = NULL, target_wn, target_value = 1, method = "linear" )
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. If NULL, uses the default set by 'set_spec_wn()'. |
target_wn |
A numeric value specifying the wavenumber to use as reference for normalization. |
target_value |
A numeric value specifying the target intensity value at the reference wavenumber. Default is 1. |
method |
A character string specifying the interpolation method when the exact wavenumber is not found. Options are "linear" (default) or "nearest". |
A 'tibble' with the wavenumber-normalized spectral data, containing the wavelength column and the normalized numeric columns.
This function normalizes the numeric spectral data in each column to a specified range [min, max], preserving the wavelength column.
spec_norm_minmax(.data, wn_col = NULL, min = 0, max = 1)spec_norm_minmax(.data, wn_col = NULL, min = 0, max = 1)
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. Default is '"Wn"'. |
min |
A numeric value specifying the minimum value of the desired range. Default is 0. |
max |
A numeric value specifying the maximum value of the desired range. Default is 1. |
A 'tibble' with the normalized spectral data, containing the wavelength column and the normalized numeric columns.
This function standardizes the numeric spectral data in each column to have a mean of 0 and a standard deviation of 1 (unit variance), while preserving the wavelength column.
spec_norm_var(.data, wn_col = NULL)spec_norm_var(.data, wn_col = NULL)
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. Default is '"Wn"'. |
A 'tibble' with the standardized spectral data, containing the wavelength column and the standardized numeric columns.
This function computes a Principal Component Analysis (PCA) on spectral data, excluding the wavenumber column from the analysis.
spec_pca(.data, wn_col = NULL, scale = TRUE, center = TRUE)spec_pca(.data, wn_col = NULL, scale = TRUE, center = TRUE)
.data |
A data frame containing spectral data, with one column representing wavenumbers and the remaining columns containing spectral intensity values. |
wn_col |
A string specifying the name of the column that contains the wavenumber values. If NULL, the function attempts to retrieve the default wavenumber column set by 'set_spec_wn()'. |
scale |
A logical value indicating whether the spectral data should be scaled (default is TRUE). |
center |
A logical value indicating whether the spectral data should be centered (default is TRUE). |
A 'prcomp' object containing the PCA results, including principal components, standard deviations, and loadings.
set_spec_wn("Wavenumber") pca_result <- spec_pca(CoHAspec) summary(pca_result)set_spec_wn("Wavenumber") pca_result <- spec_pca(CoHAspec) summary(pca_result)
Creates a customizable scree plot based on a 'prcomp' object showing variance explained by each component.
spec_pca_screeplot( pca, n = 10, show_labels = TRUE, show_cumulative = TRUE, bar_color = "steelblue", line_color = "darkred", show_kaiser = FALSE, title = "Scree Plot", subtitle = NULL, accuracy = 1 )spec_pca_screeplot( pca, n = 10, show_labels = TRUE, show_cumulative = TRUE, bar_color = "steelblue", line_color = "darkred", show_kaiser = FALSE, title = "Scree Plot", subtitle = NULL, accuracy = 1 )
pca |
A PCA object returned by [prcomp()]. |
n |
Number of components to display. Defaults to 10. |
show_labels |
Logical. Show percentage labels on bars? Default is TRUE. |
show_cumulative |
Logical. Show cumulative variance line? Default is TRUE. |
bar_color |
Fill color for bars. Default is "steelblue". |
line_color |
Color of the cumulative line and points. Default is "darkred". |
show_kaiser |
Logical. Show Kaiser criterion line? Default is FALSE. |
title |
Plot title. Default is "Scree Plot". |
subtitle |
Optional plot subtitle. |
accuracy |
Number of decimal places for variance percentages. Default is 1. |
A ggplot2 scree plot object.
pca <- prcomp(USArrests, scale. = TRUE) spec_pca_screeplot(pca, n = 4) spec_pca_screeplot(pca, show_kaiser = TRUE, bar_color = "darkblue")pca <- prcomp(USArrests, scale. = TRUE) spec_pca_screeplot(pca, n = 4) spec_pca_screeplot(pca, show_kaiser = TRUE, bar_color = "darkblue")
This function calculates the contribution of each wavenumber to the principal components (PCs) in a PCA result. Contributions are computed as the squared loadings multiplied by 100.
This function calculates the contribution of each wavenumber to the principal components (PCs) in a PCA result. Contributions are computed as the squared loadings multiplied by 100.
spec_pca_wn_contrib(PCA) spec_pca_wn_contrib(PCA)spec_pca_wn_contrib(PCA) spec_pca_wn_contrib(PCA)
PCA |
An object of class 'prcomp', containing the results of a principal component analysis. |
The function extracts the PCA loadings (rotation matrix) and computes the squared values of each loading, scaled to percentage values. This helps interpret the importance of each wavenumber in defining the principal components.
The function extracts the PCA loadings (rotation matrix) and computes the squared values of each loading, scaled to percentage values. This helps interpret the importance of each wavenumber in defining the principal components.
A tibble containing the wavenumber column and the percentage contribution of each wavenumber to each principal component.
A tibble containing the wavenumber column and the percentage contribution of each wavenumber to each principal component.
pca_result <- spec_pca(CoHAspec) wn_contrib <- spec_pca_wn_contrib(pca_result) print(wn_contrib) pca_result <- spec_pca(CoHAspec) wn_contrib <- spec_pca_wn_contrib(pca_result) print(wn_contrib)pca_result <- spec_pca(CoHAspec) wn_contrib <- spec_pca_wn_contrib(pca_result) print(wn_contrib) pca_result <- spec_pca(CoHAspec) wn_contrib <- spec_pca_wn_contrib(pca_result) print(wn_contrib)
This function reads spectral data from a file, automatically detecting the format and using either 'readr' or 'readxl' functions. It also sets the specified wavenumber column as the default using 'set_spec_wn()'.
spec_read(file, wn_col, ...)spec_read(file, wn_col, ...)
file |
Path to the file to be read. |
wn_col |
Character. Name of the column containing wavenumber values. This column will be set as the default wavenumber column. |
... |
Additional arguments passed to 'readr::read_delim()', 'readr::read_csv()', or 'readxl::read_excel()', depending on the file format. |
The function automatically determines the file format based on the extension: - '.txt', '.csv', '.tsv', and '.csv2' are read using 'readr' functions. - '.xls' and '.xlsx' are read using 'readxl::read_excel()'.
If the specified 'wn_col' does not exist in the data, an error is returned.
A tibble containing the spectral data.
file_path <- system.file("extdata", "CoHAspec.csv", package = "tidyspec") df <- spec_read(file_path, wn_col = "Wavenumber") check_wn_col() # Verifica se a coluna de wavenumber está definidafile_path <- system.file("extdata", "CoHAspec.csv", package = "tidyspec") df <- spec_read(file_path, wn_col = "Wavenumber") check_wn_col() # Verifica se a coluna de wavenumber está definida
This function selects user-specified columns from a spectral dataset, always ensuring that the wavenumber column ('wn_col') is included, unless explicitly excluded.
spec_select(.data, ...)spec_select(.data, ...)
.data |
A data frame containing spectral data. |
... |
Column selection helpers (e.g., column names, -column_to_exclude). |
A data frame containing the selected columns.
[dplyr::select()], [set_spec_wn()]
This function generates a customizable plot for spectral data, allowing for the selection of plot type (absorbance or transmittance), x-axis direction, plot geometry (points or lines), and color palette.
spec_smartplot( .data, wn_col = NULL, xdir = c("reverse", "standard"), geom = c("point", "line"), xmin = NULL, xmax = NULL, alpha = 0.8, type = c("absorbance", "transmittance"), palette = c("viridis", "plasma", "magma", "cividis", "turbo", "Set1", "Set2", "Dark2", "Paired", "custom"), custom_colors = NULL )spec_smartplot( .data, wn_col = NULL, xdir = c("reverse", "standard"), geom = c("point", "line"), xmin = NULL, xmax = NULL, alpha = 0.8, type = c("absorbance", "transmittance"), palette = c("viridis", "plasma", "magma", "cividis", "turbo", "Set1", "Set2", "Dark2", "Paired", "custom"), custom_colors = NULL )
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength or wavenumber data. This parameter is required. |
xdir |
A character string specifying the direction of the x-axis. Choices are '"reverse"' (typically used for wavenumber) or '"standard"'. |
geom |
A character string specifying the geometry of the plot. Choices are '"point"' for a scatter plot or '"line"' for a line plot. |
xmin |
A numeric value specifying the minimum x-axis value. If not provided, the minimum value from 'wn_col' will be used. |
xmax |
A numeric value specifying the maximum x-axis value. If not provided, the maximum value from 'wn_col' will be used. |
alpha |
A numeric value (0–1) specifying the transparency of plotted points or lines. Default is '0.8'. |
type |
A character string specifying the y-axis label. Either '"absorbance"' (default) or '"transmittance"'. |
palette |
A character string specifying the color palette to use. Built-in options:
|
custom_colors |
A character vector of valid color strings (hex codes or R color names) used when 'palette = "custom"'. The number of colors should match or exceed the number of spectra in the data. If fewer colors are provided than spectra, colors will be recycled with a warning. |
A 'ggplot' object representing the spectral plot.
## Not run: # Default viridis palette spec_smartplot(spec_data, wn_col = "wavenumber") # Built-in qualitative palette spec_smartplot(spec_data, wn_col = "wavenumber", palette = "Set1") # Custom colors spec_smartplot(spec_data, wn_col = "wavenumber", palette = "custom", custom_colors = c("#E63946", "#457B9D", "#2A9D8F")) ## End(Not run)## Not run: # Default viridis palette spec_smartplot(spec_data, wn_col = "wavenumber") # Built-in qualitative palette spec_smartplot(spec_data, wn_col = "wavenumber", palette = "Set1") # Custom colors spec_smartplot(spec_data, wn_col = "wavenumber", palette = "custom", custom_colors = c("#E63946", "#457B9D", "#2A9D8F")) ## End(Not run)
This function generates a customizable interactive plot for spectral data using plotly, allowing for the selection of plot type (absorbance or transmittance), x-axis direction, plot geometry (points or lines), and color palette.
spec_smartplotly( .data, wn_col = NULL, xdir = c("reverse", "standard"), geom = c("point", "line"), xmin = NULL, xmax = NULL, alpha = 0.8, type = c("absorbance", "transmittance"), palette = c("viridis", "plasma", "magma", "cividis", "turbo", "Set1", "Set2", "Dark2", "Paired", "custom"), custom_colors = NULL )spec_smartplotly( .data, wn_col = NULL, xdir = c("reverse", "standard"), geom = c("point", "line"), xmin = NULL, xmax = NULL, alpha = 0.8, type = c("absorbance", "transmittance"), palette = c("viridis", "plasma", "magma", "cividis", "turbo", "Set1", "Set2", "Dark2", "Paired", "custom"), custom_colors = NULL )
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength or wavenumber data. This parameter is required. |
xdir |
A character string specifying the direction of the x-axis. Choices are '"reverse"' (typically used for wavenumber) or '"standard"'. |
geom |
A character string specifying the geometry of the plot. Choices are '"point"' for a scatter plot or '"line"' for a line plot. |
xmin |
A numeric value specifying the minimum x-axis value. If not provided, the minimum value from 'wn_col' will be used. |
xmax |
A numeric value specifying the maximum x-axis value. If not provided, the maximum value from 'wn_col' will be used. |
alpha |
A numeric value (0–1) specifying the transparency of plotted points or lines. Default is '0.8'. |
type |
A character string specifying the y-axis label. Either '"absorbance"' (default) or '"transmittance"'. |
palette |
A character string specifying the color palette to use. Built-in options:
|
custom_colors |
A character vector of valid color strings (hex codes or R color names) used when 'palette = "custom"'. The number of colors should match or exceed the number of spectra in the data. If fewer colors are provided than spectra, colors will be recycled with a warning. |
A 'plotly' object representing the interactive spectral plot.
## Not run: # Default viridis palette, line geometry spec_smartplotly(spec_data, wn_col = "wavenumber", geom = "line") # Built-in qualitative palette spec_smartplotly(spec_data, wn_col = "wavenumber", palette = "Set1") # Custom colors spec_smartplotly(spec_data, wn_col = "wavenumber", palette = "custom", custom_colors = c("#E63946", "#457B9D", "#2A9D8F")) ## End(Not run)## Not run: # Default viridis palette, line geometry spec_smartplotly(spec_data, wn_col = "wavenumber", geom = "line") # Built-in qualitative palette spec_smartplotly(spec_data, wn_col = "wavenumber", palette = "Set1") # Custom colors spec_smartplotly(spec_data, wn_col = "wavenumber", palette = "custom", custom_colors = c("#E63946", "#457B9D", "#2A9D8F")) ## End(Not run)
This function applies a moving average smoothing to numeric spectral data using a specified window size and polynomial degree, while preserving the wavelength column.
spec_smooth_avg(.data, wn_col = NULL, window = 15, degree = 2)spec_smooth_avg(.data, wn_col = NULL, window = 15, degree = 2)
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. Default is '"Wn"'. |
window |
A numeric value specifying the window size for the moving average smoothing. Default is 15. |
degree |
A numeric value specifying the degree of the polynomial for smoothing. Default is 2. |
A 'tibble' with the smoothed spectral data, containing the wavelength column and the smoothed numeric columns.
This function applies Savitzky-Golay smoothing to numeric spectral data using a specified window size, polynomial order, and differentiation degree, while preserving the wavelength column.
spec_smooth_sga(.data, wn_col = NULL, window = 15, forder = 4, degree = 0)spec_smooth_sga(.data, wn_col = NULL, window = 15, forder = 4, degree = 0)
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. Default is '"Wn"'. |
window |
A numeric value specifying the window size for the Savitzky-Golay smoothing. Default is 15. |
forder |
A numeric value specifying the polynomial order for smoothing. Default is 4. |
degree |
A numeric value specifying the degree of differentiation. Default is 0 (no differentiation). |
A 'tibble' with the smoothed spectral data, containing the wavelength column and the smoothed numeric columns.
This function converts transmittance data to absorbance using the formula 'A = 2 - log10(T)', where 'T' is the transmittance. It also filters out any infinite values resulting from the transformation, while preserving the wavelength column.
spec_trans2abs(.data, wn_col = NULL)spec_trans2abs(.data, wn_col = NULL)
.data |
A 'data.frame' or 'tibble' containing spectral data. |
wn_col |
A character string specifying the column name for the wavelength data. Default is '"Wn"'. |
A 'tibble' with the converted absorbance data, containing the wavelength column and the absorbance numeric columns.
'spec_values_at()' extracts spectral values at specified wavenumber positions. The function finds the closest wavenumber matches and returns the corresponding spectral intensities for each sample column.
spec_values_at( spec_data, wn_values, wn_col = NULL, method = "closest", tolerance = Inf, format = "wide" )spec_values_at( spec_data, wn_values, wn_col = NULL, method = "closest", tolerance = Inf, format = "wide" )
spec_data |
A tibble containing spectral data |
wn_values |
Numeric vector of wavenumber values to extract |
wn_col |
Character string specifying the wavenumber column name. If NULL, uses the globally set wavenumber column from 'set_spec_wn()'. |
method |
Character string specifying the extraction method. Options: "closest" (default), "exact", "interpolate". See Details. |
tolerance |
Numeric value specifying the maximum allowed difference for "closest" method. Default: Inf (no limit). |
format |
Character string specifying output format. Options: "wide" (default), "long". See Details. |
Extraction methods: - "closest": Find the closest wavenumber match (default) - "exact": Only return exact matches (NA for non-matches) - "interpolate": Use linear interpolation between adjacent points
Output formats: - "wide": Each wavenumber becomes a row, samples as columns - "long": Tidy format with wavenumber, sample, and value columns
A tibble with extracted values in the specified format
## Not run: # Set wavenumber column set_spec_wn("Wavenumber") # Extract values at specific wavenumbers peaks <- c(1650, 1450, 1250, 1050) extracted <- spec_values_at(CoHAspec, peaks) # Use exact matching only extracted <- spec_values_at(CoHAspec, peaks, method = "exact") # Get results in long format extracted <- spec_values_at(CoHAspec, peaks, format = "long") # Use interpolation extracted <- spec_values_at(CoHAspec, peaks, method = "interpolate") ## End(Not run)## Not run: # Set wavenumber column set_spec_wn("Wavenumber") # Extract values at specific wavenumbers peaks <- c(1650, 1450, 1250, 1050) extracted <- spec_values_at(CoHAspec, peaks) # Use exact matching only extracted <- spec_values_at(CoHAspec, peaks, method = "exact") # Get results in long format extracted <- spec_values_at(CoHAspec, peaks, format = "long") # Use interpolation extracted <- spec_values_at(CoHAspec, peaks, method = "interpolate") ## End(Not run)