PlotFTIR

R Code for pretty plotting of FTIR spectra

https://github.com/nrcan/plotftir

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (17.3%) to scientific vocabulary

Keywords

chemometrics datavis ftir
Last synced: 6 months ago · JSON representation

Repository

R Code for pretty plotting of FTIR spectra

Basic Info
  • Host: GitHub
  • Owner: NRCan
  • License: other
  • Language: R
  • Default Branch: main
  • Homepage:
  • Size: 11.7 MB
Statistics
  • Stars: 3
  • Watchers: 2
  • Forks: 1
  • Open Issues: 3
  • Releases: 4
Topics
chemometrics datavis ftir
Created over 1 year ago · Last pushed 6 months ago
Metadata Files
Readme Changelog Contributing License Code of conduct Security

README.Rmd

---
output: github_document
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "66.67%",
  dpi = 300
)
library(magrittr)
library(PlotFTIR)
```

# PlotFTIR 

([Français](#introduction-et-installation))


[![R-CMD-check](https://github.com/NRCan/PlotFTIR/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/NRCan/PlotFTIR/actions/workflows/R-CMD-check.yaml)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
[![Coveralls test coverage](https://coveralls.io/repos/github/NRCan/PlotFTIR/badge.svg)](https://coveralls.io/github/NRCan/PlotFTIR)
[![CRAN Downloads](https://cranlogs.r-pkg.org/badges/PlotFTIR)](https://cran.r-project.org/package=PlotFTIR)


## Introduction and Installation
The goal of `PlotFTIR` is to easily and quickly kick-start the production of journal-quality Fourier Transform Infra-Red (FTIR) spectral plots in R using ggplot2. The produced plots can be published directly or further modified by ggplot2 functions.

You can install the development version of `PlotFTIR` from [GitHub](https://github.com/) with:

``` r
# install.packages("devtools")
devtools::install_github("NRCan/PlotFTIR")

library(PlotFTIR)
```

## Example Plots

This is a basic example which shows you how to plot a prepared set of FTIR spectra:

```{r basic_plot_en}
biodiesel_plot <- plot_ftir(biodiesel)
biodiesel_plot
```

You can also plot spectra in a stacked/offset manner instead of overlaid:
```{r stack_plot_en}
# Generate a plot
plot_ftir_stacked(biodiesel)
```

Note the default plot and legend titles are in english but can be automatically changed to french defaults by supplying the `lang = 'fr'` argument to plot creation functions.

Plots can be manipulated, for example, by zooming in on a range:
```{r biodiesel_zoom_en}
# Zoom to a specified range of 1850 to 1650 cm^-1
zoom_in_on_range(biodiesel_plot, c(1650, 1850))
```

Some FTIR plots have a compressed low-energy portion of the graph which you might wish to zoom in on. You can achieve this by the following:
```{r biodiesel_compress_en}
# compress the data with wavenumbers above 2000 (to the left of 2000 on the
# plot) by a factor of 5
compress_low_energy(biodiesel_plot, cutoff = 2000, compression_ratio = 5)
```

You can also add marker lines (with labels) at specific wavenumbers on the plots, controlling their line or text properties as needed. Similarly, a shaded band can be added to indicate a region.
```{r biodiesel_labelled_en}
biodiesel_marked <- add_wavenumber_marker(biodiesel_plot,
  wavenumber = 1742,
  text = "C=O Stretch",
  label_aesthetics = list("color" = "red")
) 
add_band(biodiesel_marked, c(2750,3050), "C-H Stretch")
```

If the need arises to rename samples listed in the legend, this is possible via `rename_plot_sample_ids()`. Samples must be listed in the rename vector with the format `"new name" = "old name"`. 
```{r biodiesel_rename_en}
new_names <- c(
  "0.0% Biodiesel" = "biodiesel_0",
  "0.25% Biodiesel" = "biodiesel_0_25",
  "0.50% Biodiesel" = "biodiesel_0_50",
  "1.0% Biodiesel" = "biodiesel_1_0",
  "2.5% Biodiesel" = "biodiesel_2_5",
  "5.0% Biodiesel" = "biodiesel_5_0",
  "7.5% Biodiesel" = "biodiesel_7_5",
  "10.0% Biodiesel" = "biodiesel_10_0",
  "Commercial B0.5" = "biodiesel_B0_5",
  "Commercial B5" = "biodiesel_B5",
  "Unknown Biodiesel" = "diesel_unknown"
)
rename_plot_sample_ids(biodiesel_plot, new_names)
```

A helper function for the renaming is provided (see the documentation for `get_plot_sample_ids()`).

Specific sample(s) can be highlighted (other samples greyed out) by calling `highlight_sample()`. 

Finally, plot legends are customizable (for basic changes) through a helper function `move_plot_legend()`.

## Data Sets

The package contains two datasets to provide example spectra for plotting:
* `biodiesel` is a set of diesels with 0 to 10 % FAMEs (fatty acid methyl esters; biodiesel) content, plus two known and one unknown diesel spectra.
* `sample_spectra` is a set of random FTIR spectra which includes spectra of pure toluene, isopropanol, and heptanes, as well as white printer paper and a polystyrene film.

An example of the `biodiesel` data set is below:
```{r biodiesel_head_en}
head(biodiesel)
```

## Tidy Plot Production

Note that because most functions return a data type similar to what is provided, tidy-eval is possible (using the base R pipe `|>` or [`magrittr` pipe function](https://magrittr.tidyverse.org/reference/pipe) `%>%`). 

```{r tidy_en, warning=FALSE}
library(magrittr)

new_ids <- c(
  "Toluene" = "toluene", "C7 Alkane" = "heptanes", "IPA" = "isopropanol",
  "White Paper" = "paper", "PS Film" = "polystyrene"
)

sample_spectra |>
  absorbance_to_transmittance() |>
  plot_ftir(plot_title = "Example FTIR Spectra") |>
  zoom_in_on_range(zoom_range = c(3800, 800)) |>
  compress_low_energy(compression_ratio = 4) |>
  add_wavenumber_marker(
    wavenumber = 1495,
    text = "C-C Aromatic",
    line_aesthetics = list("linetype" = "dashed"),
    label_aesthetics = list("color" = "#7e0021")
  ) |>
  add_wavenumber_marker(
    wavenumber = 3340,
    text = "O-H Alcohol",
    line_aesthetics = list("linetype" = "dotted"),
    label_aesthetics = list("color" = "#ff420e")
  ) |>
  rename_plot_sample_ids(sample_ids = new_ids) |>
  move_plot_legend(position = "bottom", direction = "horizontal")
```

## Data Manipulation

FTIR spectral data can be converted between absorbance and transmittance. Only one type of data can exist in a data.frame and be plotted. The functions `absorbance_to_transmittance()` and `transmittance_to_absorbance()` perform these conversions.

```{r convert_datatype_en}
biodiesel_transm <- absorbance_to_transmittance(biodiesel)
head(biodiesel_transm)
```

Functions are provided for adjusting the baseline of spectra, adding or subtracting scalar values from entire spectra, normalizing spectra, and averaging spectra, see:
* `recalculate_baseline()`
* `add_scalar_value()` and `subtract_scalar_value()`
* `normalize_spectra()`
* `average_spectra()`

## Reading Files

`PlotFTIR` can read .csv, .asp, and .jdx file types. The .csv or .jdx files should contain only one spectra, with columns for `wavenumber` and `absorbance` or `transmittance.` The .asp files should be according to the file specifications (not modified by the user).

## Interfacing With `ir` and `ChemoSpec` Packages

`PlotFTIR` has functions to interface with the `ir` package by Henning Teickner. This package offers complex baseline capabilities, smoothing, and more data analysis tools. More information on the `ir` package is available in their [documetation (via CRAN)](https://cran.r-project.org/package=ir). There is also capabilities to interface with `ChemoSpec` package by Bryan Hanson, which supports advanced statistics and chemometrics of spectral data. More information at [the `ChemoSpec` documentation](https://bryanhanson.github.io/ChemoSpec/index.html).

## Citing This Package

Please cite this package in any journal articles containing images produced by way of the package. If installed from GitHub or CRAN the date field will be properly filled with the publishing year.

```{r cite-en, warning=FALSE}
citation("PlotFTIR")
```

## Language Settings

The package has the ability to change language from English to French for plots on a per-plot basis (call `plot_ftir()` functions with `lang = 'en'` or `lang = 'fr` arguments). In addition, the default language can be set to English or French by setting `options('PlotFTIR.lang' = 'en')` or `options('PlotFTIR.lang' = 'fr')` respectively. This can be added to your .RProfile to persist between R sessions.

([English](#introduction-and-installation))

## Introduction et installation

Le but de `PlotFTIR` est de lancer facilement et rapidement la production des tracés de spectres de spectroscopie infrarouge à transformée de Fourier (IRTF) de qualité de revues scientifiques dans le system R en utilisant ggplot2. Les tracés produits peuvent être publiés directement ou modifiés par les fonctions ggplot2.

Vous pouvez installer la version de développement de `PlotFTIR` depuis [GitHub](https://github.com/) avec:

``` r
# install.packages("devtools")
devtools::install_github("NRCan/PlotFTIR")

library(PlotFTIR)
```

## Exemples des tracés
Ceci est un example de base qui vous montre comment tracer un ensemble de spectres IRTF dejà preparé:

```{r basic_plot_fr}
library(PlotFTIR)
plot_ftir(sample_spectra, lang = "fr")
```

Vous pouvez également tracer les spectres de manière empilée/décalée au lieu de les superposer :
```{r stack_plot_fr}
plot_ftir_stacked(biodiesel, plot_title = "Spectre IRTF empilée", lang = "fr")
```

Notez que les titres par défaut de tracé et du légende sont en anglais, mais qu'ils peuvent être automatiquement modifiés en français en fournissant l'argument `lang = 'fr'` aux fonctions de création de tracés.

Les tracés peuvent être manipulés, par exemple, en zoomant sur une plage :
```{r biodiesel_zoom_fr}
# Générer un tracé
biodiesel_trace <- plot_ftir(biodiesel, lang = "fr")
# Zoom sur une plage spécifiée de 1850 à 1650 cm^-1
zoom_in_on_range(biodiesel_trace, c(1650, 1850))
```

Certains tracés IRTF ont une partie compressée du graphique à faible énergie qui peuvent etre agrandie de la manière suivante :
```{r biodiesel_compress_fr}
# compresser les données avec des nombres d'onde supérieurs à 2000 (à gauche de
# 2000 sur le tracé) d'un facteur 5
compress_low_energy(biodiesel_trace, cutoff = 2000, compression_ratio = 5)
```

Vous pouvez également ajouter des lignes de marqueur (avec des étiquettes) à des numéros d'onde spécifiques sur les tracés, en contrôlant leurs propriétés de ligne ou de texte selon vos besoins. De même, une bande ombrée peut être ajoutée pour indiquer une région.
```{r biodiesel_labelled_fr}
biodiesel_marked <- add_wavenumber_marker(biodiesel_trace,
  wavenumber = 1742,
  text = "C=O étirement",
  label_aesthetics = list("color" = "red")
) 
add_band(biodiesel_marked, c(2750,3050), "C-H étirement")
```

S'il est nécessaire de renommer les échantillons répertoriés dans la légende, cela est possible via `rename_plot_sample_ids()`. Le vecteur de renommage doit avoir le format `"nouveau nom" = "ancien nom"`.

```{r biodiesel_rename_fr}
nouveau_noms <- c(
  "0,0% Biodiesel" = "biodiesel_0",
  "0,25% Biodiesel" = "biodiesel_0_25",
  "0,50% Biodiesel" = "biodiesel_0_50",
  "1,0% Biodiesel" = "biodiesel_1_0",
  "2,5% Biodiesel" = "biodiesel_2_5",
  "5,0% Biodiesel" = "biodiesel_5_0",
  "7,5% Biodiesel" = "biodiesel_7_5",
  "10,0% Biodiesel" = "biodiesel_10_0",
  "B0,5 Commercial" = "biodiesel_B0_5",
  "B5 Commercial" = "biodiesel_B5",
  "Biodiesel Inconnu" = "diesel_unknown"
)
rename_plot_sample_ids(biodiesel_trace, nouveau_noms)
```

Une fonction d'assistance pour le changement de nom est fournie (voir la documentation pour `get_plot_sample_ids()`).

Un ou plusieurs échantillons spécifiques peuvent être mis en évidence (les autres échantillons étant grisés) en appelant `highlight_sample()`. 

Enfin, les légendes des tracés sont personnalisables (pour les modifications de base) via une fonction d'assistance `move_plot_legend()`.

## Production de tracés "tidy" 

Notez que comme la plupart des fonctions renvoient un type de données similaire à celui qui est fourni, l'évaluation des données est possible (en utilisant [la fonction `magrittr` tuyau](https://magrittr.tidyverse.org/reference/pipe) `%>%`).

```{r tidy_fr, warning=FALSE}
library(magrittr)

nouveaux_ids <- c(
  "Toluène" = "toluene", "C7 alcane" = "heptanes", "alcool isopropylique" = "isopropanol",
  "papier blanc" = "paper", "film de polystyrène" = "polystyrene"
)

sample_spectra |>
  absorbance_to_transmittance() |>
  plot_ftir(plot_title = "Exemple de spectres IRTF", lang = "fr") |>
  zoom_in_on_range(zoom_range = c(3800, 800)) |>
  compress_low_energy(compression_ratio = 4) |>
  add_wavenumber_marker(
    wavenumber = 1495,
    text = "C-C aromatique",
    line_aesthetics = list("linetype" = "dashed"),
    label_aesthetics = list("color" = "#7e0021")
  ) |>
  add_wavenumber_marker(
    wavenumber = 3340,
    text = "O-H alcool",
    line_aesthetics = list("linetype" = "dotted"),
    label_aesthetics = list("color" = "#ff420e")
  ) |>
  rename_plot_sample_ids(sample_ids = nouveaux_ids) |>
  move_plot_legend(position = "bottom", direction = "horizontal")
```

## Production de tracés "tidy" 

Notez que, comme la plupart des fonctions renvoient un type de données similaire à celui fourni, une "tidy-eval" est possible.

## Ensembles des données

Le package contient deux ensembles de données pour fournir des exemples de spectres à tracer:
* `biodiesel` est un ensemble de diesels avec une teneur en esters méthyliques d'acides gras (EMAGs) (ou biodiesel) de 0 à 10 %, plus deux spectres de diesel connus et un inconnu.
* `sample_spectra` est un ensemble de spectres IRTF aléatoires qui comprennent des spectres de toluène pur, d'isopropanol et d'heptanes, ainsi que du papier d'imprimante blanc et un film de polystyrène.

Un exemple de l'ensemble de données `biodiesel` est ci-dessous:
```{r biodiesel_head_fr}
head(biodiesel)
```

## Manipulation de données

Les données spectrales IRTF peuvent être converties entre l'absorbance et la transmission. Un seul type de données peut exister dans un data.frame et être tracé. Les fonctions `absorbance_to_transmittance()` et `transmittance_to_absorbance()` effectuent ces conversions.

```{r convert_datatype_fr}
biodiesel_transm <- absorbance_to_transmittance(biodiesel)
head(biodiesel_transm)
```

Des fonctions sont fournies pour ajuster la ligne de base des spectres, ajouter ou soustraire des valeurs scalaires de spectres entiers, normalisation des spectres, et calculer la moyenne des spectres, voir :
* `recalculate_baseline()`
* `add_scalar_value()` et `subtract_scalar_value()`
* `normalize_spectra()`
* `average_spectra()`

## Lecture des fichiers

`PlotFTIR` peut lire les fichiers de type .csv, .asp, et .jdx. Le fichier .csv ou .jdx ne doit contenir qu'un seul spectre, avec des colonnes pour le `wavenumber` et `absorbance` ou `transmittance`. Les fichiers .asp doivent être conformes aux spécifications du fichier (non modifiées par l'utilisateur).

## Interfaçage avec le paquetage `ir`

`PlotFTIR` possède des fonctions pour s'interfacer avec le paquet `ir` de Teickner. Ce package offre des capacités de lignes de base complexes, de lissage, et plus d'outils d'analyse de données. Plus d'informations sur le paquet `ir` sont disponibles dans leur [documetation (via CRAN)] (https://cran.r-project.org/package=ir). Il est également possible de s'interfacer avec le paquet `ChemoSpec` de Bryan Hanson, qui prend en charge les statistiques avancées et la chimiométrie des données spectrales. Plus d'informations sur [la documentation de `ChemoSpec`] (https://bryanhanson.github.io/ChemoSpec/index.html).

## Citer ce paquet

Veuillez citer ce paquet dans tout article de journal contenant des images produites à l'aide de ce paquet. Si le paquet est installé à partir de GitHub ou de CRAN, le texte de la date sera correctement rempli avec l'année de publication.

```{r cite-fr, warning=FALSE}
citation("PlotFTIR")
```

## Paramètres de langue

Le paquetage a la capacité de changer la langue de l'anglais au français pour les tracés sur une base individuelle (appeler les fonctions `plot_ftir()` avec les arguments `lang = 'en'` ou `lang = 'fr'`). De plus, la langue par défaut peut être réglée sur l'anglais ou le français en réglant `options('PlotFTIR.lang' = 'en')` ou `options('PlotFTIR.lang' = 'fr')` respectivement. Ceci peut être ajouté à votre .RProfile pour persister entre les sessions R.

Owner

  • Name: Natural Resources Canada
  • Login: NRCan
  • Kind: organization
  • Location: Canada

GitHub Events

Total
  • Create event: 13
  • Release event: 4
  • Issues event: 21
  • Watch event: 1
  • Delete event: 7
  • Issue comment event: 9
  • Push event: 56
  • Public event: 1
  • Pull request event: 12
  • Fork event: 1
Last Year
  • Create event: 13
  • Release event: 4
  • Issues event: 21
  • Watch event: 1
  • Delete event: 7
  • Issue comment event: 9
  • Push event: 56
  • Public event: 1
  • Pull request event: 12
  • Fork event: 1

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 7
  • Total pull requests: 5
  • Average time to close issues: about 1 month
  • Average time to close pull requests: 1 day
  • Total issue authors: 1
  • Total pull request authors: 1
  • Average comments per issue: 0.43
  • Average comments per pull request: 0.2
  • Merged pull requests: 4
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 7
  • Pull requests: 5
  • Average time to close issues: about 1 month
  • Average time to close pull requests: 1 day
  • Issue authors: 1
  • Pull request authors: 1
  • Average comments per issue: 0.43
  • Average comments per pull request: 0.2
  • Merged pull requests: 4
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • pbulsink (13)
Pull Request Authors
  • pbulsink (7)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 256 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 4
  • Total maintainers: 1
cran.r-project.org: PlotFTIR

Plot FTIR Spectra

  • Versions: 4
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 256 Last month
Rankings
Dependent packages count: 27.8%
Dependent repos count: 34.3%
Average: 49.7%
Downloads: 87.0%
Last synced: 6 months ago

Dependencies

.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v4 composite
  • actions/checkout v3 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pr-commands.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/pr-fetch v2 composite
  • r-lib/actions/pr-push v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/test-coverage.yaml actions
  • actions/checkout v4 composite
  • actions/upload-artifact v4 composite
  • codecov/codecov-action v4 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
DESCRIPTION cran
  • R >= 3.3 depends
  • cli * imports
  • rlang * imports
  • scales * imports
  • ChemoSpec * suggests
  • R.utils * suggests
  • ggplot2 >= 3.0.0 suggests
  • ggthemes * suggests
  • ir * suggests
  • knitr * suggests
  • rmarkdown * suggests
  • testthat >= 3.0.0 suggests
  • withr * suggests