ggplot2 with stats, geoms and annotations suitable for light spectra. It also defines
plot() methods specialized for the classes defined in package
photobiology for storing different types of spectral data. This vignette describes the use of these plot methods.
Although the package uses internally
photobiologyWavebands it does not re-export these packages, which need to loaded if used.
## ## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats': ## ## filter, lag
## The following objects are masked from 'package:base': ## ## intersect, setdiff, setequal, union
## For news about 'photobiology', please, see http://www.r4photobiology.info/
## For on-line documentation see http://docs.r4photobiology.info/photobiology/
## For news about 'photobiologyWavebands', please, see http://www.r4photobiology.info/
## For on-line documentation see http://docs.r4photobiology.info/photobiologyWavebands/
## For news about 'ggspectra', please, see https://www.r4photobiology.info/
## For on-line documentation see https://docs.r4photobiology.info/ggspectra/
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
plot()methods. These go much further than the
plot() method defined in base-R for numeric data, as these classes store metadata that allows the automatic construction of axis labels as quantities and units are well defined.
Here we use the same example
source_spct object from package
photobiology used in the User Guide 1. In contrast to the examples in the User Guide 1, here we obtain with a very simple statement a complete annotated plot of the solar spectrum at ground level.
We can change the basis of expression of spectral irradiance and irradiance from
"photon". Be aware that by design, all values, summaries and spectral data will always use the same base of expression.
If a single object contains multiple spectra in long form, they are all plotted. In addition no summary values are displayed as they would overlap.
Although the defaults provide a useful plot, the
plot() methods accepts arguments for several parameters that allow to tweak the looks and contents of the plots without explicit manipulation of the spectral data.
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. The second one,
"contribution" displays in labels the integral of each waveband divided by the integral of the whole spectrum. 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. Using
"contribution.pc" results in the corresponding values being expressed as percentages.
Which annotations are included can be controlled through parameter
annotations. These are passed as a vector of character strings, with three values with special meaning for the first element (with index = 1):
"=" means use only the annotations that follow in the vector,
"+" means add to the default the annotations that follow, and
"-" means remove the annotations that follow from the defaults. A
NULL value means use defaults as is,
"" means no annotations, and
"reserve.space" means no annotations, but expand axis limits and set identity scales ready for manually adding annotations. Which annotations are added by default can be changed by setting R option
"photobiology.plot.annotations" to a character vector with the names of the desired default annotations.
|annotation||default for classes||overrides|
Titles come in different flavours. Package ‘ggplot2’ now supports both titles and subtitles. The
"title" as argument to parameter
plot() methods and of function
ggtitle_spct() takes optional modifiers that can be used to specify the contents of the automatic title and subtitle. Currently supported modifiers are shown in the table below. If the metadata item is not stored in the plotted 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 or no subtitle|
"title:objt", the default for
"title", adds a title with the name of the object being plotted.
where use these three attributes if available. For example
"title:what:when" will use thewhat.measured
attribute for the title and thewhen.measured` attribute for the subtitle.
We can add an annotation to the defaults.
In this case
"boundaries", an annotation that adds a horizontal dashed line and does not override any other annotation.
We can list all the annotations to be included in a plot, in which case
"=" is optional so as to maintain compatibility with earlier versions.
As indicated in the table above, some annotations override other annotations which fulfil a similar role. Here
"boxes", 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 found.
"valleys". They use the repulsive geometry
geom_label_repel from package ‘ggrepel’.
"" as argument to
annotations results in a plot with no annotations, and no scales set.
"reserve.space" as argument to
annotations results in a plot with no annotations, but scales set and expanded so as to receive annotations.
The size of the font used for the annotations is controlled by argument
ylim allows to mannually set the limits of the \(y\) axis using the same syntax as in package ‘ggplot2’. The annotations are still automatically possitioned, and the range extended to make space for them. In other words the values passed to
ylim still give the “space” available for plotting data.
w.band play very different roles. The first one determines the
range of wavelengths to include in the data plotted, which is slightly different to the effect of
range is used to trim the spectral data before passing it to
ggplot, using interpolation when needed (see
photobiology::trim_wl()). The second one,
w.band is only used for the annotations and decorations. It should be however noted, that a
waveband object is a valid argument for both
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 passed to
The argument passed to
range can be a
waveband or and spectrum, in which case their wavelength range is used.
The time unit is also stored in the metadata, as demonstrated here. The units in axis labels have changed to the units used in the spectral data.
##  "day"
Even though the
plot() 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
plot() methods for other spectral classes have slight differences. We show some examples for
By default transmittance, reflectance and absorptance are expressed as fractions of one, as in the plot above, but optionally percents can be plotted.
In many cases it is possible to convert on-the-fly the quantity plotted.
We add a guessed spectral reflectance to the data object to allow the estimation of internal absorptance.
If one really needs to, one can add a suitable stat using ‘local’ data. A peak annotation could be added manually.
The examples above use an emission spectrum. All the spectral classes defined in package ‘photobiology’ are supported, however.
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.
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, method
select() from package ‘dplyr’ should be used instead of subscripting (
[ ]) so as to preserve the metadata. In addition renaming of the variable is needed as shown below when a single column is extracted.
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.
## Off-range transmittance values [-0.00999..0.89250] instead of [0..1]
plot() methods are available for collections of spectra. We use in the examples a simple collection of two spectra. The output and acceptable arguments are the same as those in the
plot() methods for multiple spectra in long form described above.
Plot accepting all defaults.
Plot using one panel per spectrum. Setting
idfactor = NA suppresses the default mapping to
Plot as separate plots. This approach is specially useful with heterogeneous generic_mspct objects, although it works with any collection of spectra.
Using colors is slightly more involved as the identity scale is in use for color in annotations. For this same reason no color key in generated automatically. (In the case of more than two spectra it is easier to use
plyr::mapvalues instead of several nested
Plots created with
plot() method are
ggplot objects and can be customized, bearing in mind that any added layers will be plotted on top or existing layers.
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.
plot() method for
waveband objects is also provided.