#> tweaking rgl

1 Introduction

This data set consists of a time series of 84 spectra of an unstable laser emission at 405 nm recorded during ca. 1.5 h.

The spectra were recorded during the installation of the 405 nm laser at a Raman spectrometer. There is no Raman scattering involved in this data, but the Raman software recorded the abscissa of the spectra as Raman shift in wavenumbers.

This document shows:

  • How to convert the wavelength axis (spectral abscissa),
  • How to display time series data as intensity over time diagram,
  • How to display time series data as 3d and false colour image of intensity as function of time and wavelength.

2 Loading the Data and Preprocessing

Load the hyperSpec package.

Read the data files:

# Renishaw file
file <- system.file("extdata/laser.txt.gz", package = "hyperSpec")
laser <- read.txt.Renishaw(file, data = "ts")
#> Warning: Function 'read.txt.Renishaw' is deprecated. 
#> Please, find alternatives in package 'hySpc.read.txt'
#> https://r-hyperspec.github.io/hySpc.read.txt
plot(laser, "spcprctl5")
The raw laser emission spectra.

Figure 2.1: The raw laser emission spectra.

As the laser emission was recorded with a Raman spectrometer, the wavelength axis initially is the Raman shift in wavenumbers (cm-1).

As most of the spectra do not show any signal (fig. 2.1), so the spectral range can be cut to -75 – 0 cm-1. Note that negative numbers in the spectral range specification with the tilde do not exclude the spectral range but rather mean negative values of the wavelength axis. The results are shown in figure 2.2.

laser <- laser[, , -75 ~ 0]
plot(laser, "spcprctl5")
The cut spectra laser emission spectra.

Figure 2.2: The cut spectra laser emission spectra.

The wavelength axis was recorded as Raman shift from 405 nm. However, the spectra were taken before calibrating the wavelength axis. The band at -50 cm-1 is known to be at 405 nm.

wl(laser) <- wl(laser) + 50

Furthermore, as the spectra are not Raman shift but emission, the wavelength axis should be converted to proper wavelengths in nm.

The Raman shift is calculated from the wavelength as follows in equation (2.1) with \(\Delta\tilde\nu\) being the Raman shift, and \(\lambda_0\) the excitation wavelength for a Raman process, here 405 nm.

\[\begin{equation} \Delta\tilde\nu = \frac{1}{\lambda_0} - \frac{1}{\lambda} \tag{2.1} \end{equation}\]

The wavelengths corresponding to the wavenumbers are thus:

\[\begin{equation} \lambda = \frac{1}{ \frac{1}{\lambda_0} - \Delta\tilde\nu} \tag{2.2} \end{equation}\]

Taking into account that 1 cm = 10\(^7\) nm, we arrive at the new wavelength axis:

wl(laser) <- list(
  wl = 1e7 / (1 / 405e-7 - wl(laser)),
  label = expression(lambda / nm)
)
plot(laser, "spcprctl5")
The spectra with wavelength axis.

Figure 2.3: The spectra with wavelength axis.

Note that the new wavelength axis label is immediately assigned as well.

laser$filename <- NULL
laser
#> hyperSpec object
#>    84 spectra
#>    2 data columns
#>    36 data points / spectrum

This version of laser datasets is shipped with package hyperSpec.

3 Inspecting the Time Dependency of the Laser Emission

The maxima of the different emission lines encountered during this measurement are at 405.0, 405.1, 405.3, and 405.4 nm (fig. 3.1).

Alternatively they can be extracted from the graph using locator() which reads out the coordinates of the points the user clicks with the mouse (use middle or right click to end the input):

wls <- locator()$x
plot(laser, "spcmeansd")

cols <- c("black", "blue", "red", "darkgreen")
abline(v = wls, col = cols)
The spectral position of the bands in the laser emission time series.

Figure 3.1: The spectral position of the bands in the laser emission time series.

Function plotc() can also be used to plot time-series. In that case, the abscissa needs to be specified in parameter use.c. The collection time is stored in column $t in seconds from start of the measurement, and can be handed over as the column name. The resulting time series are shown in figure 3.2.

plotc(laser[, , wls], spc ~ t, groups = .wavelength, type = "b", cex = 0.3, col = cols)
The time series data.
The colors in this plot correspond to colors in fig. \@ref(fig:ts).

Figure 3.2: The time series data. The colors in this plot correspond to colors in fig. 3.2.

Another option is to condition the plot on \(\lambda\) 3.3.

plotc(laser[, , wls], spc ~ t | .wavelength, type = "b", cex = 0.3, col = "black")
The time series plots can also be conditioned on `$.wavelength`{.r}.

Figure 3.3: The time series plots can also be conditioned on $.wavelength.

4 False-Colour Plot of the Spectral Intensity Over Wavelength and Time

Package hyperSpec supplies functions to draw the spectral matrix using package lattice’s levelplot().

plot(laser, "mat", contour = TRUE, col = "#00000060")
The spectra matrix of the `laser`{.r} data set.
The ordinate of the plot may be the number of the spectrum accessed by `$.row`{.r} (here) or any other extra data column, as, e.g., `$t`{.r} in fig. \@ref(fig:plotmatt).

Figure 4.1: The spectra matrix of the laser data set. The ordinate of the plot may be the number of the spectrum accessed by $.row (here) or any other extra data column, as, e.g., $t in fig. 4.2.

Package hyperSpec’s levelplot() method can be used to display the spectra matrix over a data column (instead of the row number): fig. 4.1, 4.2. Note that the hyperSpec object is the second argument to the function (according to the notation in levelplot()).

levelplot(spc ~ .wavelength * t, laser, contour = TRUE, col = "#00000080")
The spectra matrix of the `laser`{.r} data set.
The ordinate of the plot may be any extra data column, here `$t`{.r}.

Figure 4.2: The spectra matrix of the laser data set. The ordinate of the plot may be any extra data column, here $t.

5 A 3D Plot of the Spectral Intensity Over Wavelength and Time

Class hyperSpec objects can be drawn with package rgl[1]:

Package rgl’s function persp3d() plots a surface in 3d defined by points in x, y, and z. Handing over the appropriate data columns of the hyperSpec object is easy (fig. 5.1).

laser <- laser[, , 404.8 ~ 405.6] / 10000
laser$t <- laser$t / 3600
cols <- rep(matlab.palette(nrow(laser)), nwl(laser))

surface3d(y = wl(laser), x = laser$t, z = laser$spc, col = cols)
surface3d(
  y = wl(laser), x = laser$t, z = laser$spc + .1 * min(laser),
  col = "black", alpha = .2, front = "lines", line_antialias = TRUE
)

aspect3d(c(1, 1, 0.25))

axes3d(c("x+-", "y--", "z--"))
axes3d("y--", nticks = 25, labels = FALSE)
mtext3d("t / h", "x+-", line = 2.5)
mtext3d("lambda / nm", "y--", line = 2.5)
mtext3d("I / a.u.", "z--", line = 2.5)
The 3d plot of the laser data.

Figure 5.1: The 3d plot of the laser data.

Session Info

sessioninfo::session_info("hyperSpec")
#> ─ Session info ───────────────────────────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.1.0 (2021-05-18)
#>  os       macOS Catalina 10.15.7      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       UTC                         
#>  date     2021-07-14                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────────────────────────
#>  package        * version    date       lib source        
#>  brio             1.1.2      2021-04-23 [1] CRAN (R 4.1.0)
#>  callr            3.7.0      2021-04-20 [1] CRAN (R 4.1.0)
#>  cli              3.0.0      2021-06-30 [1] CRAN (R 4.1.0)
#>  colorspace       2.0-2      2021-06-24 [1] CRAN (R 4.1.0)
#>  crayon           1.4.1      2021-02-08 [1] CRAN (R 4.1.0)
#>  desc             1.3.0      2021-03-05 [1] CRAN (R 4.1.0)
#>  diffobj          0.3.4      2021-03-22 [1] CRAN (R 4.1.0)
#>  digest           0.6.27     2020-10-24 [1] CRAN (R 4.1.0)
#>  dplyr            1.0.7      2021-06-18 [1] CRAN (R 4.1.0)
#>  ellipsis         0.3.2      2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate         0.14       2019-05-28 [1] CRAN (R 4.1.0)
#>  fansi            0.5.0      2021-05-25 [1] CRAN (R 4.1.0)
#>  farver           2.1.0      2021-02-28 [1] CRAN (R 4.1.0)
#>  generics         0.1.0      2020-10-31 [1] CRAN (R 4.1.0)
#>  ggplot2        * 3.3.5      2021-06-25 [1] CRAN (R 4.1.0)
#>  glue             1.4.2      2020-08-27 [1] CRAN (R 4.1.0)
#>  gtable           0.3.0      2019-03-25 [1] CRAN (R 4.1.0)
#>  hyperSpec      * 0.100.0    2021-07-14 [1] local         
#>  hySpc.testthat   0.2.1.9000 2021-06-15 [1] local         
#>  isoband          0.2.5      2021-07-13 [1] CRAN (R 4.1.0)
#>  jpeg             0.1-8.1    2019-10-24 [1] CRAN (R 4.1.0)
#>  jsonlite         1.7.2      2020-12-09 [1] CRAN (R 4.1.0)
#>  labeling         0.4.2      2020-10-20 [1] CRAN (R 4.1.0)
#>  lattice        * 0.20-44    2021-05-02 [2] CRAN (R 4.1.0)
#>  latticeExtra     0.6-29     2019-12-19 [1] CRAN (R 4.1.0)
#>  lazyeval         0.2.2      2019-03-15 [1] CRAN (R 4.1.0)
#>  lifecycle        1.0.0      2021-02-15 [1] CRAN (R 4.1.0)
#>  magrittr         2.0.1      2020-11-17 [1] CRAN (R 4.1.0)
#>  MASS             7.3-54     2021-05-03 [2] CRAN (R 4.1.0)
#>  Matrix           1.3-3      2021-05-04 [2] CRAN (R 4.1.0)
#>  mgcv             1.8-35     2021-04-18 [2] CRAN (R 4.1.0)
#>  munsell          0.5.0      2018-06-12 [1] CRAN (R 4.1.0)
#>  nlme             3.1-152    2021-02-04 [2] CRAN (R 4.1.0)
#>  pillar           1.6.1      2021-05-16 [1] CRAN (R 4.1.0)
#>  pkgconfig        2.0.3      2019-09-22 [1] CRAN (R 4.1.0)
#>  pkgload          1.2.1      2021-04-06 [1] CRAN (R 4.1.0)
#>  png              0.1-7      2013-12-03 [1] CRAN (R 4.1.0)
#>  praise           1.0.0      2015-08-11 [1] CRAN (R 4.1.0)
#>  processx         3.5.2      2021-04-30 [1] CRAN (R 4.1.0)
#>  ps               1.6.0      2021-02-28 [1] CRAN (R 4.1.0)
#>  purrr            0.3.4      2020-04-17 [1] CRAN (R 4.1.0)
#>  R6               2.5.0      2020-10-28 [1] CRAN (R 4.1.0)
#>  RColorBrewer     1.1-2      2014-12-07 [1] CRAN (R 4.1.0)
#>  rematch2         2.1.2      2020-05-01 [1] CRAN (R 4.1.0)
#>  rlang            0.4.11     2021-04-30 [1] CRAN (R 4.1.0)
#>  rprojroot        2.0.2      2020-11-15 [1] CRAN (R 4.1.0)
#>  rstudioapi       0.13       2020-11-12 [1] CRAN (R 4.1.0)
#>  scales           1.1.1      2020-05-11 [1] CRAN (R 4.1.0)
#>  testthat         3.0.4      2021-07-01 [1] CRAN (R 4.1.0)
#>  tibble           3.1.2      2021-05-16 [1] CRAN (R 4.1.0)
#>  tidyselect       1.1.1      2021-04-30 [1] CRAN (R 4.1.0)
#>  utf8             1.2.1      2021-03-12 [1] CRAN (R 4.1.0)
#>  vctrs            0.3.8      2021-04-29 [1] CRAN (R 4.1.0)
#>  viridisLite      0.4.0      2021-04-13 [1] CRAN (R 4.1.0)
#>  waldo            0.2.5      2021-03-08 [1] CRAN (R 4.1.0)
#>  withr            2.4.2      2021-04-18 [1] CRAN (R 4.1.0)
#>  xml2             1.3.2      2020-04-23 [1] CRAN (R 4.1.0)
#> 
#> [1] /Users/runner/work/_temp/Library
#> [2] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

References

[1] D. Adler, D. Murdoch, Rgl: 3D visualization using opengl, 2021. https://CRAN.R-project.org/package=rgl.