Skip to content

Commit

Permalink
Merge pull request #11 from aphalo/develop-0.3.13
Browse files Browse the repository at this point in the history
Update autoplot() methods
  • Loading branch information
aphalo authored Jul 6, 2024
2 parents efee4af + a257689 commit a413893
Show file tree
Hide file tree
Showing 438 changed files with 61,922 additions and 59,460 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: ggspectra
Type: Package
Title: Extensions to 'ggplot2' for Radiation Spectra
Version: 0.3.12.9000
Date: 2024-04-26
Version: 0.3.12.9001
Date: 2024-07-06
Authors@R:
c(
person("Pedro J.", "Aphalo", email = "pedro.aphalo@helsinki.fi", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-3385-972X")),
Expand All @@ -19,7 +19,7 @@ LazyLoad: TRUE
ByteCompile: TRUE
Depends:
R (>= 4.1.0),
photobiology (> 0.11.1),
photobiology (>= 0.11.2),
ggplot2 (>= 3.4.0)
Imports:
photobiologyWavebands (>= 0.5.1),
Expand All @@ -36,5 +36,5 @@ URL: https://docs.r4photobiology.info/ggspectra/,
https://github.com/aphalo/ggspectra/
BugReports: https://github.com/aphalo/ggspectra/issues/
Encoding: UTF-8
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
VignetteBuilder: knitr
15 changes: 11 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ to avoid "surprising" users.
formal parameter `add.symbols` with default given by R option
`ggspectra.add.symbols`.
- Revise _scales_ for transmittance, absorptance and reflectance and the
matching _axis-label building functions_ by adding
formal parameter `pc.out` with default given by R option
`ggspectra.pc.out`.
- Revise all autoplot methods so that the default for their `pc.out` defaults
matching _axis-label building functions_ by adding formal parameter `pc.out`
with default given by R option `ggspectra.pc.out`.
- Revise all `autoplot()` methods so that the default for their `pc.out` defaults
to the value of R option `ggspectra.pc.out`.
- Revise all `autoplot()` methods for objects of class `generic_spct` and
derived so that summaries requested by arguments passed to parameter `plot.data`
are computed for multiple spectra in long form like they were for collections
of spectra.
- Fix broken `idfactor` handling in `autoplot()` methods for objects of class `generic_spct` and derived.
- Use in `autoplot()` methods a plotmath expression for the wavelength axis
label, including lambda as symbol, for consistency with the labels of the
_y_-axis.

# ggspectra 0.3.12

Expand Down
99 changes: 39 additions & 60 deletions R/autoplot-calibration-spct.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ cal_plot <- function(spct,
plot <- plot + geom_spct(fill = "black", colour = NA, alpha = 0.2)
}
plot <- plot + geom_line(na.rm = na.rm)
plot <- plot + labs(x = "Wavelength (nm)", y = s.counts.label)
plot <- plot + labs(x = expression("Wavelength, "*lambda~(nm)), y = s.counts.label)

if (length(annotations) == 1 && annotations == "") {
return(plot)
Expand Down Expand Up @@ -230,68 +230,18 @@ cal_plot <- function(spct,

}

#' Create a complete ggplot for an irradiation calibration spectrum.
#' Plot one or more irradiance-calibration spectra.
#'
#' These methods return a ggplot object with an annotated plot of a
#' calibration_spct object or of the spectra contained in a calibration_mspct
#' These methods return a ggplot object with an annotated plot of the spectral
#' data contained in a \code{calibration_spct} or a \code{calibration_mspct}
#' object.
#'
#' @note Note that scales are expanded so as to make space for the annotations.
#' The object returned is a ggplot object, and can be further manipulated.
#'
#' @inheritSection decoration Plot Annotations
#' @inheritSection autotitle Title Annotations
#' @inherit autoplot.source_spct
#'
#' @param object a calibration_spct object or a calibration_mspct object.
#' @param ... in the case of collections of spectra, additional arguments passed
#' to the plot methods for individual spectra, otherwise currently ignored.
#' @param w.band a single waveband object or a list of waveband objects.
#' @param range an R object on which range() returns a vector of length 2, with
#' min annd max wavelengths (nm).
#' @param norm numeric Normalization wavelength (nm) or character string "max",
#' or "min" for normalization at the corresponding wavelength, "update" to
#' update the normalization after modifying units of expression, quantity
#' or range but respecting the previously used criterion, or "skip" to force
#' return of \code{object} unchanged. Always skipped for
#' \code{plot.qty == "all"}, which is the default.
#' @param unit.out character IGNORED.
#' @param pc.out logical, if TRUE use percents instead of fraction of one.
#' @param label.qty character string giving the type of summary quantity to use
#' for labels, one of "mean", "total", "contribution", and "relative".
#' @param span a peak is defined as an element in a sequence which is greater
#' than all other elements within a window of width span centered at that
#' element.
#' @param wls.target numeric vector indicating the spectral quantity values for
#' which wavelengths are to be searched and interpolated if need. The
#' \code{character} strings "half.maximum" and "half.range" are also accepted
#' as arguments. A list with \code{numeric} and/or \code{character} values is
#' also accepted.
#' @param annotations a character vector ("summaries" is ignored). For details
#' please see sections Plot Annotations and Title Annotations.
#' @param geom character The name of a ggplot geometry, currently only
#' \code{"area"}, \code{"spct"} and \code{"line"}. The default \code{NULL}
#' selects between them based on \code{stacked}.
#' @param time.format character Format as accepted by
#' \code{\link[base]{strptime}}.
#' @param tz character Time zone to use for title and/or subtitle.
#' @param norm numeric normalization wavelength (nm) or character string "max"
#' for normalization at the wavelength of highest peak.
#' @param text.size numeric size of text in the plot decorations.
#' @param idfactor character Name of an index column in data holding a
#' \code{factor} with each spectrum in a long-form multispectrum object
#' corresponding to a distinct spectrum. If \code{idfactor=NULL} the name of
#' the factor is retrieved from metadata or if no metadata found, the default
#' "spct.idx" is tried. If \code{idfactor=NA} no aesthetic is mapped to the
#' spectra and the user needs to use 'ggplot2' functions to manually map an
#' aesthetic or use facets for the spectra.
#' @param facets logical or integer Indicating if facets are to be created for
#' the levels of \code{idfactor} when \code{spct} contain multiple spectra in
#' long form.
#' @param ylim numeric y axis limits,
#' @param object.label character The name of the object being plotted.
#' @param na.rm logical.
#'
#' @return a \code{ggplot} object.
#'
#' @seealso \code{\link[photobiology]{normalize}},
#' \code{\link[photobiology]{calibration_spct}},
Expand Down Expand Up @@ -321,10 +271,44 @@ autoplot.calibration_spct <-
text.size = 2.5,
idfactor = NULL,
facets = FALSE,
plot.data = "as.is",
ylim = c(NA, NA),
object.label = deparse(substitute(object)),
na.rm = TRUE) {

if (is.null(idfactor)) {
idfactor <- getIdFactor(object)
}
if (is.na(idfactor) || !is.character(idfactor)) {
idfactor <- getMultipleWl(object) > 1L
}

if (plot.data != "as.is") {
return(
autoplot(object = subset2mspct(object),
w.band = w.band,
range = range,
norm = norm,
unit.out = unit.out,
pc.out = pc.out,
label.qty = label.qty,
span = span,
wls.target = wls.target,
annotations = annotations,
geom = geom,
time.format = time.format,
tz = tz,
text.size = text.size,
# chroma.type = chroma.type,
idfactor = idfactor,
facets = facets,
plot.data = plot.data,
ylim = ylim,
object.label = object.label,
na.rm = na.rm)
)
}

force(object.label)

annotations.default <-
Expand Down Expand Up @@ -367,11 +351,6 @@ autoplot.calibration_spct <-

#' @rdname autoplot.calibration_spct
#'
#' @param plot.data character Data to plot. Default is "as.is" plotting one line
#' per spectrum. When passing "mean", "median", "sum", "prod", var", "sd",
#' "se" as argument all the spectra must contain data at the same wavelength
#' values.
#'
#' @export
#'
autoplot.calibration_mspct <-
Expand Down
92 changes: 39 additions & 53 deletions R/autoplot-cps-spct.r
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ cps_plot <- function(spct,
plot <- plot + geom_spct(fill = "black", colour = NA, alpha = 0.2)
}
plot <- plot + geom_line(na.rm = na.rm)
plot <- plot + labs(x = "Wavelength (nm)", y = s.cps.label)
plot <- plot + labs(x = expression("Wavelength, "*lambda~(nm)), y = s.cps.label)

if (length(annotations) == 1 && annotations == "") {
return(plot)
Expand Down Expand Up @@ -214,60 +214,17 @@ cps_plot <- function(spct,

}

#' Create a complete ggplot for detector-counts per second spectra.
#' Plot one or more detector-counts-per-second spectra.
#'
#' This function returns a ggplot object with an annotated plot of a
#' response_spct object.
#'
#' Note that scales are expanded so as to make space for the annotations. The
#' object returned is a ggplot objects, and can be further manipulated.
#' These methods return a ggplot object with an annotated plot of a
#' \code{cps_spct} or a \code{cps_mspct} object.
#'
#' @inheritSection decoration Plot Annotations
#' @inheritSection autotitle Title Annotations
#' @inherit autoplot.source_spct
#'
#' @param object a cps_spct object.
#' @param ... in the case of collections of spectra, additional arguments passed
#' to the plot methods for individual spectra, otherwise currently ignored.
#' @param w.band a single waveband object or a list of waveband objects.
#' @param range an R object on which range() returns a vector of length 2, with
#' min annd max wavelengths (nm).
#' @param unit.out character IGNORED.
#' @param pc.out logical, if TRUE use percent instead of fraction of one for
#' normalized spectral data.
#' @param label.qty character string giving the type of summary quantity to use
#' for labels, one of "mean", "total", "contribution", and "relative".
#' @param span a peak is defined as an element in a sequence which is greater
#' than all other elements within a window of width span centered at that
#' element.
#' @param wls.target numeric vector indicating the spectral quantity values for
#' which wavelengths are to be searched and interpolated if need. The
#' \code{character} strings "half.maximum" and "half.range" are also accepted
#' as arguments. A list with \code{numeric} and/or \code{character} values is
#' also accepted.
#' @param annotations a character vector ("summaries" is ignored). For details
#' please see sections Plot Annotations and Title Annotations.
#' @param geom character The name of a ggplot geometry, currently only
#' \code{"area"}, \code{"spct"} and \code{"line"}. The default \code{NULL}
#' selects between them based on \code{stacked}.
#' @param time.format character Format as accepted by \code{\link[base]{strptime}}.
#' @param tz character Time zone to use for title and/or subtitle.
#' @param norm numeric normalization wavelength (nm) or character string
#' \code{"max"} for normalization at the wavelength of highest peak or
#' \code{"skip"} for no change to exisitng normalization in \code{object}.
#' @param text.size numeric size of text in the plot decorations.
#' @param idfactor character Name of an index column in data holding a
#' \code{factor} with each spectrum in a long-form multispectrum object
#' corresponding to a distinct spectrum. If \code{idfactor=NULL} the name of
#' the factor is retrieved from metadata or if no metadata found, the
#' default "spct.idx" is tried.
#' @param facets logical or integer Indicating if facets are to be created for
#' the levels of \code{idfactor} when \code{spct} contain multiple spectra in
#' long form.
#' @param ylim numeric y axis limits,
#' @param object.label character The name of the object being plotted.
#' @param na.rm logical.
#'
#' @return a \code{ggplot} object.
#'
#' @seealso \code{\link[photobiology]{normalize}},
#' \code{\link[photobiology]{cps_spct}},
Expand Down Expand Up @@ -311,10 +268,44 @@ autoplot.cps_spct <-
text.size = 2.5,
idfactor = NULL,
facets = FALSE,
plot.data = "as.is",
ylim = c(NA, NA),
object.label = deparse(substitute(object)),
na.rm = TRUE) {

if (is.null(idfactor)) {
idfactor <- getIdFactor(object)
}
if (is.na(idfactor) || !is.character(idfactor)) {
idfactor <- getMultipleWl(object) > 1L
}

if (plot.data != "as.is") {
return(
autoplot(object = subset2mspct(object),
w.band = w.band,
range = range,
norm = norm,
unit.out = unit.out,
pc.out = pc.out,
label.qty = label.qty,
span = span,
wls.target = wls.target,
annotations = annotations,
geom = geom,
time.format = time.format,
tz = tz,
text.size = text.size,
# chroma.type = chroma.type,
idfactor = idfactor,
facets = facets,
plot.data = plot.data,
ylim = ylim,
object.label = object.label,
na.rm = na.rm)
)
}

force(object.label)

annotations.default <-
Expand Down Expand Up @@ -367,11 +358,6 @@ autoplot.cps_spct <-

#' @rdname autoplot.cps_spct
#'
#' @param plot.data character Data to plot. Default is "as.is" plotting one line
#' per spectrum. When passing "mean", "median", "sum", "prod", "var", "sd",
#' "se" as argument all the spectra must contain data at the same wavelength
#' values.
#'
#' @export
#'
autoplot.cps_mspct <-
Expand Down
Loading

0 comments on commit a413893

Please sign in to comment.