Learn R Programming

ggspectra

Extensions to ‘ggplot2’ for Radiation Spectra

The goal of ‘ggspectra’ is to make it easy to plot radiation spectra and similar data, such and transmittance, absorbance and reflectance spectra, producing fully annotated publication- and presentation-ready plots. It is an extension to packages ‘ggplot2’ and ‘photobiology’, and part of the ‘r4photobiology’ suite.

Package ‘ggspectra’ provides stats, geoms, scales and functions to be used for plotting radiation-related spectra and annotating the plots with quantities derived from spectral data. All summary quantities implemented in ‘photobiology’ can be used for annotations including biologically effective irradiances and exposures.

Please, see the web page The R for Photobiology Suite for an overview of the component packages.

Examples

Plotting spectra with an specialization of the autoplot() method provides the most automatic approach. The specializations provided support the classes defined in package ‘photobiology’, both for individual spectra and for collections of spectra.

library(ggspectra)
autoplot(sun.spct)

Some degree of flexibility can be obtained by passing arguments.

autoplot(sun.spct, annotations = c("+", "title:where:when"), unit.out = "photon")

Maximum flexibility is achieved when building a plot layer by layer using the grammar of graphics approach, as implemented in ‘ggplot2’, but taking advantage of the specialized ggplot() methods, geometries, statistics and scales specialized for the plotting of spectra, defined in this package. Although designed with the classes defined from package ‘photobiology’ in mind, geometries, statistics and scales can also be used with data stored in other formats, as long as the user pays attention to the expected units and bases of expression expected.

ggplot(sun.spct, unit.out = "photon") +
  geom_spct() +
  scale_y_s.q.irrad_continuous() +
  scale_x_wl_continuous(sec.axis = sec_axis_w_number()) +
  theme_bw()

Installation

Installation of the most recent stable version from CRAN:

install.packages("ggspectra")

Installation of the current unstable version from GitHub:

# install.packages("remotes")
remotes::install_github("aphalo/ggspectra")

Documentation

HTML documentation is available at (https://docs.r4photobiology.info/ggspectra/), including additional on-line-only articles.

News on updates to the different packages of the ‘r4photobiology’ suite are regularly posted at (https://www.r4photobiology.info/).

Two articles introduce the basic ideas behind the design of the suite and its use: Aphalo P. J. (2015) (https://doi.org/10.19232/uv4pb.2015.1.14) and Aphalo P. J. (2016) (https://doi.org/10.19232/uv4pb.2016.1.15).

A book is under preparation, and the draft is currently available at (https://leanpub.com/r4photobiology/). The book contains many examples of plots created with ‘ggspectra’.

A handbook written before the suite was developed contains useful information on the quantification and manipulation of ultraviolet and visible radiation: Aphalo, P. J., Albert, A., Björn, L. O., McLeod, A. R., Robson, T. M., & Rosenqvist, E. (Eds.) (2012) Beyond the Visible: A handbook of best practice in plant UV photobiology (1st ed., p. xxx + 174). Helsinki: University of Helsinki, Department of Biosciences, Division of Plant Biology. ISBN 978-952-10-8363-1 (PDF), 978-952-10-8362-4 (paperback). PDF file available from (https://doi.org/10.31885/9789521083631).

Contributing

Pull requests, bug reports, and feature requests are welcome at (https://github.com/aphalo/ggspectra).

Citation

If you use this package to produce scientific or commercial publications, please cite according to:

citation("ggspectra")
#> To cite package ‘photobiology’ in publications use:
#> 
#>   Aphalo, Pedro J. (2015) The r4photobiology suite. UV4Plants Bulletin,
#>   2015:1, 21-29. DOI:10.19232/uv4pb.2015.1.14
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Article{,
#>     author = {Pedro J. Aphalo},
#>     title = {The r4photobiology suite},
#>     journal = {UV4Plants Bulletin},
#>     volume = {2015},
#>     number = {1},
#>     pages = {21-29},
#>     year = {2015},
#>     doi = {10.19232/uv4pb.2015.1.14},
#>   }

License

© 2015-2025 Pedro J. Aphalo (pedro.aphalo@helsinki.fi). Released under the GPL, version 2 or greater. This software carries no warranty of any kind.

Copy Link

Version

Install

install.packages('ggspectra')

Monthly Downloads

594

Version

0.3.16

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Pedro Aphalo

Last Published

June 24th, 2025

Functions in ggspectra (0.3.16)

autoplot.reflector_spct

Plot one or more reflector spectra.
Tfr_label

Transmittance axis labels
apply_normalization

Apply normalization argument
autoplot.cps_spct

Plot one or more detector-counts-per-second spectra.
autoplot.raw_spct

Plot one or more raw-detector-counts spectra.
autoplot.response_spct

Plot one or more response spectra.
autoplot.object_spct

Plot one or more "object" spectra.
autoplot.filter_spct

Plot one or more "filter" spectra.
autoplot.calibration_spct

Plot one or more irradiance-calibration spectra.
autoplot.generic_spct

Plot generic spectral data.
autoplot.source_spct

Plot one or more light-source spectra.
axis_labels_uk

Default text for axis labels
check_idfactor_arg

Check idfactor argument and retrieve if needed
cps_label

Counts-per-second axis labels
autotitle

Add title, subtitle and caption to a spectral plot
black_or_white

Chose black vs. white color based on weighted mean of RGB channels
counts_label

Raw-counts axis labels
color_chart

Create a color checker chart
autoplot.waveband

Create a complete ggplot for a waveband descriptor.
cal_plot

Create a complete ggplot for an irradiation calibration spectrum.
cps_plot

Create a complete ggplot for detector-counts per second spectra.
exponent2prefix

SI unit prefixes
e_plot

Create a complete ggplot for light-source spectra.
find_idfactor

Find 'idfactor'
generic_plot

Create a complete ggplot for generic spectral data.
e_rsp_plot

Create a complete ggplot for response spectra.
fast_wb2rect_df

Colours from wavebands
plot.generic_spct

Deprecated plot methods
multiplot

Multiple plot function
geom_spct

Spectral data plots.
q_plot

Create a complete ggplot for light-source spectra.
scale_x_energy_eV_continuous

Energy per photon x-scale
ggspectra-package

ggspectra: Extensions to 'ggplot2' for Radiation Spectra
multipliers_label

Calibration multipliers axis labels
ggplot

Create a new ggplot plot from spectral data.
scale_x_wl_continuous

Wavelength x-scale
decoration

Add decorations to spectrum plot (private)
s.e.irrad_label

Spectral irradiance axis labels
s.e.response_label

spectral response and action axis labels
raw_plot

Create a complete ggplot for raw detector-counts spectra.
GeomSpct

Stat* Objects
duration2character

Convert lubridate duration objects to a string if possible
rename_idfactor

Update idfactor name in object
scale_y_A_continuous

Absorbance y-scale
scale_x_wavenumber_continuous

Wavenumber x-scale
scale_x_frequency_continuous

Frequency x-scale
q_rsp_plot

Create a complete ggplot for response spectra.
scale_y_cps_continuous

Counts-per-second y-scale
decode_annotations

Merge user supplied annotations with default ones
sec_axis_w_number

Secondary axes for wavelengths
scale_y_Afr_continuous

Absorptance y-scale
set_annotations_default

Set defaults for plotting
scale_y_multipliers_continuous

Calibration multipliers y-scale
scale_y_s.e.irrad_continuous

Spectral irradiance y-scale
scale_y_s.e.response_continuous

Spectral response and action y-scales
stat_color

Calculate colours from wavelength.
stat_find_qtys

Find quantity value for target wavelength value.
stat_wb_contribution

Integrate ranges under spectral curve.
scale_y_Tfr_continuous

Transmittance y-scale
stat_wb_box

Draw colour boxes for wavebands
scale_y_Rfr_continuous

Reflectance y-scale
scale_y_counts_continuous

Raw-counts y-scale
stat_peaks

Find peaks and valleys.
stat_spikes

Find spikes
stat_find_wls

Find wavelength for target quantity value.
stat_label_peaks

Label peaks and valleys.
stat_wb_column

Integrate ranges under curve.
stat_wb_label

Label ranges under spectral curve.
stat_wb_irrad

Integrate irradiance for wavebands.
stat_wb_sirrad

Integrate spectral irradiance for wavebands.
title_spct

Build a plot title from object metadata (deprecated!)
stat_wb_hbar

Integrate ranges under curve.
stat_wb_total

Integrate ranges under spectral curve.
stat_wl_strip

Calculate colours from wavelength.
stat_wl_summary

Average area under curve for regions.
subtitle_spct.generic_spct

Build a plot subtitle from object metadata (deprecated!)
w_number

Deprecated functions
w_length_label

Wave- axis labels
stat_wb_mean

Integrate ranges under curve.
stat_wb_relative

Integrate ranges under spectral curve.
Afr_label

Absorptance axis labels
SI_pl_format

Formatter for plain labels discounting for SI multipliers
R_plot

Create a complete ggplot for a reflector spectrum.
T_plot

Create a complete ggplot for a filter spectrum.
A_label

Absorbance axis labels
SI_tg_format

Formatter for tagged labels using SI multipliers
Rfr_label

Reflectance axis labels
Afr_plot

Create a complete ggplot for a filter spectrum.
O_plot

Create a complete ggplot for a object spectrum.
A_plot

Create a complete ggplot for a filter spectrum.