User Guide: 1 Grammar of Graphics

‘ggspectra’ 0.3.5

Pedro J. Aphalo



Package ggspectra extends ggplot2 with stats, geoms, scales and annotations suitable for light spectra. It also defines ggplot() and 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 ‘Autoplot Methods’.

The new elements can be freely combined with methods and functions defined in packages ggplot2, scales, ggrepel and cowplot.

What are plot specializations for?

The plot specializations produce fully annotated plots as ggplot objects, which can be further manipulated if so desired. They use the metadata stored in spectral objects of classes defined in package ‘photobiology’ to automatically generate suitable axis labels, scales and annotations. (Please, see vignette “Plot methods” for details.)

What are ggplot specializations for?

The ggplot specializations set default aes according to the type of spectral object. They also add support for unit.out arguments allowing on-the-fly conversion of units of expression or spectral quantities. These are only defaults and can be overridden by explicit use of aes() to set the mapping of aesthetics.

What are the different stats useful for?

The stats defined in this package help with with the generation of annotations and decorations of plots of spectral data. They are meant to be used only when the x aesthetic is mapped to a variable containing wavelength values expressed in nanometres. They are designed to work with spectral objects of the classes defined in package ‘photobiology’. Many of them also work well with any data frame as long as the x aesthetic mapping fulfils the expectations. Package ‘ggpmisc’ contains some equivalent stats which do not assume that x is mapped to wavelength, accepting numeric and datetime values.

stat default geom (used for) other uses
stat_peaks point (highlight maxima) wavelength label, spectral quantity label
stat_valleys point (highlight minima) wavelength label, spectral quantity label
stat_label_peaks text (wavelength label) spectral quantity label (support ‘ggrepel’)
stat_label_valleys text (wavelength label) spectral quantity label (support ‘ggrepel’)
stat_find_wls point (highlight wls at qty) wavelength label, spectral quantity label
stat_find_qtys point (highlight qty at wl) wavelength label, spectral quantity label
stat_color color, fill
stat_wb_label text (waveband name) rect (showing range of waveband and its color)
stat_wb_total text (y integral) label(s) with waveband integral
stat_wb_mean text (y mean) label(s) with waveband mean
stat_wl_summary text (y mean) label with wavelength range mean
stat_wb_contribution text (contribution) label(s) with waveband integral / whole spectrum integral
stat_wb_relative text (relative) label(s) with waveband integral / sum of integrals of all wavebands
stat_wb_e_irrad text (energy irradiance) rect (showing range of waveband and its color)
stat_wb_q_irrad text (photon irradiance) rect (showing range of waveband and its color)
stat_wb_e_sirrad text (spectral energy irradiance) rect (showing range of waveband and its color)
stat_wb_q_sirrad text (spectral photon irradiance) rect (showing range of waveband and its color)
stat_wl_strip rect (fill of wavelength or waveband) text (label with waveband name)
stat_wb_box rect (fill of waveband)
stat_wb_hbar errorbarh (color of waveband)
stat_wb_column rect (fill of waveband)

What is geom_spct useful for?

The geom_spct geometry is a special case of geom_area, but with the minimum of the y range fixed to 0, but with stacking not enabled.

What are the new scales for?

The new scales are convenience wrapper functions built on top of the scales exported by package ‘ggplot2’, but with default arguments that are suitable for spectral data.

Functions for automatic generation of secondary x-axes in the case when a variable containing wavelength data (nm) is mapped to the x aesthetic simplify the task of adding an axis with frequencies or wave numbers to the plot of a spectrum.

scale unit.exponent name labels breaks
scale_y_cps_continuous 0 cps_label() SI_pl_format()
scale_y_counts_continuous 3 counts_label() SI_pl_format()
scale_y_counts_tg_continuous 3 counts_label() SI_tg_format()
scale_y_A_internal_continuous 0 A_internal_label() SI_pl_format()
scale_y_A_total_continuous 0 A_total_label() SI_pl_format()
scale_y_Tfr_internal_continuous 0 Tfr_internal_label() SI_pl_format()
scale_y_Tfr_total_continuous 0 Tfr_total_label() SI_pl_format()
scale_y_Rfr_internal_continuous 0 Rfr_internal_label() SI_pl_format()
scale_y_Rfr_total_continuous 0 Rfr_total_label() SI_pl_format()
scale_y_s.e.irrad_continuous 0 s.e.irrad_label() SI_pl_format()
scale_y_s.q.irrad_continuous -6 s.q.irrad_label() SI_pl_format()
scale_y_s.e.response_continuous 0 s.e.response_label() SI_pl_format()
scale_y_s.q.response_continuous 0 s.q.response_label() SI_pl_format()
scale_x_wl_continuous -9 w_length_label() SI_pl_format() pretty_breaks(n=7)

In addition secondary axis definitions, sec_axis_w_number() and sec_axis_w_frequency(), and SI system formatters SI_pl_format, and SI_tg_format are exported, together with auxiliary functions for finding the nearest SI multiplier based on an arbitrary exponent.

What are functions color_chart() and black_or_white() for?

Function color_chart() makes a color chart of rectangular tiles from a vector R color definitions. The chart returned is a ggplot object. Function black_or_white() accepts a vector of color definitions and returns a vector with colors "white" or "black" depending on the approximate luminosity of each color in the input. The main use is to automatically achieve suitable contrast between text plotted on top of a color background.

What is function autotitle() for?

Function autotitle() adds a title, subtitle and/or a caption to a plot. The difference with ggtitle() from package ‘ggplot2’ is that autotitle() automatically retrieves metadata from an spectral object based on keys. It is used internally by all autoplot() methods defined in package ‘ggspectra’ and allowed syntax and key values are described in User Guide 2: Plot Methods together with plot annotations.

Set up

## 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
## Loading required package: tibble
## News at

good_label_repel <- packageVersion('ggrepel') != "0.8.0" #||
#  packageVersion('ggplot2') >= "3.1.0"

Create a collection of two source_spct objects.

two_suns.mspct <- source_mspct(list(sun1 = sun.spct, sun2 = sun.spct / 2))

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.

two_suns.spct <- rbindspct(two_suns.mspct)

We change the default theme.


ggplot() methods for spectra

The only difference between these specializations and the base ggplot() method is that the aesthetics for \(x\) and \(y\) have suitable defaults. These are just defaults, so if needed they can still be supplied with a mapping argument with an user-defined aes().

ggplot(sun.spct) + geom_line()

It is possible to add to the defaults by means of + and aes() as shown below.

ggplot(two_suns.spct) + aes(color = spct.idx) + geom_line()

If a mapping is supplied directly through ggplot, \(x\) and \(y\) should be included.

ggplot(two_suns.spct, aes(w.length, s.e.irrad, color = spct.idx)) + geom_line()

In the case of ggplot.source_spct() an additional parameter allows setting the type of units to use in the plot. This not only sets a suitable aes() for \(y\) but also if needed converts the spectral data. The two possible values are "energy" and "photon" and the default, depends on option photobiology.radiation.unit. This parameter has a default value that can be modified through option "photobiology.radiation.unit". Package ‘photobiology’ defines convenience functions for this.

ggplot(sun.spct, unit.out = "photon") + geom_line()

After evaluation of photon_as_default(), a new default is in effect, but we can override it with an explicit argument if needed.

ggplot(sun.spct) + geom_line()
ggplot(sun.spct, unit.out = "energy") + geom_line()

This new default will remain active for the rest of the R session, unless changed. We can easily either unset this default, or all photobiology-package related user set defaults.


The next example is for spectral properties of filters.

ggplot(yellow_gel.spct) + geom_line()

In the case of ggplot.filter_spct() the additional parameter is called plot.qty and allows choosing between "transmittance" and "absorbance".

ggplot(yellow_gel.spct, plot.qty = "absorbance") + geom_line()

In the case of ggplot.object_spct() three values ("reflectance", "transmittance" and "all" are accepted. Passing "all" as argument results in the spectral data being molten into a long form, using value and variable as value and key columns, respectively. The column variable has three levels Tfr, Rfr and Afr indexing the Tfr and Rfr from the object_spct object plus newly calculated absorptance values.

This parameter has a default value that can be modified through option "photobiology.filter.qty". Package ‘photobiology’ defines convenience functions for this.

ggplot(yellow_gel.spct) + geom_line()
ggplot(polyester.spct) + geom_line()

The names of the additional parameters are consistent with those used in the autoplot() methods defined in this package.

ggplot() methods for collections fof spectra

The ggplot() methods for collections of spectra work similarly to the methods for spectra when used with an spectral object containing concatenated spectra, as that shown in the previous section.

Plotting a collection of spectra using an aesthetic.

ggplot(two_suns.mspct) + 
  aes(linetype = spct.idx) +
  wl_guide(ymax = -0.05) +

Using facets.

ggplot(two_suns.mspct) + 
  wl_guide(ymax = -0.05) +
  geom_spct() +
  geom_line() +
  facet_wrap(~spct.idx, ncol = 1L)

ggplot(two_suns.mspct) + 
  wl_guide(ymax = -0.05) +
  geom_spct() +
  geom_line() +
  facet_wrap(~spct.idx, ncol = 1L, scales = "free_y")

## Scales


Currently only an x-scale function suitable for wavelengths in nanometres is defined in package ‘ggspectra’.

Functions for automating the addition of secondary axes are also defined.

It is possible to set a different SI scaling factor for the units.

It is possible to set a non-SI scaling factor for the units.

It is possible find the largest valid SI-scaling factor that is smaller or equal to and arbitrary power of 10.

## [1] 12

Raw counts

Counts per second

Spectral irradiance

We can change the SI unit multipliers on-the-fly. Beware that these are not data transformations, only the labelling of the axes changes! Consequently summary values will not be affected. We will express the spectral irradiance using milli watts instead of watts and wavelengths in micrometres instead of nanometres.

The scales are just wrappers on the scales defined in packages ‘ggplot2’ and ‘scales’ with defaults suitable for spectral data. They pass additional arguments to the underlying scale functions allowing, for example, the control of the formatting of tick labels.

In addition to formatters defined in package ‘scales’ as used above, additional formatters are defined in package ‘ggspectra’, like those used in the next example.

Transformation objects from package ‘scales’, or user defined, can be also passed as arguments. We replace zeros to avoid a warnings from the log10() call, and as the scale limits discards some observations, we use na.rm = TRUE to silence this additional warning.



### Absorbance



Several ggplot stats are defined by this package. All of them target light spectra, as they expect \(x\) to represent wavelengths expressed in nanometres. However, they should behave correctly as long as this is true, with any ggplot object, based on any data format acceptable to ggplot. The name of the original variable is irrelevant, and it is the user responsibility to supply the correct variables through aes(). Of course, when using the spectral classes defined in package photobiology the defaults easy this task.

Peaks, valleys and target values

Four stats are available for peaks and valleys, with the same formal parameters. These stats do not fit peaks, simply search for local maxima and local minima in the data as supplied. Stats stat_peaks() and stat_valleys() subset the original data while stat_label_peaks() and stat_label_valleys() only set a boolean flag to mark the local extremes. Two stats are available for highlighting arbitrary locations in spectra, one of them, stat_find_wls() accepts a target for the spectral quantity and locates the corresponding wavelength values while the other, stat_find_qtys() accepts a target wavelength value and locates the corresponding spectral quantity value. Stats stat_find_wls() and stat_find_qtys() subset the original data or generate new data by interpolation.

All six stats set the same default aesthetics based on calculated values. Not all of these default aesthetics are used by the default geom, but they make using other geoms easier. Furthermore generated text labels are formatted with sptintf() and these six stats accept format definitions through parameters label.fmt, x.label.fmt, and y.label.fmt. Please see the documentation for a list of all the computed varaibles returned in data. These stats use internally functions photobiology::find_peaks() and photobiology::find_wls() and arguments are passed down to them.

The examples that follow, apply with minimal changes to stat_peaks(), stat_valleys(), stat_find_wls() and stat_find_qtys().

Because of the conversion, the location of maxima and minima when an irradiance or response spectrum is expressed in photon- vs. energy-based units may differ. This is expected and not a bug.

The complement to stat_peaks() is stat_valleys.

Stats stat_find_wls() and stat_find_qtys() are another complementary pair. The default target, used in this example, is the half maximum.

One of the values calculated and mapped is colour as seen by humans corresponding to the wavelength at the location of peaks and valleys. The colour is mapped to the fill aesthetic, so using a ‘filled’ shape results in a colourful plot. The identity scale is needed so that the correct colours are displayed using the colour definitions in the calculated data instead of a palette.

We can use any of the aesthetics affecting the default geom, "point", and also with other _geom_s.

We can use several other geoms as needed, demonstrated here with geom "text".

The same stat can be included more than once in the same plot, using different geoms. We here in addition demonstrate the use of several different parameters. The span argument determines the number of consecutive observations tested when searching for a local extreme, and it should be an odd integer number. In addition we here demonstrate the use of a geom new to ggplot2 2.0.0 called "label" which again results in colourful labels by default. Here we make use of the computed variable BW.color which is set to "white" or "white" for maximum contrast with the computed variable fill.

To avoid overlap between labels we can use the repulsive geoms from package ‘ggrepel’. In this case we need to use stat_label_peaks(). Repulsion allows the use of larger labels. We also need to expand the \(y\) limits to make space for the labels. Here we also demonstrate the arguments passed to the geom for a nice display.

If we do not use a repulsive geom that skips labels set to "", the plot produced by stat_label_peaks is of little use unless we explicitly skip the unlabelled observations as shown below.

Setting span to NULL results in the span set to the range of the data, and so in this case the stat returns the global extreme, instead of a local one. In this case we use geoms "vline" and "hline" taking advantage that suitable aesthetics are set by the stats.

By default the label aesthetic is mapped to a calculated label x.label giving the wavelength in nanometres. This mapping can be changed to give to a label giving the y-value at the peak.

As aesthetics can use values computed on-the-fly we can even use paste() to map a label that combines both values and adds additional text.

Finally we demonstrate that both stats can be simultaneously used. One can also choose to use different spans as demonstrated here, resulting in more maxima being marked by points than labelled with text.

This final example shows a few additional tricks used in this case to mark and label the maximum of the spectrum. This example also demonstrates why it is important that these are stats. The peaks are searched and labels generated once for each group, in this case each facet.

Color from wavelength

This stat calculates the colour corresponding to each \(x\)-value (assumed expressed in nanometres) and adds it to the data. It does not summarize the data like stat_summary() nor does it subset the data like stat_peaks, consequently the plot does not require any additional geom to have all observations plotted. It sets both color and fill aesthetics to a suitable default.

ggplot(sun.spct) + 
  stat_color() + scale_color_identity()

By use of a filled shape and adding a black border by overriding the default color aesthetic and over-plotting these point on top of a line, we obtain a better separation from the background.

ggplot(sun.spct) +
  geom_line() +
  stat_color(shape = 21, color = "black") + 

With a trick using many narrow bars we can fill the area under the line with a the calculated colours. This works satisfactorily as the data set has a small wavelength step, as in this case we are using a bar of uniform colour for each wavelength value in the data set.

ggplot(sun.spct) + 
  stat_color(geom = "bar") + 
  geom_point(shape = 21, color = "black", stroke = 1.2, fill = "white") +
  scale_fill_identity() + 
  scale_color_identity() + 

As final example we demonstrate a plot with facets and shape based groups.

ggplot(two_suns.spct) + aes(shape = spct.idx) +
  stat_color() + scale_color_identity() +
  geom_line() + 
  facet_grid(spct.idx~., scales = "free_y")

Averages and similar summaries

Our summary statistics are quite different to ggplot2s stat_summary(). One could criticize that they calculates summaries using a grouping that is not based on a ggplot aesthetic. This is a deviation from the grammar of graphics but allows the calculation of summaries for an arbitrary region of the range of \(x\)-values in the spectral data.

Summaries producing graphical elements

Three statistics generate only graphic output. First we demonstrate stat_wb_box() that produces a filled box for each waveband, filled with the color corresponding to the waveband.

The statistics stat_wb_column outputs a column for each waveband, with an area equal to the integral for the corresponding region of the spectrum.

The statistic stat_wd_hbar outputs a horizontal bar showing the mean spectral y-value for each waveband.

These graphical summaries are frequently used together with text and label elements with names or numerical summaries.

Name labels

Summaries by wavelength range

The summary is calculated for a range of wavelengths, and the range argument defaults to the range of wavelengths in each group defined by other aesthetics. The default geom is "text".

We can optionally supply an argument for range to limit the summary to a certain part of the spectrum, in which case the use of the default "text" geom is misleading. We add the line last so that it is drawn on top of the rectangle.

Here we show how to add horizontal line and label for the overall mean, by adding the same stat twice, using different values for geom.

Or we can add the aesthetic twice with two different geoms to get the value plotted as a rectangular area and the value as formatted text. We use vjust to move the text above the end of the bar, instead of it being centred on the value itself.

An example using the color aesthetic for grouping and moving the text label down. Setting

Same example as above but using a free scale for \(y\), still working as expected.

Means by waveband

Our stat_wb_mean() is quite different to ggplot2s stat_summary(). One could criticize that it calculates summaries using a grouping that is not based on a ggplot aesthetic. This is a deviation from the grammar of graphics but allows the calculation of summaries for an arbitrary waveband of the spectrum based on photobiology::waveband objects, or lists of such objects. In contrast to stat_wl_summary() this allows the use of several ranges, and also of different weighting functions. This function returns both mean and total values for each waveband. It differs from stat_wb_total() only in the default aesthetics set.

The first example uses a waveband object created on-the-fly and defining a range of wavelengths.

If a numeric vector or a spectrum is supplied as argument to waveband, its range is calculated and used to construct a temporary waveband object.

Lists of wavebands, either user-defined or as in this case using a list constructor can be also used.

Totals by waveband

Our stat_wb_total() is quite different to ggplot2s stat_summary(). One could criticize that it calculates summaries using a grouping that is not based on a ggplot aesthetic. This is a deviation from the grammar of graphics but allows the calculation of summaries for an arbitrary waveband of the spectrum based on photobiology::waveband objects, or lists of such objects. In contrast to stat_wl_summary() this allows the use of several ranges, and also of different weighting functions. This function returns both mean and total values for each waveband. It differs from stat_wb_mean() only in the default aesthetics set.

The first example uses a waveband object created on-the-fly and defining a range of wavelengths.

If a numeric vector or a spectrum is supplied as argument to waveband, its range is calculated and used to construct a temporary waveband object.

Lists of wavebands, either user-defined or as in this case using constructor defined in package photobiologyWavebands can be also used. In the case of totals, areas represent them graphically in a very useful way.

Irradiances by waveband

The _stat_s in previous sections can be used for non-weighed irradiances, but not for BSWFs. These irrad _stat_s have additional formal parameters for passing metadata, and use method photobiology::irrad() internally on a photobiology::source_spct object constructed from the data for \(x\) and \(y\) aesthetics. Function stat_wb_sirrad() is equivalent to stat_wb_mean() and function stat_wb_irrad() is equivalent to stat_wb_total() following the same naming convention as in package photobiology.

Also following the same naming convention as in package photobiology, e and q versions of these functions default to energy and photon based quantities to "seconds" for time unit.

We can also use waveband objects describing biological spectral weighting functions (BSWFs), such as CIE’s erythema function.

For daily data we need to override the default time unit.

Here we pass a list of waveband objects. And add areas to facilitate comparisons as irradiances are integral over wavelengths.

Spectral irradiances by waveband

Spectral irradiances are equivalent to means, and can be best represented graphically as horizontal bars.

Irradiances by waveband

Labels from wavebands

Sometimes we may want to only annotate a plot with waveband names and regions without calculating any summary quantity. stat_wb_label() fills this role. Although the same result can be achieved with some of the summary statistics, if no summary is needed this statistic can avoid unnecessary computations, and more importantly used in plots where the \(y\) is not mapped, as only the \(x\) aesthetic is required.

ggplot(data.frame(w.length = 300:800), aes(w.length)) +
  stat_wl_strip( = VIS_bands(), ymax = Inf, ymin = -Inf) +
  stat_wb_label( = VIS_bands(), angle = 90) +
  scale_fill_identity() + 
  scale_color_identity() + 
  scale_y_continuous(labels = NULL) +
  scale_x_continuous(breaks = seq(from = 300, to = 800, by = 25)) +
  labs(x = "Wavelength (nm)", title = "Colours according to ISO standard") +

We can also in a similar way make a plot comparing different sets of wavebands, in the next example we compare the VIS and NIR bands of the different imagers used in Landsat missions 1 to 8.

ggplot(data.frame(w.length = 300:1100), aes(w.length)) +
  stat_wl_strip( = RBV_bands(), ymax = 1, ymin = 3) +
  stat_wb_label( = RBV_bands(), ypos.fixed = 2, angle = 90, vjust = 0.3, size = 3) +
  stat_wl_strip( = MSS_bands(), ymax = 4, ymin = 6, na.rm = TRUE) +
  stat_wb_label( = MSS_bands(), ypos.fixed = 5, angle = 90, vjust = 0.3, size = 3) +
  stat_wl_strip( = ETM_bands(), ymax = 7, ymin = 9, na.rm = TRUE) +
  stat_wb_label( = ETM_bands(), ypos.fixed = 8, angle = 90, vjust = 0.3, size = 3) +
  stat_wl_strip( = OLI_bands(), ymax = 10, ymin = 12, na.rm = TRUE) +
  stat_wb_label( = OLI_bands(), ypos.fixed = 11, angle = 90, vjust = 0.3, size = 3) +
  scale_fill_identity() + 
  scale_color_identity() + 
  scale_y_continuous(labels = c("RBV", "MSS", "TM/ETM", "OLI"), 
                     breaks = c(2,5,8,11),
                     limits = c(0, 13),
                     name = "Imager",
                     sec.axis = dup_axis(labels = c("L1-L2", "L1-L5", "L4-L7", "L8"), name = "Landsat mission")) +
  scale_x_continuous(breaks = seq(from = 300, to = 1200, by = 100),
                     limits = c(400, 1100),
                     sec.axis = dup_axis()) +
  labs(x = "Wavelength (nm)", title = "Landsat imagers: VIS and NIR bands") +

The different definitions of the ultraviolet regions.

ggplot(data.frame(w.length = 100:400), aes(w.length)) +
  stat_wl_strip( = UV_bands("ISO"), ymax = 1, ymin = 3, color = "white") +
  stat_wb_label( = UV_bands("ISO"), ypos.fixed = 2, size = 3) +
  stat_wl_strip( = UV_bands("CIE"), ymax = 4, ymin = 6, color = "white") +
  stat_wb_label( = UV_bands("CIE"), ypos.fixed = 5, size = 3) +
  stat_wl_strip( = UV_bands("plants"), ymax = 7, ymin = 9, color = "white") +
  stat_wb_label( = UV_bands("plants"), ypos.fixed = 8, size = 3) +  
  stat_wl_strip( = UV_bands("none"), ymax = 10, ymin = 12, color = "white") +
  stat_wb_label( = UV_bands("none"), ypos.fixed = 11, size = 3) +
  stat_wl_strip( = UV_bands("medical"), ymax = 13, ymin = 15, color = "white") +
  stat_wb_label( = UV_bands("medical"), ypos.fixed = 14, size = 3) +  

  scale_fill_identity() + 
  scale_color_identity() + 
  scale_y_continuous(labels = c("ISO", "CIE", "plants", "none", "medical"), 
                     breaks = c(2,5,8,11,14),
                     limits = c(0, 16),
                     name = "Definition",
                     sec.axis = dup_axis(labels = 
                      c("use", "use", "use?", "avoid!", "avoid!"), name = "Recommendation")) +
  scale_x_continuous(breaks = c(seq(from = 100, to = 400, by = 50), 280, 315),
                     limits = c(100, 400),
                     sec.axis = 
                       dup_axis(breaks = 
                                c(100, 150, 200, 220, 250, 290, 320, 340, 400))) +
  labs(x = "Wavelength (nm)", title = "UV bands",
       subtitle = "According to ISO standard, CIE recommendations, and non-standard use") +

It should be noted that this approach is rather different from the way one would normally plot a horizontal bar plot with the grammar of graphics of ‘ggplot2’, but for plottings existing waveband definitions it is convenient and ensures that the waveband definitions agree with those used for computing summary quantities elsewhere.

Wavelength colours at new x-values

The stat_wl_summary() and stat_wb_mean() return a reduced data set with fewer rows than the original data. stat_wl_strip() depending on the input, can also return a data set with more rows than the input data. This is the default behaviour, with with argument NULL. This stat operates only on the variable mapped to the x aesthetic, a y-mapping is not required as input. <- data.frame(x = 300:800)
ggplot(, aes(x)) + stat_wl_strip(ymin = -1, ymax = 1, color = NA) +

The default geom is "rect" which with suitable mappings of ymin and ymax aesthetics can be used to add a colour guide to the x-axis (if its scale maps wavelengths in nanometres). When is NULL a long series of contiguous wavebands is generated to create the illusion of a continuous colour gradient.

ggplot(sun.spct) +
  geom_line() +
  stat_wl_strip(ymin = -Inf, ymax = -0.025) + 
  scale_fill_identity() +

When a list of wavebands is supplied, it is used for the calculation of colours. These calculations do not use the spectral irradiance, they simply assume a flat spectrum, so the represent the colours of the wavelengths, rather than the colour of the light described by the spectral irradiance.

ggplot(sun.spct) +
  geom_line() + 
  stat_wl_strip( = VIS_bands(), ymin = -Inf, ymax = -0.025) + 
  scale_fill_identity() +

stat_wl_strip() can also used to produce a background layer with colours corresponding to wavebands. Here we use alpha = 0.5 to add transparency.

ggplot(sun.spct) +
  stat_wl_strip( = VIS_bands(), ymin = -Inf, ymax = Inf, alpha = 0.4) + 
  scale_fill_identity() +
  geom_line() +

By default an almost continuous colour gradient can be generated for the background.

ggplot(sun.spct) +
  stat_wl_strip(alpha = 0.4, ymin = -Inf, ymax = Inf) + 
  scale_fill_identity() +
  geom_line() +