ggspectra
0.1.12Package ggspectra
extends ggplot2
with stats, geoms and annotations suitable for light spectra. It also defines ggplot()
and plot()
methods specialized for the classes defined in package photobiology
for storing different types of spectral data. The plot()
methods are described separately in vignette ‘Plot Methods’.
The new elements can be freely combined with methods and functions defined in packages ggplot2
, scales
, ggrepel
and cowplot
.
plot
especializations 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.)
ggplot
especializations 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.
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_color |
color, fill | |
stat_wb_label |
text (waveband name) | rect (showing range of waveband and its color) |
stat_wb_total |
rect (above data) | label(s) with waveband integral |
stat_wb_mean |
rect (showing integral) | label(s) with waveband mean |
stat_wb_summary |
rect (showing integral) | label with range mean |
stat_wb_contribution |
rect (above data) | label(s) with waveband integral / whole spectrum integral |
stat_wb_relative |
rect (above data) | 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 (color of wavelength or waveband) | text (label with waveband name) |
library(ggplot2)
library(photobiology)
library(photobiologyWavebands)
library(ggspectra)
library(ggrepel)
We bind two spectra, to later on demonstrate grouping.
two_suns.spct <- rbindspct(list(sun1 = sun.spct, sun2 = sun.spct * 2))
We change the default theme.
theme_set(theme_bw())
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 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
.
ggplot(sun.spct, unit.out = "photon") + geom_line()
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()
The names of the additional parameters are consistent with those by method plot()
in this package and in the now deprecated package photobiologygg
.
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.
Four stats are available, they all have 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. All these stats set several aesthetics based on calculated values. Not all of these aesthetics are used by the default geom, but they make using other geoms easy. Furthermore text labels are formatted with sptintf()
and these four stats accept format definitions through parameters label.fmt
, x.label.fmt
, and y.label.fmt
. Both the \(x\) and \(y\) values at the maxima or minima are returned or flagged. The stats use internally function photobiology::find_peaks()
and arguments are passed down to it.
ggplot(sun.spct) + geom_line() + stat_peaks()
Because of the conversion, the location of maxima and minima when an spectrum is expressed in photon and energy based units may differ. This is expected and not a bug.
ggplot(sun.spct, unit.out = "photon") + geom_line() + stat_peaks()
The complement to stat_peaks()
is stat_valleys
.
ggplot(sun.spct) + geom_line() + stat_valleys()
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.
ggplot(sun.spct) + geom_line() +
stat_peaks(shape = 21) + scale_fill_identity()
We can use any of the aesthetics affecting the default geom, "point"
, and also with other _geom_s.
ggplot(sun.spct) + geom_line() +
stat_peaks(span = 21, shape = 4, color = "red", size = 2) +
stat_peaks(span = 21, color = "red", geom = "rug", sides = "b")
We can use several other geoms as needed, demonstrated here with geom "text"
.
ggplot(sun.spct) + geom_line() +
stat_peaks(span = 21, 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.
ggplot(sun.spct) + geom_line() +
stat_peaks(shape = 21, span = 25, size = 2) + scale_fill_identity() +
stat_peaks(geom = "label", span = 25, vjust = "bottom", size = 3,
color = "white")
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.
ggplot(sun.spct) + geom_line() +
stat_peaks(shape = 21, span = 25, size = 2) + scale_fill_identity() +
stat_label_peaks(geom = "label_repel", span = 25, size = 3.5, nudge_y = 0.075,
color = "white") +
stat_valleys(shape = 21, span = 25, size = 2) + scale_fill_identity() +
stat_label_valleys(geom = "label_repel", span = 25, size = 3.5, nudge_y = -0.075,
color = "white") +
expand_limits(y = c(-0.08, 0.9))
## Scale for 'fill' is already present. Adding another scale for 'fill',
## which will replace the existing scale.
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.
ggplot(sun.spct) + geom_line() +
stat_peaks(shape = 21, span = 25, size = 2) + scale_fill_identity() +
stat_label_peaks(geom = "label", span = 25, vjust = "bottom", size = 3, color = "white",
aes(label = ifelse(..is_peak.., ..x.label.., NA)), na.rm = TRUE) +
expand_limits(y = c(NA, 0.9))
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.
ggplot(sun.spct) + geom_line() +
stat_peaks(span = NULL, geom = "vline", linetype = "dotted") +
stat_peaks(span = NULL, geom = "hline", linetype = "dotted")
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.
ggplot(sun.spct) + geom_line() +
stat_peaks(span = 21, geom = "text", aes(label = ..y.label..))
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.
ggplot(sun.spct) + geom_line() +
stat_peaks(span = NULL) +
stat_peaks(span = NULL, geom = "text", vjust = -0.5,
aes(label = paste(..y.label.., "at", ..x.label.. , "nm"))) +
expand_limits(y = c(NA, 0.9))
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.
ggplot(sun.spct) + geom_line() +
stat_peaks(span = 21, geom = "point", colour = "red") +
stat_valleys(span = 21, geom = "point", colour = "blue") +
stat_peaks(span = 51, geom = "text", colour = "red",
vjust = -0.3, label.fmt = "%3.0f nm") +
stat_valleys(span = 51, geom = "text", colour = "blue",
vjust = 1.2, label.fmt = "%3.0f nm")
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.
ggplot(two_suns.spct) + aes(color = spct.idx) +
geom_line() + ylim(NA, 1.8) +
stat_peaks(span = NULL, color = "black") +
stat_peaks(span = NULL, geom = "text", vjust = -0.5, size = 3,
color = "black",
aes(label = paste(..y.label.., "at", ..x.label.. , "nm"))) +
facet_grid(spct.idx~.)
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") +
scale_fill_identity()
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() +
theme_bw()
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")
Our stat_wl_summary()
is quite different to ggplot2
s 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 region of the range of \(x\)-values in the spectral data.
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"
.
ggplot(sun.spct) + geom_line() + stat_wl_summary()
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.
ggplot(sun.spct) +
stat_wl_summary(range = c(300,350), geom = "rect") +
geom_line()
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
.
ggplot(sun.spct) +
geom_line() +
stat_wl_summary(geom = "hline", color = "red") +
stat_wl_summary(label.fmt = "Mean = %.3g", color = "red", vjust = -0.3)
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.
ggplot(sun.spct) +
stat_wl_summary(range = c(400,500), geom = "rect", alpha = 0.2, fill = color(450)) +
stat_wl_summary(range = c(400,500), label.fmt = "Mean = %.3g", vjust = -0.3, geom = "text") +
geom_line()
An example using the color
aesthetic for grouping and moving the text label down. Setting
ggplot(two_suns.spct) + aes(color = spct.idx) +
geom_line() +
stat_wl_summary(geom = "hline") +
stat_wl_summary(label.fmt = "Mean = %.3g", vjust = 1.2, show.legend = FALSE) +
facet_grid(spct.idx~.)
Same example as above but using a free scale for \(y\), still working as expected.
ggplot(two_suns.spct) + aes(color = spct.idx) +
geom_line() +
stat_wl_summary(geom = "hline") +
stat_wl_summary(label.fmt = "Mean = %.3g", vjust = 1.2, show.legend = FALSE) +
facet_grid(spct.idx~., scales = "free_y")
Our stat_wb_mean()
is quite different to ggplot2
s 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.
ggplot(sun.spct) +
stat_wb_mean(w.band = PAR(), geom = "rect", alpha = 0.5) +
stat_wb_mean(w.band = PAR(), geom = "text") +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
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.
ggplot(sun.spct) +
stat_wb_mean(w.band = c(400,500), geom = "rect", alpha = 0.5) +
stat_wb_mean(w.band = c(400,500), geom = "text") +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
Lists of wavebands, either user-defined or as in this case using constructor defined in package photobiologyWavebands
can be also used.
ggplot(yellow_gel.spct) +
stat_wb_mean(w.band = Plant_bands(), geom = "rect", alpha = 0.7) +
stat_wb_mean(w.band = Plant_bands(), angle = 90, hjust = 0, geom = "text", ypos.fixed = 0.1,
aes(label = paste(..wb.name.., " = ", ..y.label.., sep = ""))) +
geom_line() +
scale_fill_identity() +
theme_bw()
Our stat_wb_total()
is quite different to ggplot2
s 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.
ggplot(sun.spct) +
stat_wb_total(w.band = PAR(), geom = "rect", alpha = 0.5) +
stat_wb_total(w.band = PAR(), geom = "text") +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
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.
ggplot(sun.spct) +
stat_wb_total(w.band = c(400,500), geom = "rect", alpha = 0.5) +
stat_wb_total(w.band = c(400,500), geom = "text") +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
Lists of wavebands, either user-defined or as in this case using constructor defined in package photobiologyWavebands
can be also used.
ggplot(yellow_gel.spct) +
stat_wb_total(w.band = Plant_bands(), geom = "rect", alpha = 0.4, color = "white",
ypos.fixed = 1) +
stat_wb_mean(w.band = Plant_bands(), geom = "text", label.fmt = "%1.2f",
ypos.fixed = 1) +
geom_line() +
scale_fill_identity() +
theme_bw()
The _stat_s in previous sections can be used for unweighted 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
.
ggplot(sun.spct) +
stat_wb_irrad(w.band = PAR(), geom = "rect", alpha = 0.5,
unit.in = "energy", time.unit = "second") +
stat_wb_irrad(w.band = PAR(), geom = "text",
unit.in = "energy", time.unit = "second") +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
ggplot(sun.spct, unit.out = "photon") +
stat_wb_irrad(w.band = PAR(), geom = "rect", alpha = 0.5,
unit.in = "photon", time.unit = "second") +
stat_wb_irrad(w.band = PAR(), geom = "text",
unit.in = "photon", time.unit = "second",
aes(label = sprintf("%s = %.3g", ..wb.name.., ..yint.. * 1e6))) +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
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.
ggplot(sun.spct) +
stat_wb_e_irrad(w.band = PAR(), geom = "rect", alpha = 0.5) +
stat_wb_e_irrad(w.band = PAR(), geom = "text") +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
We can also use waveband
objects describing biological spectral weighting functions (BSWFs), such as CIE’s erythema function.
ggplot(sun.spct) +
stat_wb_e_irrad(w.band = CIE(), geom = "rect", alpha = 0.5) +
stat_wb_e_irrad(w.band = CIE(), geom = "text") +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
For daily data we need to override the default time unit.
ggplot(sun.daily.spct) +
stat_wb_e_irrad(w.band = CIE(), geom = "rect", alpha = 0.5,
time.unit = "day") +
stat_wb_e_irrad(w.band = CIE(), geom = "text",
time.unit = "day", label.mult = 1e-3) +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
Here we pass a list of waveband
objects.
ggplot(sun.spct, unit.out = "photon") +
stat_wb_q_irrad(w.band = VIS_bands(), geom = "rect", alpha = 0.5) +
stat_wb_q_irrad(w.band = VIS_bands(), geom = "text",
label.mult = 1e6, size = rel(2)) +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
ggplot(sun.spct) +
stat_wb_e_sirrad(w.band = PAR(), geom = "rect", alpha = 0.5) +
stat_wb_e_sirrad(w.band = PAR(), geom = "text") +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
ggplot(sun.spct, unit.out = "photon") +
stat_wb_q_sirrad(w.band = PAR(), geom = "rect", alpha = 0.5) +
stat_wb_q_sirrad(w.band = PAR(), geom = "text",
aes(label = sprintf("Total %s = %.3g", ..wb.name.., ..yint.. * 1e6))) +
stat_wb_q_sirrad(w.band = PAR(), geom = "text",
mapping = aes(label = sprintf("Mean %s = %.3g", ..wb.name.., ..ymean.. * 1e6)),
ypos.mult = 0.45) +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
ggplot(sun.spct) +
stat_wb_e_sirrad(w.band = CIE(), geom = "rect", alpha = 0.5) +
stat_wb_e_sirrad(w.band = CIE(), geom = "text") +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
ggplot(sun.daily.spct) +
stat_wb_e_sirrad(w.band = CIE(), geom = "rect", alpha = 0.5,
time.unit = "day") +
stat_wb_e_sirrad(w.band = CIE(), geom = "text",
time.unit = "day", label.mult = 1e-3) +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
ggplot(sun.spct) +
stat_wb_e_sirrad(w.band = VIS_bands(), geom = "rect", alpha = 0.5) +
stat_wb_e_sirrad(w.band = VIS_bands(), angle = 90, geom = "text",
ypos.fixed = 0.05, hjust = 0,
aes(label = paste(..wb.name.., ..y.label.., sep = " = "),
color = "black")) +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
ggplot(sun.spct, unit.out = "photon") +
stat_wb_q_sirrad(w.band = VIS_bands(), geom = "rect", alpha = 0.5) +
stat_wb_q_irrad(w.band = VIS_bands(), angle = 90, geom = "text",
label.mult = 1e6, ypos.fixed = 1e-7, hjust = 0,
aes(label = paste(..wb.name.., ..y.label.., sep = " = "),
color = "black")) +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
my.bands <- split_bands(c(300,800), length.out = 10)
ggplot(sun.spct) +
stat_wb_e_sirrad(w.band = my.bands, geom = "rect", alpha = 0.5) +
stat_wb_e_irrad(w.band = my.bands, angle = 90, geom = "text",
ypos.fixed = 0.05, hjust = 0, color = "black") +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
my.bands <- split_bands(c(300,800), length.out = 10)
ggplot(sun.spct, unit.out = "photon") +
stat_wb_q_sirrad(w.band = my.bands, geom = "rect", alpha = 0.5) +
stat_wb_q_sirrad(w.band = my.bands, geom = "text", angle = 90,
label.mult = 1e6, color = "black") +
geom_line() +
scale_color_identity() +
scale_fill_identity() +
theme_bw()
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, is only the \(x\) easthetic is required.
ggplot(data.frame(w.length = 300:800), aes(w.length)) +
stat_wb_label(w.band = VIS_bands(), geom = "rect", ymax = Inf, ymin = -Inf) +
stat_wb_label(w.band = VIS_bands(), angle = 90, color = "white") +
scale_fill_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") +
theme_minimal()
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 w.band
with argument NULL
. This stat operates only on the variable mapped to the x aesthetic, a y-mapping is not required as input.
my.data <- data.frame(x = 300:800)
ggplot(my.data, aes(x)) + stat_wl_strip(ymin = -1, ymax = 1) +
scale_fill_identity()
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 w.band
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() +
theme_bw()
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(w.band = VIS_bands(), ymin = -Inf, ymax = -0.025) +
scale_fill_identity() +
theme_bw()
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(w.band = VIS_bands(), ymin = -Inf, ymax = Inf, alpha = 0.4) +
scale_fill_identity() +
geom_line() +
theme_bw()
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() +
theme_bw()
The stats described above can be used together to produce more complex plots. This is just one possibility out of a vast range. Be aware that when adding many layers to the same plot, the order of the layers and adjusting their transparency using the alpha
aesthetic is very important, and best tuned by trial and error.
ggplot(sun.spct, unit.out = "photon") +
stat_wl_strip(alpha = 0.4, ymin = -Inf, ymax = Inf) +
stat_wb_total(w.band = PAR(), color = "black", geom = "rect") +
stat_wb_total(w.band = PAR(), label.mult = 1e6,
geom = "text", color = "black",
aes(label = paste(..wb.name.., " = ", ..y.label.., sep = ""))) +
geom_line() +
scale_fill_identity() +
theme_bw()
A simple geom called "spct"
is defined. It differs from geom "area"
only in the default position
which is "stacked"
for geom "area"
and "identity"
for geom "spct"
which is usually better for spectra.
ggplot(sun.spct) + geom_spct()
Using as fill the color calculated from the spectral data is different to earlier examples in that the spectral irradiance is actually taken into account in the calculation of the colour, instead of just the wavelength range.
ggplot(sun.spct) +
geom_spct(fill = color(sun.spct))
ggplot(sun.spct * yellow_gel.spct) +
geom_spct(fill = color(sun.spct * yellow_gel.spct))
Some of the examples in previous sections can be simplified by use of wl_guide()
and wb_guide()
which add both the statistics and the scale, and provide a default mapping for both ymin
and ymax
. This is handy for simple plots, but it can generate spurious warnings due to the replacement of existing scales with identical ones. In general only one of wl_guide()
or wb_guide()
should be used in a plot, and each one, only once. They just save some typing for the simplest uses, but are not suitable for complex decorations.
The examples for stat_wl_strip
can be simplified by use of wl_guide()
which adds both the statistics and the scale, and provides default mappings for both ymin
(-Inf
) and ymax
(Inf
). Another important difference is that the "rect"
geom used cannot be set to a different one by the user.
ggplot(sun.spct) +
wl_guide(alpha = 0.4) +
geom_line()
ggplot(sun.spct) +
wl_guide(ymax = -0.025) +
geom_line()
This example produces a plot with a very different look than earlier ones, but can also be achieved with a very succinct statement.
ggplot(sun.spct) +
wl_guide() +
geom_spct(alpha = 0.75, colour = "white", size = 1)
In the next example we demonstrate a trick achieved by overploting two line of different width (size
) to make the spectrum visible on a background of variable luminosity.
ggplot(sun.spct) +
wl_guide() +
geom_line(size = 2, colour = "white") +
geom_line(size = 1, colour = "black") +
geom_hline(yintercept = 0, colour = "grey92") +
theme_bw()