Skip to contents

ggplot() initializes a ggplot object. It can be used to declare the input spectral object for a graphic and to optionally specify the set of plot aesthetics intended to be common throughout all subsequent layers unless specifically overridden.

Usage

# S3 method for class 'source_spct'
ggplot(
  data,
  mapping = NULL,
  ...,
  range = NULL,
  unit.out = getOption("photobiology.radiation.unit", default = "energy"),
  environment = parent.frame()
)

# S3 method for class 'response_spct'
ggplot(
  data,
  mapping = NULL,
  ...,
  range = NULL,
  unit.out = getOption("photobiology.radiation.unit", default = "energy"),
  environment = parent.frame()
)

# S3 method for class 'filter_spct'
ggplot(
  data,
  mapping = NULL,
  ...,
  range = NULL,
  plot.qty = getOption("photobiology.filter.qty", default = "transmittance"),
  environment = parent.frame()
)

# S3 method for class 'reflector_spct'
ggplot(
  data,
  mapping = NULL,
  ...,
  range = NULL,
  plot.qty = NULL,
  environment = parent.frame()
)

# S3 method for class 'cps_spct'
ggplot(data, mapping = NULL, ..., range = NULL, environment = parent.frame())

# S3 method for class 'calibration_spct'
ggplot(data, mapping = NULL, ..., range = NULL, environment = parent.frame())

# S3 method for class 'raw_spct'
ggplot(data, mapping = NULL, ..., range = NULL, environment = parent.frame())

# S3 method for class 'object_spct'
ggplot(
  data,
  mapping = NULL,
  ...,
  range = NULL,
  plot.qty = getOption("photobiology.object.qty", default = "all"),
  environment = parent.frame()
)

# S3 method for class 'generic_spct'
ggplot(
  data,
  mapping = NULL,
  ...,
  range = NULL,
  spct_class,
  environment = parent.frame()
)

# S3 method for class 'generic_mspct'
ggplot(data, mapping = NULL, ..., range = NULL, environment = parent.frame())

# S3 method for class 'filter_mspct'
ggplot(
  data,
  mapping = NULL,
  ...,
  range = NULL,
  plot.qty = getOption("photobiology.filter.qty", default = "transmittance"),
  environment = parent.frame()
)

# S3 method for class 'source_mspct'
ggplot(
  data,
  mapping = NULL,
  ...,
  range = NULL,
  unit.out = getOption("photobiology.radiation.unit", default = "energy"),
  environment = parent.frame()
)

# S3 method for class 'object_mspct'
ggplot(
  data,
  mapping = NULL,
  ...,
  range = NULL,
  plot.qty = getOption("photobiology.object.qty", default = ifelse(length(data) > 1L,
    "as.is", "all")),
  environment = parent.frame()
)

Arguments

data

Default spectrum dataset to use for plot. If not a spectrum, the methods used will be those defined in package ggplot2. See ggplot. If not specified, must be supplied in each layer added to the plot.

mapping

Default list of aesthetic mappings to use for plot. If not specified, in the case of spectral objects, a default mapping will be used.

...

Other arguments passed on to methods.

range

an R object on which range() returns a vector of length 2, with min and max wavelengths (nm).

unit.out

character string indicating type of units to use for plotting spectral irradiance or spectral response, "photon" or "energy".

environment

If a variable defined in the aesthetic mapping is not found in the data, ggplot will look for it in this environment. It defaults to using the environment in which ggplot() is called.

plot.qty

character string One of "transmittance", "absorptance" or "absorbance" for filter_spct objects, and in addition to these "reflectance", "all" or "as.is" for object_spct objects.

spct_class

character Class into which a generic_spct object will be converted before plotting. The column names in data should match those expected by the class constructor (see setGenericSpct); other arguments should be passed by name).

Details

ggplot() is typically used to construct a plot incrementally, using the + operator to add layers to the existing ggplot object. This is advantageous in that the code is explicit about which layers are added and the order in which they are added. For complex graphics with multiple layers, initialization with ggplot is recommended.

We show seven common ways to invoke ggplot for spectra and collections of spectra:

  • ggplot(spct)

  • ggplot(spct, unit.out = <unit.to.use>)

  • ggplot(spct, plot.qty = <quantity.to.plot>)

  • ggplot(spct, range = <wavelength.range>)

  • ggplot(spct) + aes(<other aesthetics>)

  • ggplot(spct, aes(x, y, <other aesthetics>))

  • ggplot(spct, aes())

The first method is recommended if all layers use the same data and the same set of automatic default x and y aesthetics. The second, third and fourth use automatic default x and y aesthetics but first transform or trim the spectral data to be plotted. The fifth uses automatic default x and y aesthetics and adds mappings for other aesthetics. These patterns can be combined as needed. The sixth disables the use of a default automatic mapping, while the seventh delays the mapping of aesthetics and can be convenient when using different mappings for different geoms.

Note

Current implementation does not merge the default mapping with user supplied mapping. If user supplies a mapping, it is used as is, and variables should be present in the spectral object. In contrast, when using the default mapping, unit or quantity conversions are done on the fly when needed. To add to the default mapping, aes() can be used by itself to compose the ggplot. In all cases, except when an object_spct is converted into long form, the data member of the returned plot object retains its class and attributes.

plot.qty is ignored for reflectors.

Object spectra

In the case of class object_spct, the arguments "all" and "as.is" if passed to plot.qty, indicate in the first case that the data are to be converted into long form, to allow stacking, while in the second case data is copied unchanged to the plot object. "reflectance" passed to plot.qty converts data into a replector_spct object and "absorbance", "absorptance" and "reflectance", convert data into a filter_spct.

Collections of spectra

The method for collections of spectra accepts arguments for the same parameters as the corresponding methods for single spectra. Heterogeneous generic collections of spectra are not supported. When plotting collections of spectra the factor spct.idx contains as levels the names of the individual members of the collection, and can be mapped to aesthetics or used for faceting.

Examples


ggplot(sun.spct) + geom_line()

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


ggplot(yellow_gel.spct) + geom_line()

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


ggplot(Ler_leaf.spct) + facet_grid(~variable) + geom_line()

ggplot(Ler_leaf.spct) + aes(linetype = variable) + geom_line()