stats, geoms and annotations suitable for light spectra. It also defines
autoplot() methods specialized
for the classes defined in package
photobiology for storing
different types of spectral data. This vignette describes the use of
The package uses ‘ggplot2’, ‘photobiology’ and ‘photobiologyWavebands’. More importantly it defines specializations of methods defined in ‘ggplot2’ and exports methods epecialized for classes defined in package ‘photobiology’, consequently both ‘ggplot2’ and ‘photobiology’ are loaded automatically when package ‘ggspectra’ is loaded. This is the behaviour from version 0.9.26 onwards, when using earlier versions these packages needed to be loaded explicitly.
We bind two spectra into an object to be used later to demonstrate grouping.
We change the default theme.
The most automatic way of plotting spectral data stored in one of the
classes defined in package
photobiology is to use the
autoplot() methods (for compatibility with versions <
plot() remains as a deprecated name for the same
autoplot() methods defined by ‘ggplot2’ they
take advantage of all the data and metadata stored in objects of the
special classes used to store spectra. This allows the automatic
construction of axis labels as quantities and units are well
Here we use the same example
source_spct object from
package ‘photobiology’ used in User Guide 1 to demonstrate
parameters common to all
autoplot() methods for spectra.
Methods for plotting of objects of all the classes defined in package
solute_spct are available. Function
spct_classes() lists these classes.
##  "calibration_spct" "raw_spct" "cps_spct" "filter_spct" ##  "reflector_spct" "source_spct" "object_spct" "response_spct" ##  "chroma_spct" "solute_spct" "generic_spct"
The simplest possible call to
autoplot() needs only one
argument, the object to plot.
In contrast to the examples in the User Guide 1, here we
obtain directly an annotated plot of the solar spectrum at ground level.
This would be of limited use unless without the possibility to adjust
the design and components of the ggplot object created. One
possibility is to use the grammar of graphics to add to the plot.
However, it is also possible to control many features of the plot by
passing additional arguments in the call to
objects we can change the basis of expression of spectral irradiance
"photon". Be aware that by
design, all values, summaries and spectral data will always use the same
base of expression.
We can build and area + line (
"spct") plot instead the
default line plot.
If a single object contains multiple spectra in long form, they are all plotted. In this case no summary values are displayed as they would overlap.
In addition to the
autoplot() methods for individual
spectra, there are
autoplot() methods available for
collections of spectra. Collections of spectra like
two_suns.mspct can be plotted also automatically, as they
are combined on-the-fly and plotted as shown above in the example for
We use in the examples a simple collection of two spectra. The output
and accepted arguments are the same as those in the
autoplot() methods for multiple spectra in long form
described above. The plotting of objects of all the classes for
collections of spectra defined in package ‘photobiology’ is similar.
generic_mspct are currently not supported. Function
mspct_classes() lists all the classes for collections of
spectra defined in package ‘photobiology’.
##  "calibration_mspct" "raw_mspct" "cps_mspct" ##  "filter_mspct" "reflector_mspct" "source_mspct" ##  "object_mspct" "solute_mspct" "response_mspct" ##  "chroma_mspct" "generic_mspct"
The plot using defaults is identical to that above.
We can choose a different name for the factor identifying the spectra in the collection.
With facets, there is only one spectrum per panel. Summaries per waveband are shown.
Above we showed the default faceting by passing
facets = TRUE. We can set the number of columns by passing
a number. For panels in one column we pass
facets = 1.
For two columns we pass
facets = 2.
We can zoom into the spectrum, by providing a wavelength range in
nanometres. Here we pass a vector of two numbers as argument, but any
object for which a
range() method is available can also be
used, including longer numeric vectors, objects of one the spectrum
classes, such as class
source_spct, or a
We can show summaries for different wavebands, by passing them to
w.band. We can pass either individual wavebands
or lists of wavebands. First a list of wavebands created with a
constructor, here showing the limits based on the ISO standard.
A single waveband can also be created with a generic constructor.
NULL as argument for
w.band is replaced by a
waveband covering the full range of the spectral data. The whole
range is the range plotted, which is controlled by the argument
Summarizing the examples above, arguments passed to
w.band play very different roles.
range, gives the wavelengths to include in the
plot, and the default for
range is always the range of
wavelengths in the spectrum being plotted. The argument to
w.band is only used for the annotations and
decorations, described in a leater section in more detail.
The effect of
range is slightly different to the effect
range is used to
trim the spectral data before passing it to
ggplot, using interpolation when needed (see
photobiology::trim_wl()). In contrast
discards data for all wavelengths not within the range.
The default argument to
can be changed by setting the R option“photobiology.plot.bands”`
to a single waveband object or to a list waveband objects.
set_w.band_default() allows to set this option
using the same syntax as described for parameter
See package ‘photobiologyWavebands’ for waveband constructors for ISO
and other waveband definitions in common use, and package ‘photobiology’
for defining your own.
All plotting of collections of spectra shown above were done with the
plot.data = "as.is". We can also plot a row-wise
summary (wavelength by wavelength, or parallel summaries) of
the spectra in the collection, here the
mean of the
spectra. Currently implemented only for spectra with identical
wavelength vectors and of the same class.
Although the defaults result in the addition of frequently useful
autoplot() methods accepts arguments for
several parameters that make possible flexible control of the
annotations. Currently, numerical summaries can be added automatically
to plots only when a single spectrum is plotted per plot panel.
In this first example we pass
"mean" as argument to
label.qty, to print means instead of integrals in the
labels. Note that the units and quantity labels for the waveband
summaries have also changed.
label.qty values need explanation. The first one,
"relative" displays in labels the relative contribution of
the integral of each waveband, to the sum of the integrals of all
wavebands displayed in the plot. The second one,
"contribution" displays in labels the integral of each
waveband divided by the integral of the whole spectrum displayed in the
plot. Consequently, this second option should be interpreted with
caution, as the spectral data is unlikely in many cases to include the
whole emission or absorption spectrum of a source. Adding
.pc to the arguments, that is, using
"contribution.pc", results in
the corresponding values being displayed as percentages.
"contribution.pc" unless the summarized
wavebands cover the whole range of wavelengths in the spectrum, the sum
of the summary values is less than 100.
"relative.pc" the sum adds to 100.
Which annotations are included can be controlled through
annotations. It accepts a
vector, or a list of
character vectors as argument. Three
values have special meaning if at the head of the vector (with index =
"=" means override the current defaults with the
annotations that follow in the vector,
"+" means add to the
current default the annotations that follow, and
remove from the current default the annotations that follow. A
NULL value means use package-defined defaults as is,
"" means no annotations, and
means no annotations, but expand axis limits and set identity scales
ready for manually adding annotations. Used together with
"valleys*" are wildcards that remove all flavours
of each of these annotations. In cases when the intention is to both add
and remove annotations from the default, the argument to parameter
annotations can be a list of character vectors, which are
interpreted as above but operated upon sequentially.
Which annotations are included by default can be changed by setting R
"photobiology.plot.annotations" to a character
vector with the names of the desired default annotations. Function
set_annotations_default() allows to set or modify this
option using the same syntax as described for parameter
|annotation||default for classes||overrides|
Titles come in different flavours. Package ‘ggplot2’ supports titles,
subtitles and captions. The
"title" as argument to
autoplot() methods and
autotitle() takes up to three optional
modifiers separated by colons that can be used to specify the contents
of the automatic title, subtitle and caption. Currently
supported modifiers are shown in the table below. If the metadata item
is not stored in the spectral object, the title or subtitle will show
NA. See the documentation of packages ‘photobiology’ and
‘ooacquire’ for information on how to set and unset attributes of
spectral objects. To add an arbitrary title and/or subtitle to a plot,
use either function
ggtitle() or function
labs() from package ‘ggplot2’.
|Modifier||text source used|
||name of the object plotted|
||class of the plotted object|
||spectrometer serial number|
||no title, no subtitle or no caption|
"title:objt", the default for
"title", adds a title with the name of the object being
how use the
how.measured attributes if available. For example
"title:what:when" will use the
attribute for the title and the
when.measured attribute for
We add a title, subtitle and caption.
We use “none” as a filler so that only subtitle and caption are added to the plot.
"boundaries", we adds horizontal dashed line(s)
showing the valid range of values. It does not override any other
annotation. (The boundary line is shown in red when a plot displays
out-of-range spectral data.)
We can list all the annotations to be included in a plot, in which
"=" is optional so as to maintain compatibility with
As indicated in the table above, some annotations override other
annotations which fulfill a similar role. Here
"boxes", included in the default
We can also remove some of the default annotations on a case by case basis.
The behaviour of some annotations can be tweaked. Below we add
"valleys" as annotations, and control with
span how close to each other are the peaks and valleys
"valleys". They use the repulsive geometry
geom_label_repel from package ‘ggrepel’.
"" as argument to
results in a plot with no annotations, and no extra expansion of scale
"reserve.space" as argument to
annotations results in a plot with no annotations, but with
scale limits expanded so as to receive annotations.
The size of the font used for the annotations is controlled by
ylim allows to manually set the limits of the
\(y\) axis using the same syntax as in
package ‘ggplot2’. The annotations are still automatically positioned,
and the range extended to make space for them. In other words the values
ylim still give the “space” available for
The time base of the spectral unit or the duration of the exposure is stored as metadata. As demonstrated here using spectral data integrated over 24 h, a one-day-long exposure, the units in the axis labels change according to the value stored in the metadata.
##  "day"
Even though the
autoplot() methods can return a finished
plot, the returned object is a
ggplot object and can be
built upon by adding additional elements like facets,
aesthetics and even additional layers. We pass
idfactor = NA to suppress the mapping of the spectra to
It is possible to construct and bind the spectra on-the-fly, and to
use arbitrary variable names for the index factor. This works
automatically as long as row binding is done with function
rbindspct() which saves the name of the factor.
In the examples above the
sun.spct was used. The
autoplot() methods for
other spectral classes have only slight differences. We show some
filter_spct objects. For a long-pass filter
the wavelength at half maximum is more interesting than peaks or
In many cases it is possible to convert on-the-fly the quantity plotted. In this case, given that the data are clipped as absorbance, a fixed target of A = 2 for the cut-off to be labelled with the wavelength is appropriate.
If one needs to, one can add a suitable stat using ‘local’ data. A peak annotation could be added manually.
In the case of quantities like transmittance which have a certain range of valid values, both upper and lower boundaries are highlighted, but in other cases only one, or even none depending on the possible valid ranges for the spectral quantities.
Differently from other classes
cps_spct objects can contain multiple columns of data,
normally measured different integartion times, and meant to be combined
before conversion into physical quantities. In the case of raw
instrument counts data, if the spectral object contains an instrument
descriptor as metadata, the upper boundary is set to the maximum counts
of the detector.
cps_spct objects contain
multiple data columns when integration time bracketing has been used
during data acquisition. In such cases, if one wants to plot only one of
the raw spectra, one can extract the columns as usual in R using the
extraction operator (
[ ]). The single remaining column of
spectral data is automatically renamed.
If the supplied data include off-range values such as negative irradiance or fractional transmittance, reflectance, or absorptance outside the zero to one range the exceeded boundary is highlighted in red.
Using colors when adding to a plot generated by the
autoplot() methods is more involved than usual, one has to
take into account that the identity scale is in use for color in
annotations, and a ggplot can make use of only one scale for a given
aesthetic. For this same reason no color key in generated
Plots created with
autoplot() methods are
ggplot objects and can be customized, bearing in mind that
any added layers will be plotted on top or existing layers (that is
unless we make use of methods from package ‘gginnards’ that allow
insertion of layers at any position in a
Here we replace the default peaks annotations with a custom one, but still take advantage of other defaults like nice axis labels and other annotations.
autoplot() method for
waveband objects is