# Clean up to ensure reproducible workspace ----------------------------------
rm(list = ls(all.names = TRUE))
# Packages -------------------------------------------------------------------
library(hyperSpec)
library(ggplot2)
library(hySpc.ggplot2)

# Functions ------------------------------------------------------------------
source("vignette-functions.R", encoding = "UTF-8")

# Settings -------------------------------------------------------------------
source("vignette-default-settings.R", encoding = "UTF-8")

# Temporaty options ----------------------------------------------------------
# Change the value of this option in "vignette-default-settings.R"
show_reviewers_notes = getOption("show_reviewers_notes", TRUE)
dir.create("resources", showWarnings = FALSE)

knitr::write_bib(
  c(
    "hyperSpec",
    "ggplot2",
    "hySpc.ggplot2"
  ),
  file = "resources/hySpc.ggplot2-pkg.bib",
  prefix = "R_"
)

Suggested Packages and Setup {-}

```{block, type="note", echo=TRUE} Reproducing the examples in this vignette.

All spectra used in this manual are installed automatically with package hyperSpec.

***

> **Terminology**
> Throughout the documentation of the package, the following terms are used:
>
> - **wavelength** indicates any type of spectral abscissa.
> - **intensity**  indicates any type of spectral ordinate.
> - **extra data** indicates non-spectroscopic data.


```r
library(hyperSpec)
library(ggplot2)
library(hySpc.ggplot2)

theme_set(theme_bw())

set.seed(2020)

Using ggplot2 with hyperSpec objects {#plots-ggplot2}

Class hyperSpec{.r}[r cite_pkg("hyperSpec")] objects do not yet directly support plotting with package ggplot2[r cite_pkg("ggplot2")]. Nevertheless, package ggplot2 graphics can easily be obtained by using qplot*(){.r} equivalents to hyperSpec::plot_spc(){.r}, hyperSpec::plotc(){.r}, and hyperSpec::plotmap(){.r}.

Function qplotspc(){.r} {#qplotspc}

Plot spectra with qplotspc(){.r} (Fig. \@ref(fig:ggplotspc-01)).

CAPTION = "Spectra produced by `qplotspc()`{.r}: `flu` data.  "
qplotspc(flu) + aes(colour = c)
CAPTION = "Spectra with reversed order of x axis values: `paracetamol` data.  "
qplotspc(paracetamol, c(2800 ~ max, min ~ 1800)) +
  scale_x_reverse(breaks = seq(0, 3200, 400))

Let's look at the other dataset called faux_cell.

set.seed(1)
faux_cell <- generate_faux_cell()
faux_cell
CAPTION = "Ten randomly selected spectra from `faux_cell` dataset.  "
set.seed(25)
qplotspc(sample(faux_cell), spc.nmax = 10) + aes(colour = region)

Average spectra of each region.

CAPTION = "Mean spectra of each region in `faux_cell` dataset.  "
qplotspc(
  aggregate(faux_cell, faux_cell$region, mean),
  mapping = aes(x = .wavelength, y = spc, colour = region)
) +
  facet_grid(region ~ .)

Mean $\pm$ standard deviation:

CAPTION = "Mean $\\pm$ standard deviation spectra of each region in `faux_cell` dataset.  "
qplotspc(
 aggregate(faux_cell, faux_cell$region, mean_pm_sd),
 mapping = aes(x = .wavelength, y = spc, colour = region, group = .rownames)
) +
  facet_grid(region ~ .)

Function qplotc(){.r} {#qplotc}

This function plots spectroscopic concenration, depth, time-series, etc. profiles.

By default, plots spectrum only at the first wavelength.

CAPTION = "Concentration profile: intensities at the first available wavelength value.  "
qplotc(flu)

It is better to indicate the wavelength of interest explicitly.

CAPTION = "Concentration profile: intensities at 410 nm.   "
qplotc(flu[ , , 410])
CAPTION = "Concentration profile with fitted line.  "
qplotc(flu[ , , 410]) +
  geom_smooth(method = "lm", formula = y ~ x)

Function ggplotmap(){.r} {#ggplotmap}

Fig. \@ref(fig:ggplotmap-01) shows a map created with package ggplot2.

set.seed(7)
faux_cell <- generate_faux_cell()
CAPTION = "False-colour map produced by `qplotmap()`{.r}.  "
qplotmap(faux_cell[ , , 1200])
CAPTION = "False-colour map produced by `qplotmap()`{.r}: different color palette.  "
qplotmap(faux_cell[ , , 1200]) +
    scale_fill_gradientn("Intensity", colours = palette_matlab())

If you have package viridis installed, you may try:

qplotmap(faux_cell[ , , 1200]) +
    viridis::scale_fill_viridis("Intensity")

The two special columns .wavelength{.r} and .rownames{.r} contain the wavelength axis and allow to distinguish the spectra.

Function qplotmixmap(){.r} {#qplotmixmap}

This function plots false-colour map with colour mixing for multivariate overlay.

set.seed(1)
faux_cell <- generate_faux_cell()

Without pre-processing:

CAPTION = "False-color map produced by `qplotmixmap()`{.r}: raw `faux_cell` spectra at 800, 1200, and 1500 $cm^{-1}.$    "
qplotmixmap(faux_cell[, , c(800, 1200, 1500)],
  purecol = c(matrix = "red", cell = "green", nucleus = "blue")
)

With baseline removed:

CAPTION = "False-color map of `faux_cell` spectra with base line removed.  "
faux_cell_2 <- faux_cell - spc_fit_poly_below(faux_cell)

qplotmixmap(faux_cell_2[, , c(800, 1200, 1500)],
  purecol = c(matrix = "red", cell = "green", nucleus = "blue")
)

With some further pre-processing:

CAPTION = "False-color map of pre-processed `faux_cell` spectra.  "
faux_cell_3 <- faux_cell_2
faux_cell_3 <- sweep(faux_cell_3, 1, apply(faux_cell_3, 1, mean), "/")
faux_cell_3 <- sweep(faux_cell_3, 2, apply(faux_cell_3, 2, quantile, 0.05), "-")

qplotmixmap(faux_cell_3[, , c(800, 1200, 1500)],
  purecol = c(matrix = "red", cell = "green", nucleus = "blue")
)

More General Plotting with ggplot2 {#general-plotting-ggplot2}

For more general plotting, as.long.df(){.r} transforms a hyperSpec{.r} object into a long-form data.frame{.r} that is suitable for qplot(){.r}, while as.t.df(){.r} produces a data.frame{.r} where each spectrum is one column, and an additional first column gives the wavelength. Long data.frame{.r}s can be very memory consuming as they are of size $nrow · nwl × (ncol + 2)$ with respect to the dimensions of the hyperSpec{.r} object. Thus, e.g., the faux_cell{.r} data set (r round (object.size(faux_cell) / 1048576, digits = 0) MB) as hyperSpec{.r} object) needs r round (object.size(as.long.df(faux_cell, rownames = TRUE)) / 1048576, digits = 0) MB as long-format data.frame{.r}. It is therefore highly recommended to calculate the particular data to be plotted beforehand.

CAPTION = "Mean $\\pm$ standard deviation with package **ggplot2**.  "
qplotspc(mean(faux_cell)) +
  geom_ribbon(aes(
    ymin = mean + sd,
    ymax = mean - sd,
    y = 0,
    group = NA
  ),
    alpha = 0.25,
    data = as.t.df(mean_sd(faux_cell))
  )

Note that qpotspc(){.r} specifies aesthetics y = spc and groups = .rownames, which do not have corresponding columns in the data.frame returned by as.t.df(){.r}. These aesthetics must therefore be set manually in the aesthetics definition in geom_ribbon(){.r} (or any other geom_*(){.r} that uses as.t.df(){.r}). Otherwise, errors occur that object spc (and/or .rownames) cannot be found. Cut axes can be implemented by faceting (Fig. \@ref(fig:ggplotspccut)).

CAPTION = "Plot separate regions of specra.  "
qplotspc(paracetamol / 1e4, wl.range = c(min ~ 1800, 2800 ~ max)) +
  scale_x_continuous(breaks = seq(0, 3200, 400))

Session Info {-}

Session info

sessioninfo::session_info("hyperSpec")

References {-}



r-hyperspec/hySpc.ggplot2 documentation built on May 2, 2023, 2:37 p.m.