ggplot2 with stats, geoms, scales 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. The
autoplot() methods are described separately in vignette User Guide: 2 Autoplot Methods and the
ggplot() methods, statistics, and scales in User Guide: 1 Grammar of Graphics.
The new elements can be freely combined with methods and functions defined in packages ‘ggplot2’,
cowplot and other extensions to ‘ggplot2’.
This third part of the User Guide describes how to combine manipulation of spectral data with plotting. This streamlined coding is made possible by the enhancement implemented in ‘ggspectra’ (>= 0.3.5). In addition, some of the examples make use of methods available only in ‘photobiology’ (>= 0.9.30).
In ‘ggspectra’ (>= 0.3.5) the data member of
gg (ggplot) objects remains as an object of the classes for spectral data defined in ‘photobiology’ instead of being converted into a plain
data.frame. This makes it possible data manipulations in layers to be done with methods specific to spectral data.
Create a collection of two source_spct objects.
We bind the two spectra in the collection into a single spectral object. This object includes an indexing factor, by default names
spct.idx. We use this new object to later on demonstrate grouping in ggplots.
We change the default theme.
As from version 0.3.5 the class of the spectral object stored by
ggplot() is no longer stripped, data transformations in layers can use methods from package ‘photobiology’ that expect spectral objects as input. A simple example of spectral data smoothing follows.
Can be now simplified because
. refers to a
data member of class
source_spct while the automatic mapping remains valid because
smooth_spct() returns a new
source_spct object with the same column names as
The easiest way of plotting photon spectral irradiance instead of spectral energy irradiance is to temporarily change the default radiation unit.
Obviously, the default data does not need to be plotted, so this provides a roundabout way of applying methods,
which is equivalent to doing the transformation ahead of plotting, which might be preferable.
However, when using different transformations in different layers we need to apply them at each layer.
Of course, this approach works both with geoms and stats, but one should remember that these layer functions do not “see” the original data object, but instead a new data frame containing the mapped variables in columns named according to aesthetics. The next example demostrates this and illustrates that smoothing displaces the wavelength of maximum spectral irradiance.
ggplot(sun.spct) + geom_line() + stat_peaks(size = 3, span = NULL) + stat_peaks(geom = "vline", linetype = "dotted", span = NULL) + geom_line(data = . %>% smooth_spct(method = "supsmu"), colour = "red", size = 1.2) + stat_peaks(data = . %>% smooth_spct(method = "supsmu"), size = 3, span = NULL) + stat_peaks(data = . %>% smooth_spct(method = "supsmu"), geom = "vline", linetype = "dotted", span = NULL)
We can easily highlight a wavelength region.
In the plot above, spectral irradiance is taken into account when computing the colors, while below, only the wavelength at the centre of each waveband is used.
Some of the methods from ‘photobiology’ are also defined for
data.frame and can be used as summary functions with data that are not radiation spectra, such as any
data seen by layer functions including