Title: | Functions Supporting Packages ChemoSpec and ChemoSpec2D |
---|---|
Description: | Functions supporting the common needs of packages 'ChemoSpec' and 'ChemoSpec2D'. |
Authors: | Bryan A. Hanson [aut, cre] , Tejasvi Gupta [ctb] (implemented ggplot2 graphics) |
Maintainer: | Bryan A. Hanson <[email protected]> |
License: | GPL-3 |
Version: | 1.0.4 |
Built: | 2024-10-31 04:05:48 UTC |
Source: | https://github.com/bryanhanson/chemospecutils |
Functions supporting the packages ChemoSpec and ChemoSpec2D.
Bryan A. Hanson (DePauw University), Tejasvi Gupta.
Maintainer: Bryan A. Hanson [email protected]
Useful links:
Report bugs at https://github.com/bryanhanson/ChemoSpecUtils/issues
The basic procedure is to compare x[n + 1] - x[n]
for successive values of
n. When this value jumps, there is a gap which is flagged. beg.indx
and end.indx
will always be contiguous as indices must be; it is the
x
values that jump or have the gap. The indices are provided as they
are more convenient in some programming contexts. If not assigned, the
result appears at the console.
check4Gaps(x, y = NULL, silent = FALSE, tol = NULL, ...)
check4Gaps(x, y = NULL, silent = FALSE, tol = NULL, ...)
x |
A numeric vector to be checked for gaps. |
y |
An optional vector of |
silent |
Logical indicating a "no gap" message
should not be reported to the console. Important because
|
tol |
A number indicating the tolerance for checking to see if the step
between successive |
... |
Other parameters to be passed to the plot routines if
|
A data frame giving the data chunks found, with one chunk per row.
Also a plot if y
is provided. In the event there are no gaps found,
a data frame with one row is returned. The data frame has columns as follows:
beg.freq |
The first frequency value in a given data chunk. |
end.freq |
The last frequency value in a given data chunk. |
size |
The length (in frequency units) of the data chunk. |
beg.indx |
The index of the first frequency value in the data chunk. |
end.indx |
The index of the last frequency value in the data chunk. |
Bryan A. Hanson (DePauw University).
sumSpectra
which make extensive use of this function.
x <- seq(0, 2 * pi, 0.1) y <- sin(x) remove <- c(8:11, 40:45) x <- x[-remove] y <- y[-remove] gaps <- check4Gaps(x, tol = 0.11) # tol just larger than orig spacing gaps gaps <- check4Gaps(x, y, tol = 0.11) # show a plot if y given
x <- seq(0, 2 * pi, 0.1) y <- sin(x) remove <- c(8:11, 40:45) x <- x[-remove] y <- y[-remove] gaps <- check4Gaps(x, tol = 0.11) # tol just larger than orig spacing gaps gaps <- check4Gaps(x, y, tol = 0.11) # show a plot if y given
Utility function for making sure a package is available with a particular version or newer.
checkForPackageWithVersion(pkg, vers)
checkForPackageWithVersion(pkg, vers)
pkg |
Character. The name of the package to check. |
vers |
Character. The minimum acceptable version of the package. Will only be checked to the major.minor level. |
If the package with the required version or higher is available, TRUE
is returned
invisibly. Otherwise FALSE
is returned.
Bryan A. Hanson (DePauw University).
This function should be used to check the current graphics mode. It simply reports
the current setting unless it is invalid in which case the mode is set to base
.
chkGraphicsOpt(silent = TRUE)
chkGraphicsOpt(silent = TRUE)
silent |
Logical. Silences most messages if |
Character. The value of the current graphics output option/mode.
Bryan A. Hanson (DePauw University), Tejasvi Gupta.
See GraphicsOptions
for more information about the graphics options.
Utility function to verify that the structure of a Spectra
or Spectra2D
object is internally consistent.
This function should be used after manual editing of these objects.
However, in most cases rather than
directly editing these objects, one should modify them via:
chkSpectra(spectra, confirm = FALSE)
chkSpectra(spectra, confirm = FALSE)
spectra |
An object of S3 class |
confirm |
Logical indicating whether or not to write the results to the console, as would be desirable for interactive use. |
None. When used at the console, and the object is OK, no message is
written unless confirm = TRUE
. At the console, if there is a
problem, messages are issued regardless of the value of confirm
.
Bryan A. Hanson (DePauw University).
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(SrE.IR) chkSpectra(SrE.IR) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) chkSpectra(MUD1) }
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(SrE.IR) chkSpectra(SrE.IR) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) chkSpectra(MUD1) }
In ChemoSpec
and ChemoSpec2D
, the user may use any color name/format
known to R. The current color scheme of a Spectra
or
Spectra2D
object
may be determined using sumGroups
or sumSpectra
.
The colors can also be queried and changed using conColScheme
.
Colors are stored as character vectors and symbols as numeric vectors.
An important fact to keep in mind is that most people with normal vision cannot distinguish
more than about 8-12 colors, and doing so depends upon the viewing circumstances:
if on paper, printer, ink and paper type all matter, and if on a screen, the background color
makes a big difference. Further, color-blind individuals have additional challenges.
A great discussion of color issues can be found in the colorspace
package. The
Polychrome
package has further discussion and utilities for choosing qualitative
colorschemes, including those for color-blind individuals.
ChemoSpec
, but not ChemoSpec2D
, can also create plots using
the built-in symbols and lower case letters. This is useful for color-blind individuals,
plots in rgl
which can't plot regular symbols, and plots for where there are
more groups than could be reasonably coded in color. A good discussion of which symbols
are most readily distinguished can be found in Robinson: "Good Plot Symbols by Default"
Journal of Computational and Graphical Statistics DOI: 10.1080/10618600.2019.1637746
ChemoSpecUtils
supplies four color/symbol schemes for your consideration.
If the particular order of colors in any of these does not suit your needs, you can always
choose the ones you want, and/or rearrange the order, or simply provide your own.
The colors and symbols produced by gr.cols = "auto"
in the import functions.
Col8
provides eight unique colors. These are more saturated
than the automatic colors.
Col12
provides a mostly paired set of 12 unique colors suitable for groups
that come in pairs.
Col7
provides seven color-blind friendly colors. These can be visualized
at https://projects.susielu.com/viz-palette by using the hex codes obtained
by typing data(Col7); Col7
in the R
console.
Bryan A. Hanson (DePauw University).
# Make a plot showing all the built-in color options data(Col7) data(Col12) data(Sym12) data(Col8) data(Sym8) # You need to install package "RColorBrewer" to display the default color scheme if (requireNamespace("RColorBrewer", quietly = TRUE)) { auto <- RColorBrewer::brewer.pal(8, "Set1") } sp <- 0.75 # space between major plot elements tsp <- 0.15 # additional space between points and color swatches/descriptive text h <- 0.25 # height of the swatch y <- 0.0 # bottom of the plot, the reference point # empty plot plot(1:12, rep(0.0, 12), type = "n", yaxt = "n", xaxt = "n", bty = "n", xlab = "", ylab = "", ylim = c(0, 3.5) ) text(6.5, y + h + tsp * 4 + sp * 3.5, labels = "Automatic Color & Symbol Options", cex = 1.25, font = 2 ) # Col12 for (i in 1:12) { rect(i - 0.5, y, i + 0.5, y + h, border = NA, col = Col12[i]) } points(1:12, rep(y + h + tsp, 12), pch = Sym12) text(0.6, y + h + tsp * 2, adj = 0, labels = "gr.cols = 'Col12' 12 mostly paired distinct colors/symbols" ) # Col8 for (i in 1:8) { rect(i - 0.5, y + sp, i + 0.5, y + sp + h, border = NA, col = Col8[i]) } points(1:8, rep(y + h + tsp + sp, 8), pch = Sym8) text(0.6, y + h + tsp * 2 + sp, adj = 0, labels = "gr.cols = 'Col8' 8 distinct colors/symbols" ) # auto (default) if (requireNamespace("RColorBrewer", quietly = TRUE)) { for (i in 1:8) { rect(i - 0.5, y + sp * 2, i + 0.5, y + sp * 2 + h, border = NA, col = auto[i]) } points(1:8, rep(y + h + tsp + sp * 2, 8), pch = Sym8) text(0.6, y + h + tsp * 2 + sp * 2, adj = 0, labels = "gr.cols = 'auto' 8 distinct colors/symbols" ) } # colorblind-friendly for (i in 1:7) { rect(i - 0.5, y + sp * 3, i + 0.5, y + sp * 3 + h, border = NA, col = Col7[i]) } points(1:7, rep(y + h + tsp + sp * 3, 7), pch = Sym8[1:7]) text(0.6, y + h + tsp * 2 + sp * 3, adj = 0, labels = "gr.cols = 'Col7' 7 colorblind-friendly colors" )
# Make a plot showing all the built-in color options data(Col7) data(Col12) data(Sym12) data(Col8) data(Sym8) # You need to install package "RColorBrewer" to display the default color scheme if (requireNamespace("RColorBrewer", quietly = TRUE)) { auto <- RColorBrewer::brewer.pal(8, "Set1") } sp <- 0.75 # space between major plot elements tsp <- 0.15 # additional space between points and color swatches/descriptive text h <- 0.25 # height of the swatch y <- 0.0 # bottom of the plot, the reference point # empty plot plot(1:12, rep(0.0, 12), type = "n", yaxt = "n", xaxt = "n", bty = "n", xlab = "", ylab = "", ylim = c(0, 3.5) ) text(6.5, y + h + tsp * 4 + sp * 3.5, labels = "Automatic Color & Symbol Options", cex = 1.25, font = 2 ) # Col12 for (i in 1:12) { rect(i - 0.5, y, i + 0.5, y + h, border = NA, col = Col12[i]) } points(1:12, rep(y + h + tsp, 12), pch = Sym12) text(0.6, y + h + tsp * 2, adj = 0, labels = "gr.cols = 'Col12' 12 mostly paired distinct colors/symbols" ) # Col8 for (i in 1:8) { rect(i - 0.5, y + sp, i + 0.5, y + sp + h, border = NA, col = Col8[i]) } points(1:8, rep(y + h + tsp + sp, 8), pch = Sym8) text(0.6, y + h + tsp * 2 + sp, adj = 0, labels = "gr.cols = 'Col8' 8 distinct colors/symbols" ) # auto (default) if (requireNamespace("RColorBrewer", quietly = TRUE)) { for (i in 1:8) { rect(i - 0.5, y + sp * 2, i + 0.5, y + sp * 2 + h, border = NA, col = auto[i]) } points(1:8, rep(y + h + tsp + sp * 2, 8), pch = Sym8) text(0.6, y + h + tsp * 2 + sp * 2, adj = 0, labels = "gr.cols = 'auto' 8 distinct colors/symbols" ) } # colorblind-friendly for (i in 1:7) { rect(i - 0.5, y + sp * 3, i + 0.5, y + sp * 3 + h, border = NA, col = Col7[i]) } points(1:7, rep(y + h + tsp + sp * 3, 7), pch = Sym8[1:7]) text(0.6, y + h + tsp * 2 + sp * 3, adj = 0, labels = "gr.cols = 'Col7' 7 colorblind-friendly colors" )
This function permits you to change the color scheme of an existing
Spectra
or Spectra2D
object.
conColScheme(spectra, new.cols = NULL, silent = FALSE)
conColScheme(spectra, new.cols = NULL, silent = FALSE)
spectra |
An object of S3 class |
new.cols |
A character vector giving the new color values, of
|
silent |
Logical. If |
spectra An object of S3 class ChemoSpec::Spectra()
or ChemoSpec2D::Spectra2D()
.
Bryan A. Hanson (DePauw University).
For a discussion of general issues of color, see
colorSymbol
.
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(metMUD1) sumSpectra(metMUD1) newSpec <- conColScheme(metMUD1) # reports old colors newSpec <- conColScheme(metMUD1, new = c("pink", "violet")) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) sumSpectra(MUD1) newSpec <- conColScheme(MUD1) # reports old colors newSpec <- conColScheme(MUD1, new = c("pink", "violet")) }
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(metMUD1) sumSpectra(metMUD1) newSpec <- conColScheme(metMUD1) # reports old colors newSpec <- conColScheme(metMUD1, new = c("pink", "violet")) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) sumSpectra(MUD1) newSpec <- conColScheme(MUD1) # reports old colors newSpec <- conColScheme(MUD1, new = c("pink", "violet")) }
In ChemoSpec
and ChemoSpec2D
, the user may chose from the following graphics output options:
base
graphics (also the only style from the early days of ChemoSpec
through version 5).
ggplot2
graphics, the default.
plotly
graphics.
Here's how it works:
Upon starting ChemoSpec
or ChemoSpec2D
the graphics output mode
is set to ggplot2
.
To see the current value, do chkGraphicsOpt
. If by some chance the
value is corrupted it will be set to base
.
To change the graphics output mode, do options(ChemoSpecGraphics = 'option')
,
where 'option' is one of the options listed above.
ggplot2
graphics are not available for all plots. If ggplot2
graphics are not available,
base
graphics will be automatically used, regardless of the setting in
options(ChemoSpecGraphics)
.
What you can do with your plots:
Base graphics are the original graphics option in R
. They cannot be modified.
For ggplot2
graphics, ChemoSpec
and ChemoSpec2D
employ theme_bw
with only a
very few modifications. You can add things to your plot, or override the theme
used here via the usual ggplot2
methods. A few simple examples are given below
but this is not the place for a ggplot2
tutorial. See https://ggplot2.tidyverse.org/
for all things ggplot2
.
plotly
graphics is an interactive graphics option where the user can
use the tools provided by plotly
package and interact with the plot.
Bryan A. Hanson (DePauw University), Tejasvi Gupta.
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") library("ggplot2") data(metMUD1) # Using the default ggplot2 graphics p1 <- plotSpectra(metMUD1, which = c(10, 11), yrange = c(0, 1.5), offset = 0.06, amplify = 10, lab.pos = 0.5) p1 # Modifying ggplot2 graphics # Add a title p2 <- p1 + ggtitle("metMUD1 NMR Data") p2 # Zoom the axes p3 <- p1 + coord_cartesian(xlim = c(1.5, 2.0)) p3 # Change the ggplot2 theme p4 <- p1 + theme_gray() + theme(legend.position = "none") p4 # plotLoadings uses patchwork, whose plots are modified differently if (requireNamespace("patchwork", quietly = TRUE)) { pca <- c_pcaSpectra(metMUD1) p5 <- plotLoadings(metMUD1, pca, loads = c(1, 2)) p5 p6 <- p5 + patchwork::plot_annotation(title = "metMUD1 NMR Data") p6 # but to change the theme on each subplot, use the & operator p7 <- p6 & theme_gray() # compare to p6 + theme_gray() p7 } }
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") library("ggplot2") data(metMUD1) # Using the default ggplot2 graphics p1 <- plotSpectra(metMUD1, which = c(10, 11), yrange = c(0, 1.5), offset = 0.06, amplify = 10, lab.pos = 0.5) p1 # Modifying ggplot2 graphics # Add a title p2 <- p1 + ggtitle("metMUD1 NMR Data") p2 # Zoom the axes p3 <- p1 + coord_cartesian(xlim = c(1.5, 2.0)) p3 # Change the ggplot2 theme p4 <- p1 + theme_gray() + theme(legend.position = "none") p4 # plotLoadings uses patchwork, whose plots are modified differently if (requireNamespace("patchwork", quietly = TRUE)) { pca <- c_pcaSpectra(metMUD1) p5 <- plotLoadings(metMUD1, pca, loads = c(1, 2)) p5 p6 <- p5 + patchwork::plot_annotation(title = "metMUD1 NMR Data") p6 # but to change the theme on each subplot, use the & operator p7 <- p6 & theme_gray() # compare to p6 + theme_gray() p7 } }
A wrapper which performs HCA on the scores from a PCA of a
Spectra
object or POP/MIA/PARAFAC of a Spectra2D
object.
Many methods for computing the clusters and distances are
available.
hcaScores( spectra, so, scores = c(1:5), c.method = "complete", d.method = "euclidean", use.sym = FALSE, leg.loc = "topright", ... )
hcaScores( spectra, so, scores = c(1:5), c.method = "complete", d.method = "euclidean", use.sym = FALSE, leg.loc = "topright", ... )
spectra |
An object of S3 class |
so |
"Score Object" One of the following:
Any of the above score objects will have been modified to include a
list element called |
scores |
A vector of integers specifying the components (scores) to plot. |
c.method |
A character string describing the clustering method; must be
acceptable to |
d.method |
A character string describing the distance calculation
method; must be acceptable as a method in |
use.sym |
A logical; if true, use no color and use lower-case letters
to indicate group membership. Applies only to |
leg.loc |
Character; if |
... |
Parameters to be passed to the plotting routines. Applies to base graphics only. |
A list, containing an object of class hclust
and an
object of class dendrogram
. The side effect is a plot.
Bryan A. Hanson (DePauw University).
hclust
for the underlying function. See
hcaSpectra
for HCA of the entire data set stored in the
Spectra
object.
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(metMUD1) pca <- c_pcaSpectra(metMUD1) hca <- hcaScores(metMUD1, pca, main = "metMUD1 NMR Data PCA Scores") } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) mia <- miaSpectra2D(MUD1) hca <- hcaScores(MUD1, mia, scores = 1:2, main = "MUD1 MIA Scores") set.seed(123) pfac <- pfacSpectra2D(MUD1, parallel = FALSE, nfac = 2) hca <- hcaScores(MUD1, pfac, scores = 1:2, main = "MUD1 PARAFAC Scores") }
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(metMUD1) pca <- c_pcaSpectra(metMUD1) hca <- hcaScores(metMUD1, pca, main = "metMUD1 NMR Data PCA Scores") } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) mia <- miaSpectra2D(MUD1) hca <- hcaScores(MUD1, mia, scores = 1:2, main = "MUD1 MIA Scores") set.seed(123) pfac <- pfacSpectra2D(MUD1, parallel = FALSE, nfac = 2) hca <- hcaScores(MUD1, pfac, scores = 1:2, main = "MUD1 PARAFAC Scores") }
Plots the requested scores using the color scheme derived from the
Spectra
or Spectra2D
object.
Options are provided to add confidence ellipses for each group in the object.
The ellipses may be robust or classical. Option to label the extreme points provided.
plotScores( spectra, so, pcs = c(1, 2), ellipse = "none", tol = "none", use.sym = FALSE, leg.loc = "topright", ... )
plotScores( spectra, so, pcs = c(1, 2), ellipse = "none", tol = "none", use.sym = FALSE, leg.loc = "topright", ... )
spectra |
An object of S3 class |
so |
"Score Object" One of the following:
Any of the above score objects will have been modified to include a
list element called |
pcs |
A vector of two integers specifying the components (scores) to plot. |
ellipse |
A character vector specifying the type of ellipses to be
plotted. One of |
tol |
A number describing the fraction of points to be labeled. |
use.sym |
A logical; if TRUE, the color scheme is set to black and the
points plotted with symbols. Applies only to |
leg.loc |
Either a list with elements |
... |
Parameters to be passed to the plotting routines. Applies to base graphics only. |
The returned value depends on the graphics option selected (see GraphicsOptions()
).
base
: None. Side effect is a plot.
ggplot2
: The plot is displayed, and a ggplot2
object is returned if the value is assigned. The plot can be modified in the usual ggplot2
manner.
Bryan A. Hanson (DePauw University), Tejasvi Gupta.
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") # This example assumes the graphics output is set to ggplot2 (see ?GraphicsOptions). library("ggplot2") data(metMUD1) pca <- c_pcaSpectra(metMUD1) p <- plotScores(metMUD1, pca, pcs = c(1, 2), ellipse = "cls", tol = 0.05) p <- p + ggtitle("metMUD1 NMR Data") p } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") library("ggplot2") data(MUD1) mia <- miaSpectra2D(MUD1) p1 <- plotScores(MUD1, mia, tol = 0.1, ellipse = "cls") p1 <- p1 + ggtitle("MIA Scores") p1 set.seed(123) pfac <- pfacSpectra2D(MUD1, parallel = FALSE, nfac = 2) p2 <- plotScores(MUD1, pfac, tol = 0.1, leg.loc = "bottomright") p2 <- p2 + ggtitle("PARAFAC Score Plot") p2 }
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") # This example assumes the graphics output is set to ggplot2 (see ?GraphicsOptions). library("ggplot2") data(metMUD1) pca <- c_pcaSpectra(metMUD1) p <- plotScores(metMUD1, pca, pcs = c(1, 2), ellipse = "cls", tol = 0.05) p <- p + ggtitle("metMUD1 NMR Data") p } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") library("ggplot2") data(MUD1) mia <- miaSpectra2D(MUD1) p1 <- plotScores(MUD1, mia, tol = 0.1, ellipse = "cls") p1 <- p1 + ggtitle("MIA Scores") p1 set.seed(123) pfac <- pfacSpectra2D(MUD1, parallel = FALSE, nfac = 2) p2 <- plotScores(MUD1, pfac, tol = 0.1, leg.loc = "bottomright") p2 <- p2 + ggtitle("PARAFAC Score Plot") p2 }
Functions that draw a traditional scree plot, or an alternative style that is perhaps more informative. These plots illustrate the variance explained by each component in a PCA or MIA analysis.
plotScree(pca, style = "alt", ...)
plotScree(pca, style = "alt", ...)
pca |
Either:
|
style |
Character. One of |
... |
Parameters to be passed to the plotting routines. Applies to base graphics only. |
The returned value depends on the graphics option selected (see GraphicsOptions()
).
base
: None. Side effect is a plot.
ggplot2
: The plot is displayed, and a ggplot2
object is returned if the value is assigned. The plot can be modified in the usual ggplot2
manner.
Bryan A. Hanson (DePauw University), Tejasvi Gupta.
The idea for the alternative style plot came from the NIR-Quimiometria blog by jrcuesta, at https://nir-quimiometria.blogspot.com/2012/02/pca-for-nir-spectrapart-004-projections.html
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(metMUD1) pca <- c_pcaSpectra(metMUD1) p1 <- plotScree(pca, style = "trad") p1 p2 <- plotScree(pca, style = "alt") p2 } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) mia <- miaSpectra2D(MUD1) plotScree(mia, style = "alt") }
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(metMUD1) pca <- c_pcaSpectra(metMUD1) p1 <- plotScree(pca, style = "trad") p1 p2 <- plotScree(pca, style = "alt") p2 } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) mia <- miaSpectra2D(MUD1) plotScree(mia, style = "alt") }
This function removes specified frequencies from a Spectra
or Spectra2D
object.
For instance, one might want to remove regions lacking any useful
information (to reduce the data size), remove regions with large
interfering peaks (e.g. the water peak in 1H NMR) or simply focus
on a region of interest.
removeFreq(spectra, rem.freq = NULL, remF2 = NULL, remF1 = NULL)
removeFreq(spectra, rem.freq = NULL, remF2 = NULL, remF1 = NULL)
spectra |
An object of S3 class |
rem.freq |
For a |
remF2 |
Applies to |
remF1 |
As for |
An object of S3 class ChemoSpec::Spectra()
or ChemoSpec2D::Spectra2D()
.
Regarding Spectra2D
objects, one cannot remove frequencies from the interior of
a 2D NMR data set and expect
to get a meaningful contour plot, because doing so puts unrelated peaks adjacent
in the data set. This would lead to contours being drawn that don't exist in the
original data set. However, one can remove data from the interior and run a PARAFAC
analysis on the result, using the spectrum as an abstract object (that is, the
spectrum may not plottable, but the resulting scores are still meaningful).
Bryan A. Hanson (DePauw University).
removeFreq
for another way to remove data.
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(SrE.IR) sumSpectra(SrE.IR) # Examples where rem.freq is a logical vector # Remove frequencies from one end: newIR <- removeFreq(SrE.IR, rem.freq = SrE.IR$freq > 3500) # Remove frequencies from both ends at once: newIR <- removeFreq(SrE.IR, rem.freq = SrE.IR$freq > 3500 | SrE.IR$freq < 800) # Remove frequencies from the middle: newIR <- removeFreq(SrE.IR, rem.freq = SrE.IR$freq > 800 & SrE.IR$freq < 1000) # The logic of this last one is as follows. Any values # that are TRUE will be removed. values <- 1:7 values > 2 values < 6 values > 2 & values < 6 # Examples where rem.freq is a formula # Remove frequencies from one end: newIR <- removeFreq(SrE.IR, rem.freq = 3500 ~ high) # Remove frequencies from both ends is a two step process with formulas: newIR <- removeFreq(SrE.IR, rem.freq = 3500 ~ high) newIR <- removeFreq(newIR, rem.freq = low ~ 800) # Remove frequencies from the middle: newIR <- removeFreq(SrE.IR, rem.freq = 800 ~ 1000) # After any of these, inspect the results: sumSpectra(newIR) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") # Note we will set contours a bit low to better # show what is going on. data(MUD1) plotSpectra2D(MUD1, which = 7, lvls = 0.1, cols = "black", main = "MUD1 Sample 7: Complete Data Set") MUD1a <- removeFreq(MUD1, remF2 = 2.5 ~ 4) sumSpectra(MUD1a) # don't plot, removing peaks from interior is misleading MUD1b <- removeFreq(MUD1, remF2 = low ~ 2) sumSpectra(MUD1b) plotSpectra2D(MUD1b, which = 7, lvls = 0.1, cols = "black", main = "MUD1 Sample 7\nRemoved Peaks: F2 low ~ 2") MUD1c <- removeFreq(MUD1, remF1 = high ~ 23) sumSpectra(MUD1c) plotSpectra2D(MUD1c, which = 7, lvls = 0.1, cols = "black", main = "MUD1 Sample 7\nRemoved Peaks: F1 high ~ 23") MUD1d <- removeFreq(MUD1, remF2 = 2.5 ~ 4, remF1 = 45 ~ 55) sumSpectra(MUD1d) # don't plot, removing peaks from interior is misleading }
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(SrE.IR) sumSpectra(SrE.IR) # Examples where rem.freq is a logical vector # Remove frequencies from one end: newIR <- removeFreq(SrE.IR, rem.freq = SrE.IR$freq > 3500) # Remove frequencies from both ends at once: newIR <- removeFreq(SrE.IR, rem.freq = SrE.IR$freq > 3500 | SrE.IR$freq < 800) # Remove frequencies from the middle: newIR <- removeFreq(SrE.IR, rem.freq = SrE.IR$freq > 800 & SrE.IR$freq < 1000) # The logic of this last one is as follows. Any values # that are TRUE will be removed. values <- 1:7 values > 2 values < 6 values > 2 & values < 6 # Examples where rem.freq is a formula # Remove frequencies from one end: newIR <- removeFreq(SrE.IR, rem.freq = 3500 ~ high) # Remove frequencies from both ends is a two step process with formulas: newIR <- removeFreq(SrE.IR, rem.freq = 3500 ~ high) newIR <- removeFreq(newIR, rem.freq = low ~ 800) # Remove frequencies from the middle: newIR <- removeFreq(SrE.IR, rem.freq = 800 ~ 1000) # After any of these, inspect the results: sumSpectra(newIR) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") # Note we will set contours a bit low to better # show what is going on. data(MUD1) plotSpectra2D(MUD1, which = 7, lvls = 0.1, cols = "black", main = "MUD1 Sample 7: Complete Data Set") MUD1a <- removeFreq(MUD1, remF2 = 2.5 ~ 4) sumSpectra(MUD1a) # don't plot, removing peaks from interior is misleading MUD1b <- removeFreq(MUD1, remF2 = low ~ 2) sumSpectra(MUD1b) plotSpectra2D(MUD1b, which = 7, lvls = 0.1, cols = "black", main = "MUD1 Sample 7\nRemoved Peaks: F2 low ~ 2") MUD1c <- removeFreq(MUD1, remF1 = high ~ 23) sumSpectra(MUD1c) plotSpectra2D(MUD1c, which = 7, lvls = 0.1, cols = "black", main = "MUD1 Sample 7\nRemoved Peaks: F1 high ~ 23") MUD1d <- removeFreq(MUD1, remF2 = 2.5 ~ 4, remF1 = 45 ~ 55) sumSpectra(MUD1d) # don't plot, removing peaks from interior is misleading }
Removes specified groups from a Spectra
or
Spectra2D
object.
removeGroup(spectra, rem.group)
removeGroup(spectra, rem.group)
spectra |
An object of S3 class |
rem.group |
A character vector (handled as a regex) giving the groups to be removed. |
This function will report if extra data elements are found. These will probably need to be edited manually. The indices reported to the console can be helpful in this regard.
If rem.group
is a character vector, the sample
names are grepped for the corresponding values. Remember that the
grepping process is greedy, i.e. grepping for "XY" find not only "XY" but
also "XYZ".
Unused levels in $groups
are dropped.
An object of S3 class ChemoSpec::Spectra()
or ChemoSpec2D::Spectra2D()
.
Bryan A. Hanson (DePauw University).
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(SrE.IR) sumGroups(SrE.IR) SrE.IRa <- removeGroup(SrE.IR, rem.group = "pSrE") sumGroups(SrE.IRa) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) sumGroups(MUD1) MUD1a <- removeGroup(MUD1, rem.group = "Ether") sumGroups(MUD1a) }
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(SrE.IR) sumGroups(SrE.IR) SrE.IRa <- removeGroup(SrE.IR, rem.group = "pSrE") sumGroups(SrE.IRa) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) sumGroups(MUD1) MUD1a <- removeGroup(MUD1, rem.group = "Ether") sumGroups(MUD1a) }
Removes specified samples from a Spectra
or
Spectra2D
object.
removeSample(spectra, rem.sam)
removeSample(spectra, rem.sam)
spectra |
An object of S3 class |
rem.sam |
Either an integer vector specifying the samples to be removed, or a character vector (handled as a regex) giving the sample names to be removed. |
This function will report if extra data elements are found. These will probably need to be edited manually. The indices reported to the console can be helpful in this regard.
If rem.sam
is a character vector, the sample
names are grepped for the corresponding values. Remember that the
grepping process is greedy, i.e. grepping for "XY" find not only "XY" but
also "XYZ".
An object of S3 class ChemoSpec::Spectra()
or ChemoSpec2D::Spectra2D()
.
Bryan A. Hanson (DePauw University).
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(SrE.IR) # Remove the 9th spectrum/sample: SrE.IR$names SrE.IRa <- removeSample(SrE.IR, rem.sam = 9) SrE.IRa$names # Removes a spectrum/sample with this exact name: SrE.IRb <- removeSample(SrE.IR, rem.sam = "NW_adSrE") SrE.IRb$names } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) # Removes the 5th spectrum: MUD1$names MUD1a <- removeSample(MUD1, rem.sam = 5) MUD1a$names # Removes a spectrum/sample with this exact name: MUD1$names MUD1b <- removeSample(MUD1, rem.sam = "Ether_3") MUD1b$names }
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(SrE.IR) # Remove the 9th spectrum/sample: SrE.IR$names SrE.IRa <- removeSample(SrE.IR, rem.sam = 9) SrE.IRa$names # Removes a spectrum/sample with this exact name: SrE.IRb <- removeSample(SrE.IR, rem.sam = "NW_adSrE") SrE.IRb$names } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) # Removes the 5th spectrum: MUD1$names MUD1a <- removeSample(MUD1, rem.sam = 5) MUD1a$names # Removes a spectrum/sample with this exact name: MUD1$names MUD1b <- removeSample(MUD1, rem.sam = "Ether_3") MUD1b$names }
This function computes the distance between rows of a matrix using a number of methods.
It is primarily a wrapper for Dist
which provides many options.
However, cosine distance is calculated locally.
See the reference for an excellent summary of distances and similarities.
Keep in mind that distances are always positive by definition. Further, in the literature one
can find the same distance defined different ways. For instance, the definition of the
"pearson"
and "correlation"
distances differs slightly between the reference below
and Dist
. So please study the definitions carefully to get the one you want.
The example illustrates the behavior of some common distance definitions. Notice that "pearson"
and "cosine"
are mathematically identical for the particular definition of "pearson"
used by Dist
.
rowDist(x, method)
rowDist(x, method)
x |
A matrix whose rows will be used for the distance calculation. |
method |
Character; one of |
An object of class dist
.
Bryan A. Hanson (DePauw University).
R. Todeschini, D. Ballabio, V. Consonni "Distances and Similarity Measures in Chemometrics and Chemoinformatics" in Encyclopedia of Analytical Chemistry Wiley and Sons, 2020 doi:10.1002/9780470027318.a9438.pub2
# You need to install package "amap" to run the examples if (requireNamespace("amap", quietly = TRUE)) { # These examples imagines spectra as a series of vectors # on a half unit circle. # 1. Compute half of a unit circle theta <- seq(0, pi, length = 100) x = cos(theta) y = sin(theta) # 2. Compute some illustrative vectors # Get tail/origin & tip/head coordinates lt <- length(theta) set.seed(6) tips <- theta[c(1, sample(2:100, 5))] x0 <- y0 <- rep(0.0, lt) # tail/origin at 0,0 x1 <- cos(tips) # tips/heads y1 <- sin(tips) # 3. Compute the distance functions # Bounded distances RDcor <- rep(NA_real_, lt) # correlation distance RDpea <- rep(NA_real_, lt) # pearson distance RDabp <- rep(NA_real_, lt) # abspearson distance RDcos <- rep(NA_real_, lt) # cosine distance # Unbounded distances RDeuc <- rep(NA_real_, lt) # Euclidean distance RDman <- rep(NA_real_, lt) # manhattan distance # Compute all np <- 5 refVec <- c(seq(0.0, x[1], length.out = np), seq(0.0, y[1], length.out = np)) for (i in 1:lt) { Vec <- c(seq(0.0, x[i], length.out = np), seq(0.0, y[i], length.out = np)) M <- matrix(c(refVec, Vec), nrow = 2, byrow = TRUE) RDman[i] <- rowDist(M, method = "manhattan") RDeuc[i] <- rowDist(M, method = "euclidean") RDcos[i] <- rowDist(M, method = "cosine") RDcor[i] <- rowDist(M, method = "correlation") RDpea[i] <- rowDist(M, method = "pearson") RDabp[i] <- rowDist(M, method = "abspearson") } # 4. Plots # a. Unit circle w/representative vectors/spectra plot.new() plot.window(xlim = c(-1, 1), ylim = c(0, 1), asp = 1) title(main = "Representative 'Spectral' Vectors on a Unit Half Circle\nReference Vector in Red", sub = "Each 'spectrum' is represented by a series of x, y points") lines(x, y, col = "gray") # draw half circle lines(x = x[c(1,100)], y = y[c(1,100)], col = "gray") # line across bottom arrows(x0, y0, x1, y1, angle = 5) # add arrows & a red reference vector arrows(x0[1], y0[1], x1[1], y1[1], col = "red", angle = 5, lwd = 2) # b. Distances degrees <- theta*180/pi plot(degrees, RDman, type = "l", xlab = "Angle Between Spectral Vectors and Reference Vector in Degrees", ylab = "Distance", main = "Spectral Distance Comparisons\nUsing ChemoSpecUtils::rowDist") abline(h = c(1.0, 2.0), col = "gray") lines(degrees, RDeuc, col = "blue") lines(degrees, RDcos, col = "green", lwd = 4) lines(degrees, RDcor, col = "red") lines(degrees, RDabp, col = "black", lty = 2) lines(degrees, RDpea, col = "black", lty = 3) leg.txt <- c("manhattan", "euclidean", "correlation", "cosine", "pearson", "abspearson") leg.col <- c("black", "blue", "red", "green", "black", "black") leg.lwd <- c(1, 1, 1, 4, 1, 1) leg.lty <- c(1, 1, 1, 1, 3, 2) legend("topleft", legend = leg.txt, col = leg.col, lwd = leg.lwd, lty = leg.lty) }
# You need to install package "amap" to run the examples if (requireNamespace("amap", quietly = TRUE)) { # These examples imagines spectra as a series of vectors # on a half unit circle. # 1. Compute half of a unit circle theta <- seq(0, pi, length = 100) x = cos(theta) y = sin(theta) # 2. Compute some illustrative vectors # Get tail/origin & tip/head coordinates lt <- length(theta) set.seed(6) tips <- theta[c(1, sample(2:100, 5))] x0 <- y0 <- rep(0.0, lt) # tail/origin at 0,0 x1 <- cos(tips) # tips/heads y1 <- sin(tips) # 3. Compute the distance functions # Bounded distances RDcor <- rep(NA_real_, lt) # correlation distance RDpea <- rep(NA_real_, lt) # pearson distance RDabp <- rep(NA_real_, lt) # abspearson distance RDcos <- rep(NA_real_, lt) # cosine distance # Unbounded distances RDeuc <- rep(NA_real_, lt) # Euclidean distance RDman <- rep(NA_real_, lt) # manhattan distance # Compute all np <- 5 refVec <- c(seq(0.0, x[1], length.out = np), seq(0.0, y[1], length.out = np)) for (i in 1:lt) { Vec <- c(seq(0.0, x[i], length.out = np), seq(0.0, y[i], length.out = np)) M <- matrix(c(refVec, Vec), nrow = 2, byrow = TRUE) RDman[i] <- rowDist(M, method = "manhattan") RDeuc[i] <- rowDist(M, method = "euclidean") RDcos[i] <- rowDist(M, method = "cosine") RDcor[i] <- rowDist(M, method = "correlation") RDpea[i] <- rowDist(M, method = "pearson") RDabp[i] <- rowDist(M, method = "abspearson") } # 4. Plots # a. Unit circle w/representative vectors/spectra plot.new() plot.window(xlim = c(-1, 1), ylim = c(0, 1), asp = 1) title(main = "Representative 'Spectral' Vectors on a Unit Half Circle\nReference Vector in Red", sub = "Each 'spectrum' is represented by a series of x, y points") lines(x, y, col = "gray") # draw half circle lines(x = x[c(1,100)], y = y[c(1,100)], col = "gray") # line across bottom arrows(x0, y0, x1, y1, angle = 5) # add arrows & a red reference vector arrows(x0[1], y0[1], x1[1], y1[1], col = "red", angle = 5, lwd = 2) # b. Distances degrees <- theta*180/pi plot(degrees, RDman, type = "l", xlab = "Angle Between Spectral Vectors and Reference Vector in Degrees", ylab = "Distance", main = "Spectral Distance Comparisons\nUsing ChemoSpecUtils::rowDist") abline(h = c(1.0, 2.0), col = "gray") lines(degrees, RDeuc, col = "blue") lines(degrees, RDcos, col = "green", lwd = 4) lines(degrees, RDcor, col = "red") lines(degrees, RDabp, col = "black", lty = 2) lines(degrees, RDpea, col = "black", lty = 3) leg.txt <- c("manhattan", "euclidean", "correlation", "cosine", "pearson", "abspearson") leg.col <- c("black", "blue", "red", "green", "black", "black") leg.lwd <- c(1, 1, 1, 4, 1, 1) leg.lty <- c(1, 1, 1, 1, 3, 2) legend("topleft", legend = leg.txt, col = leg.col, lwd = leg.lwd, lty = leg.lty) }
Compute the distances between samples in a Spectra
or
Spectra2D
object. This is a means
to quantify the similarity between samples. A heat map style plot is an
option.
sampleDist(spectra, method = "pearson", plot = TRUE, ...)
sampleDist(spectra, method = "pearson", plot = TRUE, ...)
spectra |
An object of S3 class |
method |
Character. A string giving the distance method. See
|
plot |
Logical. Shall a level plot (heat map) be made? |
... |
Parameters to be passed to the plotting routines. Applies to base graphics only. |
A numeric matrix giving the distances between the samples.
Bryan A. Hanson (DePauw University).
For Spectra
objects, see plotSpectraDist
which compares all spectra to a single reference spectrum.
# You need to install package "lattice" for this example if (requireNamespace("lattice", quietly = TRUE)) { if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") library("lattice") data(SrE.IR) SrE.dmatrix <- sampleDist(SrE.IR, # cosine distance bounded on [0...2] method = "cosine", main = "SrE.IR Cosine Distance Between Samples" ) SrE.dmatrix <- sampleDist(SrE.IR, # abspearson distance bounded on [0...1] method = "abspearson", main = "SrE.IR Absolute Pearson Distance Between Samples" ) SrE.dmatrix <- sampleDist(SrE.IR, # euclidean distance unbounded method = "euclidean", main = "SrE.IR Euclidean Distance Between Samples" ) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") library("lattice") data(MUD1) MUD1.dmatrix <- sampleDist(MUD1, method = "cosine", main = "MUD1 Cosine Distance Between Samples" ) } }
# You need to install package "lattice" for this example if (requireNamespace("lattice", quietly = TRUE)) { if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") library("lattice") data(SrE.IR) SrE.dmatrix <- sampleDist(SrE.IR, # cosine distance bounded on [0...2] method = "cosine", main = "SrE.IR Cosine Distance Between Samples" ) SrE.dmatrix <- sampleDist(SrE.IR, # abspearson distance bounded on [0...1] method = "abspearson", main = "SrE.IR Absolute Pearson Distance Between Samples" ) SrE.dmatrix <- sampleDist(SrE.IR, # euclidean distance unbounded method = "euclidean", main = "SrE.IR Euclidean Distance Between Samples" ) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") library("lattice") data(MUD1) MUD1.dmatrix <- sampleDist(MUD1, method = "cosine", main = "MUD1 Cosine Distance Between Samples" ) } }
This function summarizes the group membership of
a Spectra
or Spectra2D
object.
sumGroups(spectra)
sumGroups(spectra)
spectra |
An object of S3 class |
A data frame as follows. Note that if there are groups with no members these are dropped.
group |
The name of the group. |
no. |
The number in the group. |
color |
The color assigned to the group. |
symbol |
The symbol assigned to the group. |
alt.symbol |
The alternative symbol assigned to the group. |
Bryan A. Hanson (DePauw University).
To summarize the entire object, sumSpectra
.
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(SrE.IR) sumGroups(SrE.IR) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) sumGroups(MUD1) }
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(SrE.IR) sumGroups(SrE.IR) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) sumGroups(MUD1) }
Provides a summary of a Spectra
or Spectra2D
object,
essentially a more spectroscopist-friendly version of str()
.
sumSpectra(spectra, ...)
sumSpectra(spectra, ...)
spectra |
An object of S3 class |
... |
Arguments to be passed downstream. Main use is to pass a value for |
Prior to summarizing, chkSpectra
is run with confirm = FALSE.
If there are problems, warnings are issued to the console and the summary is
not done.
The Spectra
or Spectra2D
object is checked to see if it contains data elements
beyond what is required. If so, these extra elements are reported to the
console.
None. Results printed at console.
Bryan A. Hanson (DePauw University).
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(SrE.IR) sumSpectra(SrE.IR) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) sumSpectra(MUD1) }
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(SrE.IR) sumSpectra(SrE.IR) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) sumSpectra(MUD1) }
A convenience function that can be used to update (change)
group names. The default group names come from the gr.crit
argument in the import functions files2SpectraObject
,
matrix2SpectraObject
or
files2Spectra2DObject
. In some cases gr.crit
may have complex regex patterns, and this function makes updating them to more
appropriate/more readible strings easier.
updateGroups(spectra, new.grps = NULL, silent = FALSE)
updateGroups(spectra, new.grps = NULL, silent = FALSE)
spectra |
An object of S3 class |
new.grps |
A vector of character values giving the new group names.
The new values must correspond to the order of the old values. This vector should
give the unique values only (so, it should have |
silent |
Logical. If |
spectra An object of S3 class ChemoSpec::Spectra()
or ChemoSpec2D::Spectra2D()
.
Bryan A. Hanson (DePauw University).
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(metMUD1) metMUD1a <- updateGroups(metMUD1) # reports old groups metMUD1a <- updateGroups(metMUD1, new.grps = c("C", "T")) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) MUD1a <- updateGroups(MUD1, new.grps = c("control", "treatment")) }
if (checkForPackageWithVersion("ChemoSpec", 6.0)) { library("ChemoSpec") data(metMUD1) metMUD1a <- updateGroups(metMUD1) # reports old groups metMUD1a <- updateGroups(metMUD1, new.grps = c("C", "T")) } if (checkForPackageWithVersion("ChemoSpec2D", 0.5)) { library("ChemoSpec2D") data(MUD1) MUD1a <- updateGroups(MUD1, new.grps = c("control", "treatment")) }