From 07266b851d499bc7cd17833136ba9780f4fe1f02 Mon Sep 17 00:00:00 2001 From: Gustav Delius Date: Sat, 23 May 2026 07:20:21 +0100 Subject: [PATCH 1/3] Add length axis option to size plots --- NEWS.md | 4 + R/ArraySpeciesBySize-class.R | 40 +++- R/ArrayTimeBySpeciesBySize-class.R | 19 +- R/animateSpectra.R | 19 +- R/plots.R | 316 +++++++++++++++++++++-------- man/addPlot.Rd | 4 + man/animate.Rd | 5 + man/plot.Rd | 7 + man/plot2.Rd | 5 + man/plotCDF.Rd | 10 +- man/plotCDF2.Rd | 5 + man/plotDiet.Rd | 25 ++- man/plotFMort.Rd | 17 +- man/plotFeedingLevel.Rd | 13 +- man/plotM2.Rd | 3 +- man/plotPredMort.Rd | 17 +- man/plotRelative.Rd | 5 + man/plotSpectra.Rd | 11 +- man/plotSpectra2.Rd | 5 + man/plotSpectraRelative.Rd | 21 +- tests/testthat/test-plots.R | 78 +++++++ 21 files changed, 513 insertions(+), 116 deletions(-) diff --git a/NEWS.md b/NEWS.md index 7affeaf9e..9bfb95b07 100644 --- a/NEWS.md +++ b/NEWS.md @@ -134,6 +134,10 @@ individual variability in growth to be modelled. - `plotSpectra()` now accepts `log_x`, `log_y`, and `log` arguments for controlling axis scaling, matching the mizer array `plot()` methods. +- Size-based plots now accept `size_axis = "l"` to show length in cm on the + size axis instead of weight in grams, using the species' allometric + weight-length relationship. + - The `plot()` and `summary()` methods for `MizerParams`, `MizerSim`, and the mizer array classes are now registered as S3 methods rather than S4 methods, so `plot()` and `summary()` remain plain S3 generics when mizer is loaded, diff --git a/R/ArraySpeciesBySize-class.R b/R/ArraySpeciesBySize-class.R index 8a0d25ed7..1128cc136 100644 --- a/R/ArraySpeciesBySize-class.R +++ b/R/ArraySpeciesBySize-class.R @@ -159,6 +159,8 @@ print.summary.ArraySpeciesBySize <- function(x, ...) { #' @param wlim A numeric vector of length two providing lower and upper limits #' for the weight (x) axis. Use `NA` to refer to the existing minimum or #' maximum. Only applies to `ArraySpeciesBySize`. +#' @param size_axis Whether to plot size as weight (`"w"`, default) or length +#' (`"l"`), using the allometric weight-length relationship. #' @param ylim A numeric vector of length two providing lower and upper limits #' for the value (y) axis. Use `NA` to refer to the existing minimum or #' maximum. @@ -180,14 +182,18 @@ print.summary.ArraySpeciesBySize <- function(x, ...) { #' \donttest{ #' plot(getEncounter(NS_params)) #' plot(getFeedingLevel(NS_params), species = c("Cod", "Herring")) +#' plot(getPredMort(NS_params), species = c("Cod", "Herring"), +#' size_axis = "l") #' } plot.ArraySpeciesBySize <- function(x, species = NULL, all.sizes = FALSE, highlight = NULL, return_data = FALSE, log_x = TRUE, log_y = FALSE, log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, ...) { + size_axis <- plot_size_axis(size_axis) log_axes <- parsePlotLog(log, log_x = log_x, log_y = log_y) log_x <- log_axes$log_x log_y <- log_axes$log_y @@ -198,6 +204,7 @@ plot.ArraySpeciesBySize <- function(x, species = NULL, plot_dat <- prepare_ArraySpeciesBySize_plot_data( x, species = species, all.sizes = all.sizes, wlim = wlim, total = total, background = background) + plot_dat <- convert_plot_size_axis(plot_dat, params, size_axis) if (return_data) return(plot_dat) @@ -206,11 +213,13 @@ plot.ArraySpeciesBySize <- function(x, species = NULL, y_label <- paste0(value_name, " [", units_str, "]") } - plotDataFrame(plot_dat, params, xlab = "Size [g]", ylab = y_label, + plotDataFrame(plot_dat, params, xlab = plot_size_xlab(size_axis), + ylab = y_label, xtrans = if (log_x) "log10" else "identity", ytrans = if (log_y) "log10" else "identity", - xlim = wlim, ylim = ylim, highlight = highlight, - y_ticks = y_ticks, legend_var = "Legend") + xlim = plot_size_xlim(wlim, size_axis), ylim = ylim, + highlight = highlight, y_ticks = y_ticks, + legend_var = "Legend") } parsePlotLog <- function(log, log_x = FALSE, log_y = FALSE) { @@ -258,10 +267,12 @@ plot2.ArraySpeciesBySize <- function(x, y, name1 = "First", name2 = "Second", species = NULL, all.sizes = FALSE, log_x = TRUE, log_y = FALSE, log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, ...) { check_plot2_compatible(x, y, "ArraySpeciesBySize") compare_array_metadata(x, y) + size_axis <- plot_size_axis(size_axis) log_axes <- parsePlotLog(log, log_x = log_x, log_y = log_y) log_x <- log_axes$log_x log_y <- log_axes$log_y @@ -277,11 +288,12 @@ plot2.ArraySpeciesBySize <- function(x, y, name1 = "First", name2 = "Second", plotComparisonDataFrame(plot_dat1, plot_dat2, params, name1 = name1, name2 = name2, - xlab = "Size [g]", ylab = y_label, + xlab = plot_size_xlab(size_axis), ylab = y_label, xtrans = if (log_x) "log10" else "identity", ytrans = if (log_y) "log10" else "identity", - xlim = wlim, ylim = ylim, - y_ticks = y_ticks, legend_var = "Legend") + xlim = plot_size_xlim(wlim, size_axis), ylim = ylim, + y_ticks = y_ticks, legend_var = "Legend", + size_axis = size_axis) } #' Plot the relative difference between two mizer array objects @@ -316,10 +328,12 @@ plotRelative.ArraySpeciesBySize <- function(x, y, species = NULL, log_x = TRUE, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, ...) { check_plot2_compatible(x, y, "ArraySpeciesBySize") compare_array_metadata(x, y) + size_axis <- plot_size_axis(size_axis) params <- attr(x, "params") plot_dat1 <- prepare_ArraySpeciesBySize_plot_data( x, species = species, all.sizes = all.sizes, wlim = wlim, @@ -329,9 +343,10 @@ plotRelative.ArraySpeciesBySize <- function(x, y, species = NULL, total = total, background = background) plotRelativeDataFrame(plot_dat1, plot_dat2, params, - xlab = "Size [g]", + xlab = plot_size_xlab(size_axis), xtrans = if (log_x) "log10" else "identity", - xlim = wlim, ylim = ylim, legend_var = "Legend") + xlim = plot_size_xlim(wlim, size_axis), ylim = ylim, + legend_var = "Legend", size_axis = size_axis) } check_plot2_compatible <- function(x, y, class) { @@ -405,6 +420,7 @@ addPlot <- function(plot, x, ...) { addPlot.ArraySpeciesBySize <- function(plot, x, species = NULL, all.sizes = FALSE, wlim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, colour = NULL, @@ -419,15 +435,19 @@ addPlot.ArraySpeciesBySize <- function(plot, x, species = NULL, is.number(alpha), alpha >= 0, alpha <= 1) + size_axis <- plot_size_axis(size_axis) plot <- deep_copy(plot) plot_dat <- prepare_ArraySpeciesBySize_plot_data( x, species = species, all.sizes = all.sizes, wlim = wlim, total = total, background = background) - check_addPlot_compatible(plot, x_var = "w", y_var = "value", + params <- attr(x, "params") + plot_dat <- convert_plot_size_axis(plot_dat, params, size_axis) + x_var <- plot_size_x_var(size_axis) + check_addPlot_compatible(plot, x_var = x_var, y_var = "value", units = attr(x, "units")) - mapping <- aes(x = .data[["w"]], y = .data[["value"]], + mapping <- aes(x = .data[[x_var]], y = .data[["value"]], group = .data[["Species"]]) if (is.null(colour)) { mapping$colour <- rlang::quo(.data[["Legend"]]) diff --git a/R/ArrayTimeBySpeciesBySize-class.R b/R/ArrayTimeBySpeciesBySize-class.R index 84e743e84..4651ff200 100644 --- a/R/ArrayTimeBySpeciesBySize-class.R +++ b/R/ArrayTimeBySpeciesBySize-class.R @@ -144,6 +144,7 @@ plot.ArrayTimeBySpeciesBySize <- function(x, species = NULL, time = NULL, return_data = FALSE, log_x = TRUE, log_y = FALSE, log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, ...) { params <- attr(x, "params") @@ -167,8 +168,9 @@ plot.ArrayTimeBySpeciesBySize <- function(x, species = NULL, time = NULL, plot.ArraySpeciesBySize(slice, species = species, all.sizes = all.sizes, highlight = highlight, return_data = return_data, log_x = log_x, log_y = log_y, log = log, - wlim = wlim, ylim = ylim, total = total, - background = background, y_ticks = y_ticks, ...) + wlim = wlim, ylim = ylim, size_axis = size_axis, + total = total, background = background, + y_ticks = y_ticks, ...) } #' @rdname plot2 @@ -184,6 +186,7 @@ plot2.ArrayTimeBySpeciesBySize <- function(x, y, name1 = "First", log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, ...) { @@ -194,7 +197,8 @@ plot2.ArrayTimeBySpeciesBySize <- function(x, y, name1 = "First", plot2.ArraySpeciesBySize(slice1, slice2, name1 = name1, name2 = name2, species = species, all.sizes = all.sizes, log_x = log_x, log_y = log_y, log = log, - wlim = wlim, ylim = ylim, total = total, + wlim = wlim, ylim = ylim, + size_axis = size_axis, total = total, background = background, y_ticks = y_ticks, ...) } @@ -209,6 +213,7 @@ plotRelative.ArrayTimeBySpeciesBySize <- function(x, y, species = NULL, log_x = TRUE, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, ...) { check_plot2_compatible(x, y, "ArrayTimeBySpeciesBySize") @@ -218,6 +223,7 @@ plotRelative.ArrayTimeBySpeciesBySize <- function(x, y, species = NULL, plotRelative.ArraySpeciesBySize(slice1, slice2, species = species, all.sizes = all.sizes, log_x = log_x, wlim = wlim, ylim = ylim, + size_axis = size_axis, total = total, background = background, ...) } @@ -269,6 +275,7 @@ animate.ArrayTimeBySpeciesBySize <- function(x, species = NULL, wlim = c(NA, NA), ylim = c(NA, NA), log_y = TRUE, + size_axis = c("w", "l"), total = FALSE, background = TRUE, frame_duration = 500, @@ -280,6 +287,7 @@ animate.ArrayTimeBySpeciesBySize <- function(x, species = NULL, is.number(transition_duration), transition_duration >= 0, is.string(easing), length(wlim) == 2, length(ylim) == 2) + size_axis <- plot_size_axis(size_axis) params <- attr(x, "params") value_name <- attr(x, "value_name") %||% "Value" @@ -350,7 +358,10 @@ animate.ArrayTimeBySpeciesBySize <- function(x, species = NULL, } animate_plotly(df, params, log_x, log_y, y_label, wlim, ylim, - frame_duration, transition_duration, easing) + size_axis = size_axis, + frame_duration = frame_duration, + transition_duration = transition_duration, + easing = easing) } #' @export diff --git a/R/animateSpectra.R b/R/animateSpectra.R index 1b393fbee..45ddcc2e1 100644 --- a/R/animateSpectra.R +++ b/R/animateSpectra.R @@ -34,6 +34,8 @@ #' range. Default is the entire time range of `x`. #' @param log_x If `TRUE` (default), use a log10 x-axis for body size. #' @param log_y If `TRUE` (default), use a log10 y-axis. +#' @param size_axis Whether to plot size as weight (`"w"`, default) or length +#' (`"l"`), using the allometric weight-length relationship. #' @param total A boolean value that determines whether the total over all #' selected species is plotted as an additional trace called `"Total"`. #' Default is `FALSE`. @@ -89,12 +91,14 @@ animate <- function(x, ...) UseMethod("animate") animate.MizerSim <- function(x, species = NULL, time_range = NULL, log_x = TRUE, log_y = TRUE, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), power = 1, total = FALSE, resource = TRUE, background = TRUE, frame_duration = 500, transition_duration = frame_duration, easing = "linear", ...) { sim <- x + size_axis <- plot_size_axis(size_axis) assert_that(is.flag(total), is.flag(resource), is.flag(background), is.number(power), is.number(frame_duration), frame_duration >= 0, @@ -156,7 +160,10 @@ animate.MizerSim <- function(x, species = NULL, time_range = NULL, nf <- mutate(nf, value = value * w^power) animate_plotly(nf, sim@params, log_x, log_y, y_label, wlim, ylim, - frame_duration, transition_duration, easing) + size_axis = size_axis, + frame_duration = frame_duration, + transition_duration = transition_duration, + easing = easing) } # Build a plotly animation from a prepared long-format data frame. @@ -166,8 +173,12 @@ animate.MizerSim <- function(x, species = NULL, time_range = NULL, # appear together and share a single legend entry. animate_plotly <- function(df, params, log_x, log_y, y_label, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = "w", frame_duration = 500, transition_duration = 500, easing = "linear") { + size_axis <- plot_size_axis(size_axis) + df <- convert_plot_size_axis(df, params, size_axis) + x_var <- plot_size_x_var(size_axis) legend_name_order <- intersect(names(params@linecolour), unique(df$legend_name)) sp_order <- unlist(lapply(legend_name_order, function(ln) { @@ -185,7 +196,7 @@ animate_plotly <- function(df, params, log_x, log_y, y_label, p <- plotly::add_lines( p, data = df_sp, - x = ~w, y = ~value, + x = stats::as.formula(paste0("~", x_var)), y = ~value, frame = ~time, name = ln, legendgroup = ln, @@ -194,7 +205,9 @@ animate_plotly <- function(df, params, log_x, log_y, y_label, ) } p <- plotly::layout(p, - xaxis = plotly_axis(df$w, wlim, log_x, "Size [g]"), + xaxis = plotly_axis( + df[[x_var]], plot_size_xlim(wlim, size_axis), + log_x, plot_size_xlab(size_axis)), yaxis = plotly_axis(df$value, ylim, log_y, y_label), legend = list(traceorder = "normal")) plotly::animation_opts(p, frame = frame_duration, diff --git a/R/plots.R b/R/plots.R index 7d76b0458..617ee4e4a 100644 --- a/R/plots.R +++ b/R/plots.R @@ -131,7 +131,7 @@ NULL # Hackiness to get past the 'no visible binding ... ' warning when running check -utils::globalVariables(c("time", "value", "Species", "w", "gear", "Age", +utils::globalVariables(c("time", "value", "Species", "w", "l", "gear", "Age", "x", "y", "Year", "Yield", "Biomass", "Size", "Proportion", "Prey", "Legend", "Type", "Gear", "Predator", "weight", "a", "b", "age", "w_max", @@ -289,7 +289,8 @@ plotComparisonDataFrame <- function(frame1, frame2, params, xlab = waiver(), ylab = waiver(), xtrans = "identity", ytrans = "identity", xlim = c(NA, NA), ylim = c(NA, NA), - y_ticks = 6, legend_var = "Legend") { + y_ticks = 6, legend_var = "Legend", + size_axis = NULL) { assert_that(is.data.frame(frame1), is.data.frame(frame2), is(params, "MizerParams")) @@ -309,6 +310,12 @@ plotComparisonDataFrame <- function(frame1, frame2, params, stop("The `legend_var` argument must be the name of a variable ", "in the data frame.") } + if (!is.null(size_axis)) { + size_axis <- plot_size_axis(size_axis) + frame <- convert_plot_size_axis(frame, params, size_axis, + species_col = group_var) + x_var <- plot_size_x_var(size_axis) + } legend_levels <- intersect(names(params@linecolour), frame[[legend_var]]) frame[[legend_var]] <- factor(frame[[legend_var]], levels = legend_levels) @@ -343,7 +350,8 @@ plotRelativeDataFrame <- function(frame1, frame2, params, xtrans = "identity", xlim = c(NA, NA), ylim = c(NA, NA), - legend_var = "Legend") { + legend_var = "Legend", + size_axis = NULL) { assert_that(is.data.frame(frame1), is.data.frame(frame2), is(params, "MizerParams")) @@ -365,6 +373,12 @@ plotRelativeDataFrame <- function(frame1, frame2, params, frame$rel_diff <- relative_difference(frame[[paste0(y_var, ".x")]], frame[[paste0(y_var, ".y")]]) frame <- frame[is.finite(frame$rel_diff), ] + if (!is.null(size_axis)) { + size_axis <- plot_size_axis(size_axis) + frame <- convert_plot_size_axis(frame, params, size_axis, + species_col = group_var) + x_var <- plot_size_x_var(size_axis) + } legend_levels <- intersect(names(params@linecolour), frame[[legend_var]]) frame[[legend_var]] <- factor(frame[[legend_var]], levels = legend_levels) @@ -416,6 +430,56 @@ log_breaks <- function(n = 6) { } } +plot_size_axis <- function(size_axis = "w") { + match.arg(size_axis, c("w", "l")) +} + +plot_size_x_var <- function(size_axis) { + if (identical(plot_size_axis(size_axis), "l")) "l" else "w" +} + +plot_size_xlab <- function(size_axis) { + if (identical(plot_size_axis(size_axis), "l")) "Length [cm]" else "Size [g]" +} + +plot_size_xlim <- function(wlim, size_axis) { + if (identical(plot_size_axis(size_axis), "l")) c(NA, NA) else wlim +} + +plot_size_tooltip <- function(size_axis, before = NULL, after = NULL) { + c(before, plot_size_x_var(size_axis), after) +} + +convert_plot_size_axis <- function(plot_dat, params, size_axis, + species_col = "Species") { + size_axis <- plot_size_axis(size_axis) + if (identical(size_axis, "w")) { + return(plot_dat) + } + if (!is(params, "MizerParams")) { + stop("Length-axis plots need a MizerParams object.") + } + if (!"w" %in% names(plot_dat)) { + stop("Length-axis plots need plotting data with a `w` column.") + } + if (!species_col %in% names(plot_dat)) { + stop("Length-axis plots need plotting data with a `", species_col, + "` column.") + } + + species <- as.character(plot_dat[[species_col]]) + species_idx <- match(species, as.character(params@species_params$species)) + plot_dat <- plot_dat[!is.na(species_idx), , drop = FALSE] + species_idx <- species_idx[!is.na(species_idx)] + if (nrow(plot_dat) == 0) { + return(plot_dat[, setdiff(names(plot_dat), "w"), drop = FALSE]) + } + sp <- params@species_params[species_idx, , drop = FALSE] + plot_dat$l <- w2l(plot_dat$w, sp) + plot_dat[, c("l", setdiff(names(plot_dat), c("l", "w"))), + drop = FALSE] +} + #' Plot the biomass of species through time #' @@ -775,13 +839,15 @@ plotlyYieldGear <- function(sim, species = NULL, #' @param log Character string specifying which axes should use log10 scales, #' in the same form as the base [plot()] argument. For example, `"x"`, #' `"y"`, `"xy"` or `""`. If supplied, this overrides `log_x` and `log_y`. +#' @param size_axis Whether to plot size as weight (`"w"`, default) or length +#' (`"l"`), using the allometric weight-length relationship. #' @param return_data A boolean value that determines whether the formatted data #' used for the plot is returned instead of the plot itself. Default value is FALSE #' @param ... Other arguments (currently unused) #' #' @return A ggplot2 object, unless `return_data = TRUE`, in which case a data -#' frame with the four variables 'w', 'value', 'Species', 'Legend' is -#' returned. +#' frame with the four variables 'w' (or 'l' if `size_axis = "l"`), 'value', +#' 'Species', 'Legend' is returned. #' @export #' @family plotting functions #' @seealso [plotting_functions] @@ -794,6 +860,7 @@ plotlyYieldGear <- function(sim, species = NULL, #' plotSpectra(sim, time_range = 10:20) #' plotSpectra(sim, time_range = 10:20, power = 0) #' plotSpectra(sim, species = c("Cod", "Herring"), power = 1) +#' plotSpectra(sim, species = c("Cod", "Herring"), size_axis = "l") #' #' # Returning the data frame #' fr <- plotSpectra(sim, return_data = TRUE) @@ -815,11 +882,13 @@ plotSpectra.MizerSim <- function(object, species = NULL, total = FALSE, resource = TRUE, background = TRUE, highlight = NULL, log_x = TRUE, log_y = TRUE, - log = NULL, return_data = FALSE, ...) { + log = NULL, size_axis = c("w", "l"), + return_data = FALSE, ...) { # to deal with old-type biomass argument if (missing(power)) { power <- as.numeric(biomass) } + size_axis <- plot_size_axis(size_axis) log_axes <- parsePlotLog(log, log_x = log_x, log_y = log_y) log_x <- log_axes$log_x log_y <- log_axes$log_y @@ -850,6 +919,7 @@ plotSpectra.MizerSim <- function(object, species = NULL, power = power, total = total, resource = resource, background = background, highlight = highlight, log_x = log_x, log_y = log_y, + size_axis = size_axis, return_data = return_data) } @@ -861,11 +931,13 @@ plotSpectra.MizerParams <- function(object, species = NULL, total = FALSE, resource = TRUE, background = TRUE, highlight = NULL, log_x = TRUE, log_y = TRUE, - log = NULL, return_data = FALSE, ...) { + log = NULL, size_axis = c("w", "l"), + return_data = FALSE, ...) { # to deal with old-type biomass argument if (missing(power)) { power <- as.numeric(biomass) } + size_axis <- plot_size_axis(size_axis) log_axes <- parsePlotLog(log, log_x = log_x, log_y = log_y) log_x <- log_axes$log_x log_y <- log_axes$log_y @@ -885,6 +957,7 @@ plotSpectra.MizerParams <- function(object, species = NULL, power = power, total = total, resource = resource, background = background, highlight = highlight, log_x = log_x, log_y = log_y, + size_axis = size_axis, return_data = return_data) } @@ -892,8 +965,9 @@ plotSpectra.MizerParams <- function(object, species = NULL, plot_spectra <- function(params, n, n_pp, species, wlim, ylim, power, total, resource, background, - highlight, log_x, log_y, return_data) { + highlight, log_x, log_y, size_axis, return_data) { params <- validParams(params) + size_axis <- plot_size_axis(size_axis) if (is.na(wlim[1])) { wlim[1] <- if (resource) min(params@w) / 100 else min(params@w) } @@ -964,13 +1038,15 @@ plot_spectra <- function(params, n, n_pp, } filter_min <- if (is.na(ylim[1])) 1e-20 else ylim[1] plot_dat <- plot_dat[plot_dat$value > filter_min, ] + plot_dat <- convert_plot_size_axis(plot_dat, params, size_axis) if (return_data) return(plot_dat) - plotDataFrame(plot_dat, params, xlab = "Size [g]", ylab = y_label, + plotDataFrame(plot_dat, params, xlab = plot_size_xlab(size_axis), + ylab = y_label, xtrans = if (log_x) "log10" else "identity", ytrans = if (log_y) "log10" else "identity", - xlim = wlim, ylim = ylim, + xlim = plot_size_xlim(wlim, size_axis), ylim = ylim, highlight = highlight, legend_var = "Legend") } @@ -994,8 +1070,8 @@ plot_spectra <- function(params, n, n_pp, #' only `"x"` and `""` are supported. If supplied, this overrides `log_x`. #' #' @return A ggplot2 object, unless `return_data = TRUE`, in which case a data -#' frame with the four variables 'w', 'value', 'Species', 'Legend' is -#' returned. +#' frame with the four variables 'w' (or 'l' if `size_axis = "l"`), 'value', +#' 'Species', 'Legend' is returned. #' @export #' @family plotting functions #' @seealso [plotSpectra()] @@ -1019,7 +1095,9 @@ plotCDF.MizerSim <- function(object, species = NULL, background = TRUE, highlight = NULL, normalise = TRUE, log_x = TRUE, log = NULL, + size_axis = c("w", "l"), return_data = FALSE, ...) { + size_axis <- plot_size_axis(size_axis) if (missing(power)) { power <- as.numeric(biomass) } @@ -1035,6 +1113,7 @@ plotCDF.MizerSim <- function(object, species = NULL, wlim = wlim, ylim = c(NA, NA), power = power, total = total, resource = resource, background = background, + size_axis = "w", return_data = TRUE) if (!missing(time_range)) { args$time_range <- time_range @@ -1042,7 +1121,8 @@ plotCDF.MizerSim <- function(object, species = NULL, plot_dat <- do.call(plotSpectra, args) plot_cdf(plot_dat, object@params, power = power, normalise = normalise, log_x = log_x, wlim = wlim, ylim = ylim, - highlight = highlight, return_data = return_data) + highlight = highlight, size_axis = size_axis, + return_data = return_data) } #' @rdname plotCDF @@ -1054,7 +1134,9 @@ plotCDF.MizerParams <- function(object, species = NULL, background = TRUE, highlight = NULL, normalise = TRUE, log_x = TRUE, log = NULL, + size_axis = c("w", "l"), return_data = FALSE, ...) { + size_axis <- plot_size_axis(size_axis) if (missing(power)) { power <- as.numeric(biomass) } @@ -1069,23 +1151,28 @@ plotCDF.MizerParams <- function(object, species = NULL, wlim = wlim, ylim = c(NA, NA), power = power, total = total, resource = resource, background = background, + size_axis = "w", return_data = TRUE) plot_cdf(plot_dat, object, power = power, normalise = normalise, log_x = log_x, wlim = wlim, ylim = ylim, - highlight = highlight, return_data = return_data) + highlight = highlight, size_axis = size_axis, + return_data = return_data) } plot_cdf <- function(plot_dat, params, power, normalise, log_x, wlim, ylim, - highlight, return_data) { + highlight, size_axis, return_data) { + size_axis <- plot_size_axis(size_axis) cdf_dat <- prepare_spectra_cdf_data(plot_dat, params, normalise = normalise) + cdf_dat <- convert_plot_size_axis(cdf_dat, params, size_axis) if (return_data) return(cdf_dat) plotDataFrame(cdf_dat, validParams(params), - xlab = "Size [g]", ylab = cdf_y_label(power, normalise), + xlab = plot_size_xlab(size_axis), + ylab = cdf_y_label(power, normalise), xtrans = if (log_x) "log10" else "identity", ytrans = "identity", - xlim = wlim, ylim = ylim, + xlim = plot_size_xlim(wlim, size_axis), ylim = ylim, highlight = highlight, legend_var = "Legend") } @@ -1170,7 +1257,8 @@ parsePlotCDFLog <- function(log, log_x) { #' } plotCDF2 <- function(object1, object2, name1 = "First", name2 = "Second", power = 1, normalise = TRUE, log_x = TRUE, log = NULL, - resource = FALSE, ...) { + resource = FALSE, size_axis = c("w", "l"), ...) { + size_axis <- plot_size_axis(size_axis) log_x <- parsePlotCDFLog(log, log_x) assert_that(is.number(power), is.flag(normalise)) @@ -1179,19 +1267,22 @@ plotCDF2 <- function(object1, object2, name1 = "First", name2 = "Second", ylim <- args$ylim %||% c(NA, NA) cf1 <- plotCDF(object1, power = power, normalise = normalise, + size_axis = "w", return_data = TRUE, ...) cf2 <- plotCDF(object2, power = power, normalise = normalise, + size_axis = "w", return_data = TRUE, ...) params <- if (is(object1, "MizerSim")) object1@params else object1 plotComparisonDataFrame(cf1, cf2, validParams(params), name1 = name1, name2 = name2, - xlab = "Size [g]", + xlab = plot_size_xlab(size_axis), ylab = cdf_y_label(power, normalise), xtrans = if (log_x) "log10" else "identity", ytrans = "identity", - xlim = wlim, ylim = ylim, - legend_var = "Legend") + xlim = plot_size_xlim(wlim, size_axis), + ylim = ylim, legend_var = "Legend", + size_axis = size_axis) } #' Compare two size spectra in the same plot @@ -1225,7 +1316,8 @@ plotCDF2 <- function(object1, object2, name1 = "First", name2 = "Second", #' } plotSpectra2 <- function(object1, object2, name1 = "First", name2 = "Second", power = 1, log_x = TRUE, log_y = TRUE, - log = NULL, ...) { + log = NULL, size_axis = c("w", "l"), ...) { + size_axis <- plot_size_axis(size_axis) log_axes <- parsePlotLog(log, log_x = log_x, log_y = log_y) log_x <- log_axes$log_x log_y <- log_axes$log_y @@ -1234,18 +1326,21 @@ plotSpectra2 <- function(object1, object2, name1 = "First", name2 = "Second", wlim <- args$wlim %||% c(NA, NA) ylim <- args$ylim %||% c(NA, NA) - sf1 <- plotSpectra(object1, power = power, return_data = TRUE, ...) - sf2 <- plotSpectra(object2, power = power, return_data = TRUE, ...) + sf1 <- plotSpectra(object1, power = power, size_axis = "w", + return_data = TRUE, ...) + sf2 <- plotSpectra(object2, power = power, size_axis = "w", + return_data = TRUE, ...) params <- if (is(object1, "MizerSim")) object1@params else object1 plotComparisonDataFrame(sf1, sf2, validParams(params), name1 = name1, name2 = name2, - xlab = "Size [g]", + xlab = plot_size_xlab(size_axis), ylab = spectra_y_label(power), xtrans = if (log_x) "log10" else "identity", ytrans = if (log_y) "log10" else "identity", - xlim = wlim, ylim = ylim, - legend_var = "Legend") + xlim = plot_size_xlim(wlim, size_axis), + ylim = ylim, legend_var = "Legend", + size_axis = size_axis) } spectra_y_label <- function(power) { @@ -1261,11 +1356,14 @@ spectra_y_label <- function(power) { #' @export plotlySpectra2 <- function(object1, object2, name1 = "First", name2 = "Second", power = 1, - log_x = TRUE, log_y = TRUE, log = NULL, ...) { + log_x = TRUE, log_y = TRUE, log = NULL, + size_axis = c("w", "l"), ...) { + size_axis <- plot_size_axis(size_axis) ggplotly(plotSpectra2(object1, object2, name1 = name1, name2 = name2, power = power, log_x = log_x, log_y = log_y, - log = log, ...), - tooltip = c("Species", "w", "value", "Model")) + log = log, size_axis = size_axis, ...), + tooltip = plot_size_tooltip(size_axis, before = "Species", + after = c("value", "Model"))) } #' Plot the relative difference between two spectra @@ -1292,6 +1390,8 @@ plotlySpectra2 <- function(object1, object2, name1 = "First", #' @param ylim A numeric vector of length two providing lower and upper limits #' for the relative difference (y) axis. Use `NA` to refer to the existing #' minimum or maximum. +#' @param size_axis Whether to plot size as weight (`"w"`, default) or length +#' (`"l"`), using the allometric weight-length relationship. #' @param ... Arguments passed to [plotSpectra()] for preparing the spectra #' data, for example `species`, `time_range`, `wlim`, `resource`, #' `background` or `total`. @@ -1307,28 +1407,33 @@ plotlySpectra2 <- function(object1, object2, name1 = "First", #' plotSpectraRelative(sim1, sim2) #' } plotSpectraRelative <- function(object1, object2, log_x = TRUE, - ylim = c(NA, NA), ...) { + ylim = c(NA, NA), + size_axis = c("w", "l"), ...) { + size_axis <- plot_size_axis(size_axis) args <- list(...) wlim <- args$wlim %||% c(NA, NA) - sf1 <- plotSpectra(object1, return_data = TRUE, ...) - sf2 <- plotSpectra(object2, return_data = TRUE, ...) + sf1 <- plotSpectra(object1, size_axis = "w", return_data = TRUE, ...) + sf2 <- plotSpectra(object2, size_axis = "w", return_data = TRUE, ...) params <- if (is(object1, "MizerSim")) object1@params else object1 plotRelativeDataFrame(sf1, sf2, validParams(params), - xlab = "Size [g]", + xlab = plot_size_xlab(size_axis), xtrans = if (log_x) "log10" else "identity", - xlim = wlim, ylim = ylim, - legend_var = "Legend") + xlim = plot_size_xlim(wlim, size_axis), ylim = ylim, + legend_var = "Legend", size_axis = size_axis) } #' @rdname plotSpectraRelative #' @export plotlySpectraRelative <- function(object1, object2, log_x = TRUE, - ylim = c(NA, NA), ...) { + ylim = c(NA, NA), + size_axis = c("w", "l"), ...) { + size_axis <- plot_size_axis(size_axis) ggplotly(plotSpectraRelative(object1, object2, log_x = log_x, - ylim = ylim, ...), - tooltip = c("Legend", "w", "rel_diff")) + ylim = ylim, size_axis = size_axis, ...), + tooltip = plot_size_tooltip(size_axis, before = "Legend", + after = "rel_diff")) } #' @rdname plotCDF @@ -1341,14 +1446,16 @@ plotlyCDF <- function(object, species = NULL, total = FALSE, resource = FALSE, background = TRUE, highlight = NULL, normalise = TRUE, - log_x = TRUE, log = NULL, ...) { + log_x = TRUE, log = NULL, + size_axis = c("w", "l"), ...) { + size_axis <- plot_size_axis(size_axis) args <- list(object = object, species = species, geometric_mean = geometric_mean, wlim = wlim, ylim = ylim, biomass = biomass, total = total, resource = resource, background = background, highlight = highlight, normalise = normalise, - log_x = log_x, log = log, ...) + log_x = log_x, log = log, size_axis = size_axis, ...) if (!missing(time_range)) { args$time_range <- time_range } @@ -1356,7 +1463,8 @@ plotlyCDF <- function(object, species = NULL, args$power <- power } ggplotly(do.call("plotCDF", args), - tooltip = c("Species", "w", "value")) + tooltip = plot_size_tooltip(size_axis, before = "Species", + after = "value")) } #' @rdname plotCDF2 @@ -1364,15 +1472,19 @@ plotlyCDF <- function(object, species = NULL, #' @export plotlyCDF2 <- function(object1, object2, name1 = "First", name2 = "Second", power = 1, normalise = TRUE, - log_x = TRUE, log = NULL, resource = FALSE, ...) { + log_x = TRUE, log = NULL, resource = FALSE, + size_axis = c("w", "l"), ...) { + size_axis <- plot_size_axis(size_axis) args <- list(object1 = object1, object2 = object2, name1 = name1, name2 = name2, - normalise = normalise, log_x = log_x, log = log, ...) + normalise = normalise, log_x = log_x, log = log, + size_axis = size_axis, ...) if (!missing(power)) { args$power <- power } ggplotly(do.call("plotCDF2", args), - tooltip = c("Species", "w", "value", "Model")) + tooltip = plot_size_tooltip(size_axis, before = "Species", + after = c("value", "Model"))) } #' @rdname plotSpectra @@ -1384,10 +1496,12 @@ plotlySpectra <- function(object, species = NULL, total = FALSE, resource = TRUE, background = TRUE, highlight = NULL, log_x = TRUE, log_y = TRUE, - log = NULL, ...) { + log = NULL, size_axis = c("w", "l"), ...) { + size_axis <- plot_size_axis(size_axis) argg <- as.list(environment()) ggplotly(do.call("plotSpectra", argg), - tooltip = c("Species", "w", "value")) + tooltip = plot_size_tooltip(size_axis, before = "Species", + after = "value")) } #' Plot the feeding level of species by size @@ -1414,9 +1528,10 @@ plotlySpectra <- function(object, species = NULL, #' plotted. Default FALSE. #' #' @return A ggplot2 object, unless `return_data = TRUE`, in which case a data -#' frame with the variables 'w', 'value' and 'Species' is returned. If also -#' `include_critical = TRUE` then the data frame contains a fourth variable -#' 'Type' that distinguishes between 'actual' and 'critical' feeding level. +#' frame with the variables 'w' (or 'l' if `size_axis = "l"`), 'value' and +#' 'Species' is returned. If also `include_critical = TRUE` then the data +#' frame contains a fourth variable 'Type' that distinguishes between +#' 'actual' and 'critical' feeding level. #' @export #' @family plotting functions #' @seealso [plotting_functions], [getFeedingLevel()] @@ -1443,7 +1558,9 @@ plotFeedingLevel <- function(object, ...) { plotFeedingLevel.MizerSim <- function(object, species = NULL, time_range, highlight = NULL, all.sizes = FALSE, include_critical = FALSE, + size_axis = c("w", "l"), return_data = FALSE, ...) { + size_axis <- plot_size_axis(size_axis) assert_that(is.flag(all.sizes), is.flag(include_critical), is.flag(return_data)) @@ -1459,6 +1576,7 @@ plotFeedingLevel.MizerSim <- function(object, species = NULL, plot_feeding_level(params, feed, species = species, highlight = highlight, all.sizes = all.sizes, include_critical = include_critical, + size_axis = size_axis, return_data = return_data) } @@ -1467,7 +1585,9 @@ plotFeedingLevel.MizerSim <- function(object, species = NULL, plotFeedingLevel.MizerParams <- function(object, species = NULL, highlight = NULL, all.sizes = FALSE, include_critical = FALSE, + size_axis = c("w", "l"), return_data = FALSE, ...) { + size_axis <- plot_size_axis(size_axis) assert_that(is.flag(all.sizes), is.flag(include_critical), is.flag(return_data)) @@ -1476,12 +1596,14 @@ plotFeedingLevel.MizerParams <- function(object, species = NULL, plot_feeding_level(params, feed, species = species, highlight = highlight, all.sizes = all.sizes, include_critical = include_critical, + size_axis = size_axis, return_data = return_data) } plot_feeding_level <- function(params, feed, species, highlight, all.sizes, include_critical, - return_data) { + size_axis, return_data) { + size_axis <- plot_size_axis(size_axis) # selector for desired species sel_sp <- valid_species_arg(params, species, return.logical = TRUE, @@ -1513,8 +1635,10 @@ plot_feeding_level <- function(params, feed, species, highlight, } plot_dat <- plot_dat[complete.cases(plot_dat), ] } + plot_dat <- convert_plot_size_axis(plot_dat, params, size_axis) if (return_data) return(plot_dat) + x_var <- plot_size_x_var(size_axis) # Need to keep species in order for legend legend_levels <- @@ -1539,9 +1663,9 @@ plot_feeding_level <- function(params, feed, species, highlight, } else { p <- ggplot(plot_dat, aes(group = Species)) } - p + geom_line(aes(x = w, y = value, + p + geom_line(aes(x = .data[[x_var]], y = value, colour = Legend, linetype = Legend, linewidth = Legend)) + - scale_x_continuous(name = "Size [g]", trans = "log10") + + scale_x_continuous(name = plot_size_xlab(size_axis), trans = "log10") + scale_y_continuous(name = "Feeding Level") + coord_cartesian(ylim = c(0, 1)) + scale_colour_manual(values = params@linecolour[legend_levels]) + @@ -1555,10 +1679,13 @@ plotlyFeedingLevel <- function(object, species = NULL, time_range, highlight = NULL, - include_critical = FALSE, ...) { + include_critical = FALSE, + size_axis = c("w", "l"), ...) { + size_axis <- plot_size_axis(size_axis) argg <- as.list(environment()) p <- ggplotly(do.call("plotFeedingLevel", argg), - tooltip = c("Species", "w", "value")) + tooltip = plot_size_tooltip(size_axis, before = "Species", + after = "value")) # When critical feeding level is included, ggplotly creates traces split by the # interaction of Species and Type, which produces a very long combined legend. @@ -1638,7 +1765,8 @@ plotlyFeedingLevel <- function(object, #' outside a species' size range. Default FALSE. #' #' @return A ggplot2 object, unless `return_data = TRUE`, in which case a data -#' frame with the three variables 'w', 'value', 'Species' is returned. +#' frame with the three variables 'w' (or 'l' if `size_axis = "l"`), 'value', +#' 'Species' is returned. #' @export #' @family plotting functions #' @seealso [plotting_functions], [getPredMort()] @@ -1663,8 +1791,10 @@ plotPredMort <- function(object, ...) { #' @export plotPredMort.MizerSim <- function(object, species = NULL, time_range, all.sizes = FALSE, - highlight = NULL, return_data = FALSE, + highlight = NULL, size_axis = c("w", "l"), + return_data = FALSE, ...) { + size_axis <- plot_size_axis(size_axis) if (missing(time_range)) { time_range <- max(as.numeric(dimnames(object@n)$time)) } @@ -1677,7 +1807,8 @@ plotPredMort.MizerSim <- function(object, species = NULL, units = "1/year", params = object@params) plot(pred_mort, species = species, all.sizes = all.sizes, - highlight = highlight, return_data = return_data, + highlight = highlight, size_axis = size_axis, + return_data = return_data, ylim = c(0, NA)) } @@ -1685,10 +1816,12 @@ plotPredMort.MizerSim <- function(object, species = NULL, #' @export plotPredMort.MizerParams <- function(object, species = NULL, all.sizes = FALSE, - highlight = NULL, return_data = FALSE, + highlight = NULL, size_axis = c("w", "l"), + return_data = FALSE, ...) { + size_axis <- plot_size_axis(size_axis) plot(getPredMort(validParams(object)), species = species, - all.sizes = all.sizes, highlight = highlight, + all.sizes = all.sizes, highlight = highlight, size_axis = size_axis, return_data = return_data, ylim = c(0, NA)) } @@ -1705,10 +1838,12 @@ plotM2 <- plotPredMort #' @export plotlyPredMort <- function(object, species = NULL, time_range, - highlight = NULL, ...) { + highlight = NULL, size_axis = c("w", "l"), ...) { + size_axis <- plot_size_axis(size_axis) argg <- as.list(environment()) ggplotly(do.call("plotPredMort", argg), - tooltip = c("Species", "w", "value")) + tooltip = plot_size_tooltip(size_axis, before = "Species", + after = "value")) } #' Plot total fishing mortality of each species by size @@ -1722,7 +1857,8 @@ plotlyPredMort <- function(object, species = NULL, #' @param all.sizes If TRUE, then fishing mortality is plotted also for sizes #' outside a species' size range. Default FALSE. #' @return A ggplot2 object, unless `return_data = TRUE`, in which case a data -#' frame with the three variables 'w', 'value', 'Species' is returned. +#' frame with the three variables 'w' (or 'l' if `size_axis = "l"`), 'value', +#' 'Species' is returned. #' @export #' @family plotting functions #' @seealso [plotting_functions], [getFMort()] @@ -1747,8 +1883,10 @@ plotFMort <- function(object, ...) { #' @export plotFMort.MizerSim <- function(object, species = NULL, time_range, all.sizes = FALSE, - highlight = NULL, return_data = FALSE, + highlight = NULL, size_axis = c("w", "l"), + return_data = FALSE, ...) { + size_axis <- plot_size_axis(size_axis) if (missing(time_range)) { time_range <- max(as.numeric(dimnames(object@n)$time)) } @@ -1759,17 +1897,20 @@ plotFMort.MizerSim <- function(object, species = NULL, f <- ArraySpeciesBySize(f, value_name = "Fishing mortality", units = "1/year", params = object@params) plot(f, species = species, all.sizes = all.sizes, - highlight = highlight, return_data = return_data) + highlight = highlight, size_axis = size_axis, + return_data = return_data) } #' @rdname plotFMort #' @export plotFMort.MizerParams <- function(object, species = NULL, all.sizes = FALSE, - highlight = NULL, return_data = FALSE, + highlight = NULL, size_axis = c("w", "l"), + return_data = FALSE, ...) { + size_axis <- plot_size_axis(size_axis) plot(getFMort(validParams(object)), species = species, - all.sizes = all.sizes, highlight = highlight, + all.sizes = all.sizes, highlight = highlight, size_axis = size_axis, return_data = return_data) } @@ -1777,10 +1918,12 @@ plotFMort.MizerParams <- function(object, species = NULL, #' @export plotlyFMort <- function(object, species = NULL, time_range, - highlight = NULL, ...) { + highlight = NULL, size_axis = c("w", "l"), ...) { + size_axis <- plot_size_axis(size_axis) argg <- as.list(environment()) ggplotly(do.call("plotFMort", argg), - tooltip = c("Species", "w", "value")) + tooltip = plot_size_tooltip(size_axis, before = "Species", + after = "value")) } @@ -2052,8 +2195,8 @@ plotlyGrowthCurves <- function(object, species = NULL, #' @inheritParams plotSpectra #' #' @return A ggplot2 object, unless `return_data = TRUE`, in which case a data -#' frame with the four variables 'Predator', 'w', 'Proportion', 'Prey' is -#' returned. +#' frame with the four variables 'Predator', 'w' (or 'l' if +#' `size_axis = "l"`), 'Proportion', 'Prey' is returned. #' @export #' @seealso [getDiet()] #' @family plotting functions @@ -2075,21 +2218,28 @@ plotDiet <- function(object, ...) { #' @rdname plotDiet #' @export plotDiet.MizerSim <- function(object, species = NULL, + size_axis = c("w", "l"), return_data = FALSE, ...) { - plotDiet(object@params, species = species, return_data = return_data, ...) + size_axis <- plot_size_axis(size_axis) + plotDiet(object@params, species = species, size_axis = size_axis, + return_data = return_data, ...) } #' @rdname plotDiet #' @export -plotDiet.MizerParams <- function(object, species = NULL, return_data = FALSE, ...) { +plotDiet.MizerParams <- function(object, species = NULL, + size_axis = c("w", "l"), + return_data = FALSE, ...) { + size_axis <- plot_size_axis(size_axis) assert_that(is.flag(return_data)) params <- validParams(object) diet <- getDiet(params) plot_diet(params, n = params@initial_n, diet = diet, species = species, - return_data = return_data) + size_axis = size_axis, return_data = return_data) } -plot_diet <- function(params, n, diet, species, return_data) { +plot_diet <- function(params, n, diet, species, size_axis, return_data) { + size_axis <- plot_size_axis(size_axis) species <- valid_species_arg(params, species, return.logical = TRUE) diet <- diet[species, , , drop = FALSE] names(dimnames(diet)) <- c("Predator", "w", "Prey") @@ -2120,14 +2270,17 @@ plot_diet <- function(params, n, diet, species, return_data) { } } + plot_dat <- convert_plot_size_axis(plot_dat, params, size_axis, + species_col = "Predator") if (return_data) return(plot_dat) + x_var <- plot_size_x_var(size_axis) legend_levels <- intersect(names(params@linecolour), plot_dat$Prey) p <- ggplot(plot_dat) + - geom_area(aes(x = w, y = Proportion, fill = Prey)) + + geom_area(aes(x = .data[[x_var]], y = Proportion, fill = Prey)) + scale_x_log10() + - labs(x = "Size [g]", y = "Proportion") + + labs(x = plot_size_xlab(size_axis), y = "Proportion") + scale_fill_manual(values = params@linecolour[legend_levels], limits = legend_levels) if (sum(species) > 1) { @@ -2139,9 +2292,12 @@ plot_diet <- function(params, n, diet, species, return_data) { #' @rdname plotDiet #' @return `plotlyDiet()` returns a plotly object. #' @export -plotlyDiet <- function(object, species = NULL, ...) { - ggplotly(plotDiet(object, species = species, ...), - tooltip = c("Predator", "w", "Proportion", "Prey")) +plotlyDiet <- function(object, species = NULL, + size_axis = c("w", "l"), ...) { + size_axis <- plot_size_axis(size_axis) + ggplotly(plotDiet(object, species = species, size_axis = size_axis, ...), + tooltip = plot_size_tooltip(size_axis, before = "Predator", + after = c("Proportion", "Prey"))) } diff --git a/man/addPlot.Rd b/man/addPlot.Rd index 602a34132..74a0d53ac 100644 --- a/man/addPlot.Rd +++ b/man/addPlot.Rd @@ -15,6 +15,7 @@ addPlot(plot, x, ...) species = NULL, all.sizes = FALSE, wlim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, colour = NULL, @@ -57,6 +58,9 @@ all species.} for the weight (x) axis. Use \code{NA} to refer to the existing minimum or maximum. Only applies to \code{ArraySpeciesBySize}.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{total}{A boolean value that determines whether the total over all selected species is plotted as well. Default is \code{FALSE}.} diff --git a/man/animate.Rd b/man/animate.Rd index 7e547049e..a44dbb147 100644 --- a/man/animate.Rd +++ b/man/animate.Rd @@ -16,6 +16,7 @@ wlim = c(NA, NA), ylim = c(NA, NA), log_y = TRUE, + size_axis = c("w", "l"), total = FALSE, background = TRUE, frame_duration = 500, @@ -34,6 +35,7 @@ animate(x, ...) log_y = TRUE, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), power = 1, total = FALSE, resource = TRUE, @@ -70,6 +72,9 @@ frames.} \item{log_y}{If \code{TRUE} (default), use a log10 y-axis.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{total}{A boolean value that determines whether the total over all selected species is plotted as an additional trace called \code{"Total"}. Default is \code{FALSE}.} diff --git a/man/plot.Rd b/man/plot.Rd index 834ebffd6..04914ab8d 100644 --- a/man/plot.Rd +++ b/man/plot.Rd @@ -22,6 +22,7 @@ log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, @@ -81,6 +82,7 @@ log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, @@ -130,6 +132,9 @@ maximum. Only applies to \code{ArraySpeciesBySize}.} for the value (y) axis. Use \code{NA} to refer to the existing minimum or maximum.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{total}{A boolean value that determines whether the total over all selected species is plotted as well. Default is \code{FALSE}.} @@ -180,6 +185,8 @@ coloured appropriately. \donttest{ plot(getEncounter(NS_params)) plot(getFeedingLevel(NS_params), species = c("Cod", "Herring")) +plot(getPredMort(NS_params), species = c("Cod", "Herring"), + size_axis = "l") } \donttest{ ggplotly(getEncounter(NS_params)) diff --git a/man/plot2.Rd b/man/plot2.Rd index bd99b0525..2f2b73d75 100644 --- a/man/plot2.Rd +++ b/man/plot2.Rd @@ -22,6 +22,7 @@ plot2(x, y, ...) log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, @@ -59,6 +60,7 @@ plot2(x, y, ...) log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, @@ -96,6 +98,9 @@ maximum. Only applies to \code{ArraySpeciesBySize}.} for the value (y) axis. Use \code{NA} to refer to the existing minimum or maximum.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{total}{A boolean value that determines whether the total over all selected species is plotted as well. Default is \code{FALSE}.} diff --git a/man/plotCDF.Rd b/man/plotCDF.Rd index 043b2ae46..aa78ec21c 100644 --- a/man/plotCDF.Rd +++ b/man/plotCDF.Rd @@ -25,6 +25,7 @@ plotCDF(object, ...) normalise = TRUE, log_x = TRUE, log = NULL, + size_axis = c("w", "l"), return_data = FALSE, ... ) @@ -43,6 +44,7 @@ plotCDF(object, ...) normalise = TRUE, log_x = TRUE, log = NULL, + size_axis = c("w", "l"), return_data = FALSE, ... ) @@ -63,6 +65,7 @@ plotlyCDF( normalise = TRUE, log_x = TRUE, log = NULL, + size_axis = c("w", "l"), ... ) } @@ -132,13 +135,16 @@ integral.} scale, in the same form as the base \code{\link[=plot]{plot()}} argument. For \code{plotCDF()}, only \code{"x"} and \code{""} are supported. If supplied, this overrides \code{log_x}.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{return_data}{A boolean value that determines whether the formatted data used for the plot is returned instead of the plot itself. Default value is FALSE} } \value{ A ggplot2 object, unless \code{return_data = TRUE}, in which case a data -frame with the four variables 'w', 'value', 'Species', 'Legend' is -returned. +frame with the four variables 'w' (or 'l' if \code{size_axis = "l"}), 'value', +'Species', 'Legend' is returned. \code{plotlyCDF()} returns a plotly object. } diff --git a/man/plotCDF2.Rd b/man/plotCDF2.Rd index 5224e814a..6660f2051 100644 --- a/man/plotCDF2.Rd +++ b/man/plotCDF2.Rd @@ -15,6 +15,7 @@ plotCDF2( log_x = TRUE, log = NULL, resource = FALSE, + size_axis = c("w", "l"), ... ) @@ -28,6 +29,7 @@ plotlyCDF2( log_x = TRUE, log = NULL, resource = FALSE, + size_axis = c("w", "l"), ... ) } @@ -56,6 +58,9 @@ only \code{"x"} and \code{""} are supported. If supplied, this overrides \code{l \item{resource}{A boolean value that determines whether resource is included. Default is FALSE.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{...}{Arguments passed to \code{\link[=plotCDF]{plotCDF()}} for preparing the cumulative distribution data, for example \code{species}, \code{time_range}, \code{wlim}, \code{resource}, \code{background} or \code{total}.} diff --git a/man/plotDiet.Rd b/man/plotDiet.Rd index c6aeb3d55..200e34696 100644 --- a/man/plotDiet.Rd +++ b/man/plotDiet.Rd @@ -9,11 +9,23 @@ \usage{ plotDiet(object, ...) -\method{plotDiet}{MizerSim}(object, species = NULL, return_data = FALSE, ...) +\method{plotDiet}{MizerSim}( + object, + species = NULL, + size_axis = c("w", "l"), + return_data = FALSE, + ... +) -\method{plotDiet}{MizerParams}(object, species = NULL, return_data = FALSE, ...) +\method{plotDiet}{MizerParams}( + object, + species = NULL, + size_axis = c("w", "l"), + return_data = FALSE, + ... +) -plotlyDiet(object, species = NULL, ...) +plotlyDiet(object, species = NULL, size_axis = c("w", "l"), ...) } \arguments{ \item{object}{An object of class \linkS4class{MizerSim} or @@ -26,13 +38,16 @@ species are selected. A vector of species names, or a numeric vector with the species indices, or a logical vector indicating for each species whether it is to be selected (TRUE) or not.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{return_data}{A boolean value that determines whether the formatted data used for the plot is returned instead of the plot itself. Default value is FALSE} } \value{ A ggplot2 object, unless \code{return_data = TRUE}, in which case a data -frame with the four variables 'Predator', 'w', 'Proportion', 'Prey' is -returned. +frame with the four variables 'Predator', 'w' (or 'l' if +\code{size_axis = "l"}), 'Proportion', 'Prey' is returned. \code{plotlyDiet()} returns a plotly object. } diff --git a/man/plotFMort.Rd b/man/plotFMort.Rd index af1c0f968..f35e860a9 100644 --- a/man/plotFMort.Rd +++ b/man/plotFMort.Rd @@ -15,6 +15,7 @@ plotFMort(object, ...) time_range, all.sizes = FALSE, highlight = NULL, + size_axis = c("w", "l"), return_data = FALSE, ... ) @@ -24,11 +25,19 @@ plotFMort(object, ...) species = NULL, all.sizes = FALSE, highlight = NULL, + size_axis = c("w", "l"), return_data = FALSE, ... ) -plotlyFMort(object, species = NULL, time_range, highlight = NULL, ...) +plotlyFMort( + object, + species = NULL, + time_range, + highlight = NULL, + size_axis = c("w", "l"), + ... +) } \arguments{ \item{object}{An object of class \linkS4class{MizerSim} or @@ -51,12 +60,16 @@ outside a species' size range. Default FALSE.} \item{highlight}{Name or vector of names of the species to be highlighted.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{return_data}{A boolean value that determines whether the formatted data used for the plot is returned instead of the plot itself. Default value is FALSE} } \value{ A ggplot2 object, unless \code{return_data = TRUE}, in which case a data -frame with the three variables 'w', 'value', 'Species' is returned. +frame with the three variables 'w' (or 'l' if \code{size_axis = "l"}), 'value', +'Species' is returned. } \description{ After running a projection, plot the total fishing mortality of each species diff --git a/man/plotFeedingLevel.Rd b/man/plotFeedingLevel.Rd index fecafb388..0d8397a6f 100644 --- a/man/plotFeedingLevel.Rd +++ b/man/plotFeedingLevel.Rd @@ -16,6 +16,7 @@ plotFeedingLevel(object, ...) highlight = NULL, all.sizes = FALSE, include_critical = FALSE, + size_axis = c("w", "l"), return_data = FALSE, ... ) @@ -26,6 +27,7 @@ plotFeedingLevel(object, ...) highlight = NULL, all.sizes = FALSE, include_critical = FALSE, + size_axis = c("w", "l"), return_data = FALSE, ... ) @@ -36,6 +38,7 @@ plotlyFeedingLevel( time_range, highlight = NULL, include_critical = FALSE, + size_axis = c("w", "l"), ... ) } @@ -63,14 +66,18 @@ outside a species' size range. Default FALSE.} \item{include_critical}{If TRUE, then the critical feeding level is also plotted. Default FALSE.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{return_data}{A boolean value that determines whether the formatted data used for the plot is returned instead of the plot itself. Default value is FALSE} } \value{ A ggplot2 object, unless \code{return_data = TRUE}, in which case a data -frame with the variables 'w', 'value' and 'Species' is returned. If also -\code{include_critical = TRUE} then the data frame contains a fourth variable -'Type' that distinguishes between 'actual' and 'critical' feeding level. +frame with the variables 'w' (or 'l' if \code{size_axis = "l"}), 'value' and +'Species' is returned. If also \code{include_critical = TRUE} then the data +frame contains a fourth variable 'Type' that distinguishes between +'actual' and 'critical' feeding level. } \description{ After running a projection, plot the feeding level of each species by size. diff --git a/man/plotM2.Rd b/man/plotM2.Rd index a59aeb884..64ead598e 100644 --- a/man/plotM2.Rd +++ b/man/plotM2.Rd @@ -14,7 +14,8 @@ plotM2(object, ...) } \value{ A ggplot2 object, unless \code{return_data = TRUE}, in which case a data -frame with the three variables 'w', 'value', 'Species' is returned. +frame with the three variables 'w' (or 'l' if \code{size_axis = "l"}), 'value', +'Species' is returned. } \description{ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} diff --git a/man/plotPredMort.Rd b/man/plotPredMort.Rd index 5f8bd1050..fa12bf73d 100644 --- a/man/plotPredMort.Rd +++ b/man/plotPredMort.Rd @@ -15,6 +15,7 @@ plotPredMort(object, ...) time_range, all.sizes = FALSE, highlight = NULL, + size_axis = c("w", "l"), return_data = FALSE, ... ) @@ -24,11 +25,19 @@ plotPredMort(object, ...) species = NULL, all.sizes = FALSE, highlight = NULL, + size_axis = c("w", "l"), return_data = FALSE, ... ) -plotlyPredMort(object, species = NULL, time_range, highlight = NULL, ...) +plotlyPredMort( + object, + species = NULL, + time_range, + highlight = NULL, + size_axis = c("w", "l"), + ... +) } \arguments{ \item{object}{An object of class \linkS4class{MizerSim} or @@ -51,12 +60,16 @@ outside a species' size range. Default FALSE.} \item{highlight}{Name or vector of names of the species to be highlighted.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{return_data}{A boolean value that determines whether the formatted data used for the plot is returned instead of the plot itself. Default value is FALSE} } \value{ A ggplot2 object, unless \code{return_data = TRUE}, in which case a data -frame with the three variables 'w', 'value', 'Species' is returned. +frame with the three variables 'w' (or 'l' if \code{size_axis = "l"}), 'value', +'Species' is returned. } \description{ After running a projection, plot the predation mortality rate of each species diff --git a/man/plotRelative.Rd b/man/plotRelative.Rd index 75de88b66..1e555f38f 100644 --- a/man/plotRelative.Rd +++ b/man/plotRelative.Rd @@ -18,6 +18,7 @@ plotRelative(x, y, ...) log_x = TRUE, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, ... @@ -45,6 +46,7 @@ plotRelative(x, y, ...) log_x = TRUE, wlim = c(NA, NA), ylim = c(NA, NA), + size_axis = c("w", "l"), total = FALSE, background = TRUE, ... @@ -72,6 +74,9 @@ maximum. Only applies to \code{ArraySpeciesBySize}.} for the value (y) axis. Use \code{NA} to refer to the existing minimum or maximum.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{total}{A boolean value that determines whether the total over all selected species is plotted as well. Default is \code{FALSE}.} diff --git a/man/plotSpectra.Rd b/man/plotSpectra.Rd index e296df713..621884554 100644 --- a/man/plotSpectra.Rd +++ b/man/plotSpectra.Rd @@ -25,6 +25,7 @@ plotSpectra(object, ...) log_x = TRUE, log_y = TRUE, log = NULL, + size_axis = c("w", "l"), return_data = FALSE, ... ) @@ -43,6 +44,7 @@ plotSpectra(object, ...) log_x = TRUE, log_y = TRUE, log = NULL, + size_axis = c("w", "l"), return_data = FALSE, ... ) @@ -63,6 +65,7 @@ plotlySpectra( log_x = TRUE, log_y = TRUE, log = NULL, + size_axis = c("w", "l"), ... ) } @@ -130,13 +133,16 @@ Default is TRUE.} in the same form as the base \code{\link[=plot]{plot()}} argument. For example, \code{"x"}, \code{"y"}, \code{"xy"} or \code{""}. If supplied, this overrides \code{log_x} and \code{log_y}.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{return_data}{A boolean value that determines whether the formatted data used for the plot is returned instead of the plot itself. Default value is FALSE} } \value{ A ggplot2 object, unless \code{return_data = TRUE}, in which case a data -frame with the four variables 'w', 'value', 'Species', 'Legend' is -returned. +frame with the four variables 'w' (or 'l' if \code{size_axis = "l"}), 'value', +'Species', 'Legend' is returned. } \description{ Plots the number density multiplied by a power of the weight, with the power @@ -157,6 +163,7 @@ plotSpectra(sim, wlim = c(1e-6, NA)) plotSpectra(sim, time_range = 10:20) plotSpectra(sim, time_range = 10:20, power = 0) plotSpectra(sim, species = c("Cod", "Herring"), power = 1) +plotSpectra(sim, species = c("Cod", "Herring"), size_axis = "l") # Returning the data frame fr <- plotSpectra(sim, return_data = TRUE) diff --git a/man/plotSpectra2.Rd b/man/plotSpectra2.Rd index 4d52bed1a..3fcfd0bcc 100644 --- a/man/plotSpectra2.Rd +++ b/man/plotSpectra2.Rd @@ -14,6 +14,7 @@ plotSpectra2( log_x = TRUE, log_y = TRUE, log = NULL, + size_axis = c("w", "l"), ... ) @@ -26,6 +27,7 @@ plotlySpectra2( log_x = TRUE, log_y = TRUE, log = NULL, + size_axis = c("w", "l"), ... ) } @@ -49,6 +51,9 @@ to logarithmic size bins.} in the same form as the base \code{\link[=plot]{plot()}} argument. For example, \code{"x"}, \code{"y"}, \code{"xy"} or \code{""}. If supplied, this overrides \code{log_x} and \code{log_y}.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{...}{Arguments passed to \code{\link[=plotSpectra]{plotSpectra()}} for preparing the spectra data, for example \code{species}, \code{time_range}, \code{wlim}, \code{ylim}, \code{resource}, \code{background} or \code{total}.} diff --git a/man/plotSpectraRelative.Rd b/man/plotSpectraRelative.Rd index 4325841ce..8c0488c96 100644 --- a/man/plotSpectraRelative.Rd +++ b/man/plotSpectraRelative.Rd @@ -5,9 +5,23 @@ \alias{plotlySpectraRelative} \title{Plot the relative difference between two spectra} \usage{ -plotSpectraRelative(object1, object2, log_x = TRUE, ylim = c(NA, NA), ...) +plotSpectraRelative( + object1, + object2, + log_x = TRUE, + ylim = c(NA, NA), + size_axis = c("w", "l"), + ... +) -plotlySpectraRelative(object1, object2, log_x = TRUE, ylim = c(NA, NA), ...) +plotlySpectraRelative( + object1, + object2, + log_x = TRUE, + ylim = c(NA, NA), + size_axis = c("w", "l"), + ... +) } \arguments{ \item{object1}{First \code{MizerParams} or \code{MizerSim} object.} @@ -20,6 +34,9 @@ plotlySpectraRelative(object1, object2, log_x = TRUE, ylim = c(NA, NA), ...) for the relative difference (y) axis. Use \code{NA} to refer to the existing minimum or maximum.} +\item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length +(\code{"l"}), using the allometric weight-length relationship.} + \item{...}{Arguments passed to \code{\link[=plotSpectra]{plotSpectra()}} for preparing the spectra data, for example \code{species}, \code{time_range}, \code{wlim}, \code{resource}, \code{background} or \code{total}.} diff --git a/tests/testthat/test-plots.R b/tests/testthat/test-plots.R index d1cfb6597..d4aa66b4a 100644 --- a/tests/testthat/test-plots.R +++ b/tests/testthat/test-plots.R @@ -173,6 +173,19 @@ test_that("plotSpectra2 supports base plot log argument", { "`log` must be a character string") }) +test_that("comparison helpers preserve non-size x variables", { + p <- plot2(getBiomass(NS_sim), getBiomass(NS_sim), species = "Cod") + expect_s3_class(p, "ggplot") + expect_true("Year" %in% names(p$data)) + expect_error(ggplot2::ggplot_build(p), NA) + + p_rel <- plotRelative(getBiomass(NS_sim), getBiomass(NS_sim), + species = "Cod") + expect_s3_class(p_rel, "ggplot") + expect_true("Year" %in% names(p_rel$data)) + expect_error(ggplot2::ggplot_build(p_rel), NA) +}) + test_that("plotSpectraRelative plots symmetric relative difference", { params2 <- params params2@initial_n[] <- params@initial_n * 2 @@ -277,6 +290,71 @@ test_that("plotCDF2 compares cumulative distributions", { "only supports log scaling on the x axis") }) +test_that("size-based plots support length axes", { + params_len <- params + params_len@species_params$a <- 0.01 + params_len@species_params$b <- 3 + sim_len <- sim + sim_len@params <- params_len + + spectra_w <- plotSpectra(params_len, species = species, resource = FALSE, + total = FALSE, return_data = TRUE) + spectra_l <- plotSpectra(params_len, species = species, resource = FALSE, + total = FALSE, size_axis = "l", + return_data = TRUE) + expect_true("l" %in% names(spectra_l)) + expect_false("w" %in% names(spectra_l)) + sp_idx <- match(as.character(spectra_w$Species), + as.character(params_len@species_params$species)) + expected_l <- w2l(spectra_w$w, + params_len@species_params[sp_idx, , drop = FALSE]) + expect_equal(spectra_l$l, expected_l) + + spectra_hidden <- plotSpectra(params_len, species = species, + resource = TRUE, total = TRUE, + size_axis = "l", return_data = TRUE) + expect_false(any(spectra_hidden$Legend %in% c("Resource", "Total"))) + + p <- plotSpectra(params_len, species = species, resource = FALSE, + size_axis = "l") + expect_identical(p$scales$get_scales("x")$name, "Length [cm]") + expect_identical(p$scales$get_scales("x")$trans$name, "log-10") + + expect_true("l" %in% names(plotCDF(params_len, species = species, + resource = FALSE, size_axis = "l", + return_data = TRUE))) + expect_true("l" %in% names(plotSpectra2(params_len, params_len, + species = species, + resource = FALSE, + size_axis = "l")$data)) + expect_true("l" %in% names(plotCDF2(params_len, params_len, + species = species, + resource = FALSE, + size_axis = "l")$data)) + expect_true("l" %in% names(plotSpectraRelative(params_len, params_len, + species = species, + resource = FALSE, + size_axis = "l")$data)) + + expect_true("l" %in% names(plotFeedingLevel(params_len, species = species, + size_axis = "l", + return_data = TRUE))) + expect_true("l" %in% names(plotPredMort(params_len, species = species, + size_axis = "l", + return_data = TRUE))) + expect_true("l" %in% names(plotFMort(params_len, species = species, + size_axis = "l", + return_data = TRUE))) + expect_true("l" %in% names(plotDiet(params_len, species = species[[1]], + size_axis = "l", + return_data = TRUE))) + expect_true("l" %in% names(plot(getPredMort(params_len), + species = species, size_axis = "l", + return_data = TRUE))) + expect_true("l" %in% names(plot(getFMort(sim_len), species = species, + size_axis = "l", return_data = TRUE))) +}) + test_that("yield plotting helpers validate comparison and gear selection", { sim_shifted <- sim dimnames(sim_shifted@n)$time <- as.character(10:13) From e8a944145512dbc784e49ecf6dbc611bba94beb7 Mon Sep 17 00:00:00 2001 From: Gustav Delius Date: Sat, 23 May 2026 10:05:29 +0100 Subject: [PATCH 2/3] Add support for length axis limits in plotting functions --- NEWS.md | 4 + R/ArraySpeciesBySize-class.R | 32 ++++- R/ArrayTimeBySpeciesBySize-class.R | 14 ++- R/animateSpectra.R | 10 +- R/plots.R | 183 ++++++++++++++++++++++------- man/addPlot.Rd | 5 + man/animate.Rd | 6 + man/plot.Rd | 6 + man/plot2.Rd | 6 + man/plotCDF.Rd | 8 ++ man/plotCDF2.Rd | 7 ++ man/plotDiet.Rd | 25 +++- man/plotFMort.Rd | 18 +++ man/plotFeedingLevel.Rd | 18 +++ man/plotPredMort.Rd | 18 +++ man/plotRelative.Rd | 6 + man/plotSpectra.Rd | 8 ++ man/plotSpectra2.Rd | 7 ++ man/plotSpectraRelative.Rd | 2 + tests/testthat/test-plots.R | 27 +++++ 20 files changed, 361 insertions(+), 49 deletions(-) diff --git a/NEWS.md b/NEWS.md index 9bfb95b07..dc97604c8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -138,6 +138,10 @@ individual variability in growth to be modelled. size axis instead of weight in grams, using the species' allometric weight-length relationship. +- Size-based plots with a `size_axis` argument now accept `llim`, the + length-axis equivalent of `wlim`, for filtering and limiting plots when + `size_axis = "l"`. + - The `plot()` and `summary()` methods for `MizerParams`, `MizerSim`, and the mizer array classes are now registered as S3 methods rather than S4 methods, so `plot()` and `summary()` remain plain S3 generics when mizer is loaded, diff --git a/R/ArraySpeciesBySize-class.R b/R/ArraySpeciesBySize-class.R index 1128cc136..cda89d462 100644 --- a/R/ArraySpeciesBySize-class.R +++ b/R/ArraySpeciesBySize-class.R @@ -159,6 +159,9 @@ print.summary.ArraySpeciesBySize <- function(x, ...) { #' @param wlim A numeric vector of length two providing lower and upper limits #' for the weight (x) axis. Use `NA` to refer to the existing minimum or #' maximum. Only applies to `ArraySpeciesBySize`. +#' @param llim A numeric vector of length two providing lower and upper limits +#' for the length (x) axis when `size_axis = "l"`. Use `NA` to refer to the +#' existing minimum or maximum. Only applies to `ArraySpeciesBySize`. #' @param size_axis Whether to plot size as weight (`"w"`, default) or length #' (`"l"`), using the allometric weight-length relationship. #' @param ylim A numeric vector of length two providing lower and upper limits @@ -190,6 +193,7 @@ plot.ArraySpeciesBySize <- function(x, species = NULL, return_data = FALSE, log_x = TRUE, log_y = FALSE, log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, ...) { @@ -198,6 +202,9 @@ plot.ArraySpeciesBySize <- function(x, species = NULL, log_x <- log_axes$log_x log_y <- log_axes$log_y + assert_that(length(wlim) == 2, + length(llim) == 2, + length(ylim) == 2) value_name <- attr(x, "value_name") %||% "Rate" units_str <- attr(x, "units") params <- attr(x, "params") @@ -205,6 +212,9 @@ plot.ArraySpeciesBySize <- function(x, species = NULL, x, species = species, all.sizes = all.sizes, wlim = wlim, total = total, background = background) plot_dat <- convert_plot_size_axis(plot_dat, params, size_axis) + if (identical(size_axis, "l")) { + plot_dat <- filter_plot_length_limits(plot_dat, llim) + } if (return_data) return(plot_dat) @@ -217,7 +227,7 @@ plot.ArraySpeciesBySize <- function(x, species = NULL, ylab = y_label, xtrans = if (log_x) "log10" else "identity", ytrans = if (log_y) "log10" else "identity", - xlim = plot_size_xlim(wlim, size_axis), ylim = ylim, + xlim = plot_size_xlim(wlim, size_axis, llim), ylim = ylim, highlight = highlight, y_ticks = y_ticks, legend_var = "Legend") } @@ -267,6 +277,7 @@ plot2.ArraySpeciesBySize <- function(x, y, name1 = "First", name2 = "Second", species = NULL, all.sizes = FALSE, log_x = TRUE, log_y = FALSE, log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, ...) { @@ -276,6 +287,9 @@ plot2.ArraySpeciesBySize <- function(x, y, name1 = "First", name2 = "Second", log_axes <- parsePlotLog(log, log_x = log_x, log_y = log_y) log_x <- log_axes$log_x log_y <- log_axes$log_y + assert_that(length(wlim) == 2, + length(llim) == 2, + length(ylim) == 2) params <- attr(x, "params") y_label <- array_y_label(x, default = "Rate") @@ -291,7 +305,8 @@ plot2.ArraySpeciesBySize <- function(x, y, name1 = "First", name2 = "Second", xlab = plot_size_xlab(size_axis), ylab = y_label, xtrans = if (log_x) "log10" else "identity", ytrans = if (log_y) "log10" else "identity", - xlim = plot_size_xlim(wlim, size_axis), ylim = ylim, + xlim = plot_size_xlim(wlim, size_axis, llim), + ylim = ylim, y_ticks = y_ticks, legend_var = "Legend", size_axis = size_axis) } @@ -328,12 +343,16 @@ plotRelative.ArraySpeciesBySize <- function(x, y, species = NULL, log_x = TRUE, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, ...) { check_plot2_compatible(x, y, "ArraySpeciesBySize") compare_array_metadata(x, y) size_axis <- plot_size_axis(size_axis) + assert_that(length(wlim) == 2, + length(llim) == 2, + length(ylim) == 2) params <- attr(x, "params") plot_dat1 <- prepare_ArraySpeciesBySize_plot_data( x, species = species, all.sizes = all.sizes, wlim = wlim, @@ -345,7 +364,8 @@ plotRelative.ArraySpeciesBySize <- function(x, y, species = NULL, plotRelativeDataFrame(plot_dat1, plot_dat2, params, xlab = plot_size_xlab(size_axis), xtrans = if (log_x) "log10" else "identity", - xlim = plot_size_xlim(wlim, size_axis), ylim = ylim, + xlim = plot_size_xlim(wlim, size_axis, llim), + ylim = ylim, legend_var = "Legend", size_axis = size_axis) } @@ -420,6 +440,7 @@ addPlot <- function(plot, x, ...) { addPlot.ArraySpeciesBySize <- function(plot, x, species = NULL, all.sizes = FALSE, wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -436,6 +457,8 @@ addPlot.ArraySpeciesBySize <- function(plot, x, species = NULL, alpha >= 0, alpha <= 1) size_axis <- plot_size_axis(size_axis) + assert_that(length(wlim) == 2, + length(llim) == 2) plot <- deep_copy(plot) plot_dat <- prepare_ArraySpeciesBySize_plot_data( @@ -443,6 +466,9 @@ addPlot.ArraySpeciesBySize <- function(plot, x, species = NULL, total = total, background = background) params <- attr(x, "params") plot_dat <- convert_plot_size_axis(plot_dat, params, size_axis) + if (identical(size_axis, "l")) { + plot_dat <- filter_plot_length_limits(plot_dat, llim) + } x_var <- plot_size_x_var(size_axis) check_addPlot_compatible(plot, x_var = x_var, y_var = "value", units = attr(x, "units")) diff --git a/R/ArrayTimeBySpeciesBySize-class.R b/R/ArrayTimeBySpeciesBySize-class.R index 4651ff200..5a6fab1fc 100644 --- a/R/ArrayTimeBySpeciesBySize-class.R +++ b/R/ArrayTimeBySpeciesBySize-class.R @@ -144,6 +144,7 @@ plot.ArrayTimeBySpeciesBySize <- function(x, species = NULL, time = NULL, return_data = FALSE, log_x = TRUE, log_y = FALSE, log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, ...) { @@ -168,7 +169,8 @@ plot.ArrayTimeBySpeciesBySize <- function(x, species = NULL, time = NULL, plot.ArraySpeciesBySize(slice, species = species, all.sizes = all.sizes, highlight = highlight, return_data = return_data, log_x = log_x, log_y = log_y, log = log, - wlim = wlim, ylim = ylim, size_axis = size_axis, + wlim = wlim, ylim = ylim, llim = llim, + size_axis = size_axis, total = total, background = background, y_ticks = y_ticks, ...) } @@ -186,6 +188,7 @@ plot2.ArrayTimeBySpeciesBySize <- function(x, y, name1 = "First", log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -197,7 +200,7 @@ plot2.ArrayTimeBySpeciesBySize <- function(x, y, name1 = "First", plot2.ArraySpeciesBySize(slice1, slice2, name1 = name1, name2 = name2, species = species, all.sizes = all.sizes, log_x = log_x, log_y = log_y, log = log, - wlim = wlim, ylim = ylim, + wlim = wlim, ylim = ylim, llim = llim, size_axis = size_axis, total = total, background = background, y_ticks = y_ticks, ...) } @@ -213,6 +216,7 @@ plotRelative.ArrayTimeBySpeciesBySize <- function(x, y, species = NULL, log_x = TRUE, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, ...) { @@ -222,7 +226,7 @@ plotRelative.ArrayTimeBySpeciesBySize <- function(x, y, species = NULL, plotRelative.ArraySpeciesBySize(slice1, slice2, species = species, all.sizes = all.sizes, log_x = log_x, - wlim = wlim, ylim = ylim, + wlim = wlim, ylim = ylim, llim = llim, size_axis = size_axis, total = total, background = background, ...) @@ -273,6 +277,7 @@ animate.ArrayTimeBySpeciesBySize <- function(x, species = NULL, time_range = NULL, log_x = TRUE, wlim = c(NA, NA), + llim = c(NA, NA), ylim = c(NA, NA), log_y = TRUE, size_axis = c("w", "l"), @@ -286,7 +291,7 @@ animate.ArrayTimeBySpeciesBySize <- function(x, species = NULL, is.number(frame_duration), frame_duration >= 0, is.number(transition_duration), transition_duration >= 0, is.string(easing), - length(wlim) == 2, length(ylim) == 2) + length(wlim) == 2, length(llim) == 2, length(ylim) == 2) size_axis <- plot_size_axis(size_axis) params <- attr(x, "params") @@ -358,6 +363,7 @@ animate.ArrayTimeBySpeciesBySize <- function(x, species = NULL, } animate_plotly(df, params, log_x, log_y, y_label, wlim, ylim, + llim = llim, size_axis = size_axis, frame_duration = frame_duration, transition_duration = transition_duration, diff --git a/R/animateSpectra.R b/R/animateSpectra.R index 45ddcc2e1..dbe290bd3 100644 --- a/R/animateSpectra.R +++ b/R/animateSpectra.R @@ -45,6 +45,9 @@ #' @param wlim A numeric vector of length two providing lower and upper limits #' for the body-size (x) axis. Use `NA` to refer to the existing minimum or #' maximum. +#' @param llim A numeric vector of length two providing lower and upper limits +#' for the length (x) axis when `size_axis = "l"`. Use `NA` to refer to the +#' existing minimum or maximum. #' @param ylim A numeric vector of length two providing lower and upper limits #' for the value (y) axis. Use `NA` to refer to the existing minimum or #' maximum. Limits are applied as Plotly axis ranges, so points outside the @@ -91,6 +94,7 @@ animate <- function(x, ...) UseMethod("animate") animate.MizerSim <- function(x, species = NULL, time_range = NULL, log_x = TRUE, log_y = TRUE, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), power = 1, total = FALSE, resource = TRUE, background = TRUE, @@ -105,6 +109,7 @@ animate.MizerSim <- function(x, species = NULL, time_range = NULL, is.number(transition_duration), transition_duration >= 0, is.string(easing), length(wlim) == 2, + length(llim) == 2, length(ylim) == 2) species <- valid_species_arg(sim, species) @@ -160,6 +165,7 @@ animate.MizerSim <- function(x, species = NULL, time_range = NULL, nf <- mutate(nf, value = value * w^power) animate_plotly(nf, sim@params, log_x, log_y, y_label, wlim, ylim, + llim = llim, size_axis = size_axis, frame_duration = frame_duration, transition_duration = transition_duration, @@ -173,6 +179,7 @@ animate.MizerSim <- function(x, species = NULL, time_range = NULL, # appear together and share a single legend entry. animate_plotly <- function(df, params, log_x, log_y, y_label, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = "w", frame_duration = 500, transition_duration = 500, easing = "linear") { @@ -206,7 +213,8 @@ animate_plotly <- function(df, params, log_x, log_y, y_label, } p <- plotly::layout(p, xaxis = plotly_axis( - df[[x_var]], plot_size_xlim(wlim, size_axis), + df[[x_var]], + plot_size_xlim(wlim, size_axis, llim), log_x, plot_size_xlab(size_axis)), yaxis = plotly_axis(df$value, ylim, log_y, y_label), legend = list(traceorder = "normal")) diff --git a/R/plots.R b/R/plots.R index 617ee4e4a..c9ceeaf6b 100644 --- a/R/plots.R +++ b/R/plots.R @@ -442,8 +442,17 @@ plot_size_xlab <- function(size_axis) { if (identical(plot_size_axis(size_axis), "l")) "Length [cm]" else "Size [g]" } -plot_size_xlim <- function(wlim, size_axis) { - if (identical(plot_size_axis(size_axis), "l")) c(NA, NA) else wlim +plot_size_xlim <- function(wlim, size_axis, llim = c(NA, NA)) { + if (identical(plot_size_axis(size_axis), "l")) llim else wlim +} + +filter_plot_length_limits <- function(plot_dat, llim) { + if (!"l" %in% names(plot_dat)) { + return(plot_dat) + } + if (!is.na(llim[1])) plot_dat <- plot_dat[plot_dat$l >= llim[1], ] + if (!is.na(llim[2])) plot_dat <- plot_dat[plot_dat$l <= llim[2], ] + plot_dat } plot_size_tooltip <- function(size_axis, before = NULL, after = NULL) { @@ -451,7 +460,8 @@ plot_size_tooltip <- function(size_axis, before = NULL, after = NULL) { } convert_plot_size_axis <- function(plot_dat, params, size_axis, - species_col = "Species") { + species_col = "Species", + drop_w = TRUE) { size_axis <- plot_size_axis(size_axis) if (identical(size_axis, "w")) { return(plot_dat) @@ -472,11 +482,21 @@ convert_plot_size_axis <- function(plot_dat, params, size_axis, plot_dat <- plot_dat[!is.na(species_idx), , drop = FALSE] species_idx <- species_idx[!is.na(species_idx)] if (nrow(plot_dat) == 0) { + if (!drop_w) { + plot_dat$l <- numeric(0) + return(plot_dat[, c("l", "w", + setdiff(names(plot_dat), c("l", "w"))), + drop = FALSE]) + } return(plot_dat[, setdiff(names(plot_dat), "w"), drop = FALSE]) } sp <- params@species_params[species_idx, , drop = FALSE] plot_dat$l <- w2l(plot_dat$w, sp) - plot_dat[, c("l", setdiff(names(plot_dat), c("l", "w"))), + if (drop_w) { + return(plot_dat[, c("l", setdiff(names(plot_dat), c("l", "w"))), + drop = FALSE]) + } + plot_dat[, c("l", "w", setdiff(names(plot_dat), c("l", "w"))), drop = FALSE] } @@ -812,6 +832,10 @@ plotlyYieldGear <- function(sim, species = NULL, #' the fish grid) or `min(params@w)` when `resource = FALSE`; the upper #' default is `max(params@w_full)`. Data is filtered to this range and the #' axis limits are set accordingly. +#' @param llim A numeric vector of length two providing lower and upper limits +#' for the length axis when `size_axis = "l"`. Use `NA` to auto-scale to the +#' data range. Data is filtered to this range and the axis limits are set +#' accordingly. #' @param ylim A numeric vector of length two providing lower and upper limits #' for the y axis. Use NA to auto-scale to the data range. Values below 1e-20 #' are always filtered out from the data regardless of `ylim[1]`. Data above @@ -878,6 +902,7 @@ plotSpectra.MizerSim <- function(object, species = NULL, time_range, geometric_mean = FALSE, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, resource = TRUE, background = TRUE, @@ -897,6 +922,7 @@ plotSpectra.MizerSim <- function(object, species = NULL, is.flag(background), is.number(power), length(wlim) == 2, + length(llim) == 2, length(ylim) == 2) species <- valid_species_arg(object, species) if (length(species) == 0 && !total && !resource) { @@ -916,6 +942,7 @@ plotSpectra.MizerSim <- function(object, species = NULL, n_pp <- apply(object@n_pp[time_elements, , drop = FALSE], 2, mean_fn) plot_spectra(object@params, n = n, n_pp = n_pp, species = species, wlim = wlim, ylim = ylim, + llim = llim, power = power, total = total, resource = resource, background = background, highlight = highlight, log_x = log_x, log_y = log_y, @@ -927,6 +954,7 @@ plotSpectra.MizerSim <- function(object, species = NULL, #' @export plotSpectra.MizerParams <- function(object, species = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, resource = TRUE, background = TRUE, @@ -946,6 +974,7 @@ plotSpectra.MizerParams <- function(object, species = NULL, is.flag(background), is.number(power), length(wlim) == 2, + length(llim) == 2, length(ylim) == 2) species <- valid_species_arg(object, species) if (length(species) == 0 && !total && !resource) { @@ -954,6 +983,7 @@ plotSpectra.MizerParams <- function(object, species = NULL, plot_spectra(object, n = object@initial_n, n_pp = object@initial_n_pp, species = species, wlim = wlim, ylim = ylim, + llim = llim, power = power, total = total, resource = resource, background = background, highlight = highlight, log_x = log_x, log_y = log_y, @@ -963,7 +993,7 @@ plotSpectra.MizerParams <- function(object, species = NULL, plot_spectra <- function(params, n, n_pp, - species, wlim, ylim, power, + species, wlim, ylim, llim, power, total, resource, background, highlight, log_x, log_y, size_axis, return_data) { params <- validParams(params) @@ -1039,6 +1069,9 @@ plot_spectra <- function(params, n, n_pp, filter_min <- if (is.na(ylim[1])) 1e-20 else ylim[1] plot_dat <- plot_dat[plot_dat$value > filter_min, ] plot_dat <- convert_plot_size_axis(plot_dat, params, size_axis) + if (identical(size_axis, "l")) { + plot_dat <- filter_plot_length_limits(plot_dat, llim) + } if (return_data) return(plot_dat) @@ -1046,7 +1079,7 @@ plot_spectra <- function(params, n, n_pp, ylab = y_label, xtrans = if (log_x) "log10" else "identity", ytrans = if (log_y) "log10" else "identity", - xlim = plot_size_xlim(wlim, size_axis), ylim = ylim, + xlim = plot_size_xlim(wlim, size_axis, llim), ylim = ylim, highlight = highlight, legend_var = "Legend") } @@ -1090,6 +1123,7 @@ plotCDF.MizerSim <- function(object, species = NULL, time_range, geometric_mean = FALSE, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, resource = FALSE, background = TRUE, @@ -1106,11 +1140,12 @@ plotCDF.MizerSim <- function(object, species = NULL, is.flag(background), is.flag(normalise), is.number(power), length(wlim) == 2, + length(llim) == 2, length(ylim) == 2) args <- list(object = object, species = species, geometric_mean = geometric_mean, - wlim = wlim, ylim = c(NA, NA), + wlim = wlim, ylim = c(NA, NA), llim = llim, power = power, total = total, resource = resource, background = background, size_axis = "w", @@ -1121,7 +1156,7 @@ plotCDF.MizerSim <- function(object, species = NULL, plot_dat <- do.call(plotSpectra, args) plot_cdf(plot_dat, object@params, power = power, normalise = normalise, log_x = log_x, wlim = wlim, ylim = ylim, - highlight = highlight, size_axis = size_axis, + llim = llim, highlight = highlight, size_axis = size_axis, return_data = return_data) } @@ -1129,6 +1164,7 @@ plotCDF.MizerSim <- function(object, species = NULL, #' @export plotCDF.MizerParams <- function(object, species = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, resource = FALSE, background = TRUE, @@ -1145,23 +1181,32 @@ plotCDF.MizerParams <- function(object, species = NULL, is.flag(background), is.flag(normalise), is.number(power), length(wlim) == 2, + length(llim) == 2, length(ylim) == 2) plot_dat <- plotSpectra(object, species = species, wlim = wlim, ylim = c(NA, NA), + llim = llim, power = power, total = total, resource = resource, background = background, size_axis = "w", return_data = TRUE) plot_cdf(plot_dat, object, power = power, normalise = normalise, log_x = log_x, wlim = wlim, ylim = ylim, - highlight = highlight, size_axis = size_axis, + llim = llim, highlight = highlight, size_axis = size_axis, return_data = return_data) } plot_cdf <- function(plot_dat, params, power, normalise, log_x, wlim, ylim, - highlight, size_axis, return_data) { + llim, highlight, size_axis, return_data) { size_axis <- plot_size_axis(size_axis) + if (identical(size_axis, "l")) { + plot_dat_l <- convert_plot_size_axis(plot_dat, params, size_axis, + drop_w = FALSE) + plot_dat_l <- filter_plot_length_limits(plot_dat_l, llim) + plot_dat <- plot_dat_l[, setdiff(names(plot_dat_l), "l"), + drop = FALSE] + } cdf_dat <- prepare_spectra_cdf_data(plot_dat, params, normalise = normalise) cdf_dat <- convert_plot_size_axis(cdf_dat, params, size_axis) @@ -1172,7 +1217,7 @@ plot_cdf <- function(plot_dat, params, power, normalise, log_x, wlim, ylim, ylab = cdf_y_label(power, normalise), xtrans = if (log_x) "log10" else "identity", ytrans = "identity", - xlim = plot_size_xlim(wlim, size_axis), ylim = ylim, + xlim = plot_size_xlim(wlim, size_axis, llim), ylim = ylim, highlight = highlight, legend_var = "Legend") } @@ -1257,10 +1302,11 @@ parsePlotCDFLog <- function(log, log_x) { #' } plotCDF2 <- function(object1, object2, name1 = "First", name2 = "Second", power = 1, normalise = TRUE, log_x = TRUE, log = NULL, - resource = FALSE, size_axis = c("w", "l"), ...) { + resource = FALSE, llim = c(NA, NA), + size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) log_x <- parsePlotCDFLog(log, log_x) - assert_that(is.number(power), is.flag(normalise)) + assert_that(is.number(power), is.flag(normalise), length(llim) == 2) args <- list(...) wlim <- args$wlim %||% c(NA, NA) @@ -1280,7 +1326,7 @@ plotCDF2 <- function(object1, object2, name1 = "First", name2 = "Second", ylab = cdf_y_label(power, normalise), xtrans = if (log_x) "log10" else "identity", ytrans = "identity", - xlim = plot_size_xlim(wlim, size_axis), + xlim = plot_size_xlim(wlim, size_axis, llim), ylim = ylim, legend_var = "Legend", size_axis = size_axis) } @@ -1316,11 +1362,13 @@ plotCDF2 <- function(object1, object2, name1 = "First", name2 = "Second", #' } plotSpectra2 <- function(object1, object2, name1 = "First", name2 = "Second", power = 1, log_x = TRUE, log_y = TRUE, - log = NULL, size_axis = c("w", "l"), ...) { + log = NULL, llim = c(NA, NA), + size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) log_axes <- parsePlotLog(log, log_x = log_x, log_y = log_y) log_x <- log_axes$log_x log_y <- log_axes$log_y + assert_that(length(llim) == 2) args <- list(...) wlim <- args$wlim %||% c(NA, NA) @@ -1338,7 +1386,7 @@ plotSpectra2 <- function(object1, object2, name1 = "First", name2 = "Second", ylab = spectra_y_label(power), xtrans = if (log_x) "log10" else "identity", ytrans = if (log_y) "log10" else "identity", - xlim = plot_size_xlim(wlim, size_axis), + xlim = plot_size_xlim(wlim, size_axis, llim), ylim = ylim, legend_var = "Legend", size_axis = size_axis) } @@ -1357,11 +1405,12 @@ spectra_y_label <- function(power) { plotlySpectra2 <- function(object1, object2, name1 = "First", name2 = "Second", power = 1, log_x = TRUE, log_y = TRUE, log = NULL, + llim = c(NA, NA), size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) ggplotly(plotSpectra2(object1, object2, name1 = name1, name2 = name2, power = power, log_x = log_x, log_y = log_y, - log = log, size_axis = size_axis, ...), + log = log, llim = llim, size_axis = size_axis, ...), tooltip = plot_size_tooltip(size_axis, before = "Species", after = c("value", "Model"))) } @@ -1408,8 +1457,10 @@ plotlySpectra2 <- function(object1, object2, name1 = "First", #' } plotSpectraRelative <- function(object1, object2, log_x = TRUE, ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) + assert_that(length(llim) == 2) args <- list(...) wlim <- args$wlim %||% c(NA, NA) @@ -1420,7 +1471,8 @@ plotSpectraRelative <- function(object1, object2, log_x = TRUE, plotRelativeDataFrame(sf1, sf2, validParams(params), xlab = plot_size_xlab(size_axis), xtrans = if (log_x) "log10" else "identity", - xlim = plot_size_xlim(wlim, size_axis), ylim = ylim, + xlim = plot_size_xlim(wlim, size_axis, llim), + ylim = ylim, legend_var = "Legend", size_axis = size_axis) } @@ -1428,10 +1480,12 @@ plotSpectraRelative <- function(object1, object2, log_x = TRUE, #' @export plotlySpectraRelative <- function(object1, object2, log_x = TRUE, ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) ggplotly(plotSpectraRelative(object1, object2, log_x = log_x, - ylim = ylim, size_axis = size_axis, ...), + ylim = ylim, llim = llim, + size_axis = size_axis, ...), tooltip = plot_size_tooltip(size_axis, before = "Legend", after = "rel_diff")) } @@ -1447,6 +1501,7 @@ plotlyCDF <- function(object, species = NULL, background = TRUE, highlight = NULL, normalise = TRUE, log_x = TRUE, log = NULL, + llim = c(NA, NA), size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) args <- list(object = object, species = species, @@ -1455,7 +1510,8 @@ plotlyCDF <- function(object, species = NULL, biomass = biomass, total = total, resource = resource, background = background, highlight = highlight, normalise = normalise, - log_x = log_x, log = log, size_axis = size_axis, ...) + log_x = log_x, log = log, llim = llim, + size_axis = size_axis, ...) if (!missing(time_range)) { args$time_range <- time_range } @@ -1473,12 +1529,13 @@ plotlyCDF <- function(object, species = NULL, plotlyCDF2 <- function(object1, object2, name1 = "First", name2 = "Second", power = 1, normalise = TRUE, log_x = TRUE, log = NULL, resource = FALSE, + llim = c(NA, NA), size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) args <- list(object1 = object1, object2 = object2, name1 = name1, name2 = name2, normalise = normalise, log_x = log_x, log = log, - size_axis = size_axis, ...) + llim = llim, size_axis = size_axis, ...) if (!missing(power)) { args$power <- power } @@ -1496,7 +1553,8 @@ plotlySpectra <- function(object, species = NULL, total = FALSE, resource = TRUE, background = TRUE, highlight = NULL, log_x = TRUE, log_y = TRUE, - log = NULL, size_axis = c("w", "l"), ...) { + log = NULL, llim = c(NA, NA), + size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) argg <- as.list(environment()) ggplotly(do.call("plotSpectra", argg), @@ -1558,11 +1616,14 @@ plotFeedingLevel <- function(object, ...) { plotFeedingLevel.MizerSim <- function(object, species = NULL, time_range, highlight = NULL, all.sizes = FALSE, include_critical = FALSE, + wlim = c(NA, NA), llim = c(NA, NA), size_axis = c("w", "l"), return_data = FALSE, ...) { size_axis <- plot_size_axis(size_axis) assert_that(is.flag(all.sizes), is.flag(include_critical), + length(wlim) == 2, + length(llim) == 2, is.flag(return_data)) if (missing(time_range)) { time_range <- max(as.numeric(dimnames(object@n)$time)) @@ -1576,6 +1637,7 @@ plotFeedingLevel.MizerSim <- function(object, species = NULL, plot_feeding_level(params, feed, species = species, highlight = highlight, all.sizes = all.sizes, include_critical = include_critical, + wlim = wlim, llim = llim, size_axis = size_axis, return_data = return_data) } @@ -1585,24 +1647,28 @@ plotFeedingLevel.MizerSim <- function(object, species = NULL, plotFeedingLevel.MizerParams <- function(object, species = NULL, highlight = NULL, all.sizes = FALSE, include_critical = FALSE, + wlim = c(NA, NA), llim = c(NA, NA), size_axis = c("w", "l"), return_data = FALSE, ...) { size_axis <- plot_size_axis(size_axis) assert_that(is.flag(all.sizes), is.flag(include_critical), + length(wlim) == 2, + length(llim) == 2, is.flag(return_data)) params <- validParams(object) feed <- getFeedingLevel(params, drop = FALSE) plot_feeding_level(params, feed, species = species, highlight = highlight, all.sizes = all.sizes, include_critical = include_critical, + wlim = wlim, llim = llim, size_axis = size_axis, return_data = return_data) } plot_feeding_level <- function(params, feed, species, highlight, all.sizes, include_critical, - size_axis, return_data) { + wlim, llim, size_axis, return_data) { size_axis <- plot_size_axis(size_axis) # selector for desired species @@ -1635,7 +1701,12 @@ plot_feeding_level <- function(params, feed, species, highlight, } plot_dat <- plot_dat[complete.cases(plot_dat), ] } + if (!is.na(wlim[1])) plot_dat <- plot_dat[plot_dat$w >= wlim[1], ] + if (!is.na(wlim[2])) plot_dat <- plot_dat[plot_dat$w <= wlim[2], ] plot_dat <- convert_plot_size_axis(plot_dat, params, size_axis) + if (identical(size_axis, "l")) { + plot_dat <- filter_plot_length_limits(plot_dat, llim) + } if (return_data) return(plot_dat) x_var <- plot_size_x_var(size_axis) @@ -1665,7 +1736,8 @@ plot_feeding_level <- function(params, feed, species, highlight, } p + geom_line(aes(x = .data[[x_var]], y = value, colour = Legend, linetype = Legend, linewidth = Legend)) + - scale_x_continuous(name = plot_size_xlab(size_axis), trans = "log10") + + scale_x_continuous(name = plot_size_xlab(size_axis), trans = "log10", + limits = plot_size_xlim(wlim, size_axis, llim)) + scale_y_continuous(name = "Feeding Level") + coord_cartesian(ylim = c(0, 1)) + scale_colour_manual(values = params@linecolour[legend_levels]) + @@ -1680,6 +1752,7 @@ plotlyFeedingLevel <- function(object, time_range, highlight = NULL, include_critical = FALSE, + wlim = c(NA, NA), llim = c(NA, NA), size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) argg <- as.list(environment()) @@ -1791,7 +1864,9 @@ plotPredMort <- function(object, ...) { #' @export plotPredMort.MizerSim <- function(object, species = NULL, time_range, all.sizes = FALSE, - highlight = NULL, size_axis = c("w", "l"), + highlight = NULL, + wlim = c(NA, NA), llim = c(NA, NA), + size_axis = c("w", "l"), return_data = FALSE, ...) { size_axis <- plot_size_axis(size_axis) @@ -1807,7 +1882,8 @@ plotPredMort.MizerSim <- function(object, species = NULL, units = "1/year", params = object@params) plot(pred_mort, species = species, all.sizes = all.sizes, - highlight = highlight, size_axis = size_axis, + highlight = highlight, wlim = wlim, llim = llim, + size_axis = size_axis, return_data = return_data, ylim = c(0, NA)) } @@ -1816,12 +1892,15 @@ plotPredMort.MizerSim <- function(object, species = NULL, #' @export plotPredMort.MizerParams <- function(object, species = NULL, all.sizes = FALSE, - highlight = NULL, size_axis = c("w", "l"), + highlight = NULL, + wlim = c(NA, NA), llim = c(NA, NA), + size_axis = c("w", "l"), return_data = FALSE, ...) { size_axis <- plot_size_axis(size_axis) plot(getPredMort(validParams(object)), species = species, - all.sizes = all.sizes, highlight = highlight, size_axis = size_axis, + all.sizes = all.sizes, highlight = highlight, + wlim = wlim, llim = llim, size_axis = size_axis, return_data = return_data, ylim = c(0, NA)) } @@ -1838,7 +1917,9 @@ plotM2 <- plotPredMort #' @export plotlyPredMort <- function(object, species = NULL, time_range, - highlight = NULL, size_axis = c("w", "l"), ...) { + highlight = NULL, + wlim = c(NA, NA), llim = c(NA, NA), + size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) argg <- as.list(environment()) ggplotly(do.call("plotPredMort", argg), @@ -1883,7 +1964,9 @@ plotFMort <- function(object, ...) { #' @export plotFMort.MizerSim <- function(object, species = NULL, time_range, all.sizes = FALSE, - highlight = NULL, size_axis = c("w", "l"), + highlight = NULL, + wlim = c(NA, NA), llim = c(NA, NA), + size_axis = c("w", "l"), return_data = FALSE, ...) { size_axis <- plot_size_axis(size_axis) @@ -1897,7 +1980,8 @@ plotFMort.MizerSim <- function(object, species = NULL, f <- ArraySpeciesBySize(f, value_name = "Fishing mortality", units = "1/year", params = object@params) plot(f, species = species, all.sizes = all.sizes, - highlight = highlight, size_axis = size_axis, + highlight = highlight, wlim = wlim, llim = llim, + size_axis = size_axis, return_data = return_data) } @@ -1905,12 +1989,15 @@ plotFMort.MizerSim <- function(object, species = NULL, #' @export plotFMort.MizerParams <- function(object, species = NULL, all.sizes = FALSE, - highlight = NULL, size_axis = c("w", "l"), + highlight = NULL, + wlim = c(NA, NA), llim = c(NA, NA), + size_axis = c("w", "l"), return_data = FALSE, ...) { size_axis <- plot_size_axis(size_axis) plot(getFMort(validParams(object)), species = species, - all.sizes = all.sizes, highlight = highlight, size_axis = size_axis, + all.sizes = all.sizes, highlight = highlight, + wlim = wlim, llim = llim, size_axis = size_axis, return_data = return_data) } @@ -1918,7 +2005,9 @@ plotFMort.MizerParams <- function(object, species = NULL, #' @export plotlyFMort <- function(object, species = NULL, time_range, - highlight = NULL, size_axis = c("w", "l"), ...) { + highlight = NULL, + wlim = c(NA, NA), llim = c(NA, NA), + size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) argg <- as.list(environment()) ggplotly(do.call("plotFMort", argg), @@ -2218,27 +2307,34 @@ plotDiet <- function(object, ...) { #' @rdname plotDiet #' @export plotDiet.MizerSim <- function(object, species = NULL, + wlim = c(NA, NA), llim = c(NA, NA), size_axis = c("w", "l"), return_data = FALSE, ...) { size_axis <- plot_size_axis(size_axis) - plotDiet(object@params, species = species, size_axis = size_axis, + plotDiet(object@params, species = species, wlim = wlim, llim = llim, + size_axis = size_axis, return_data = return_data, ...) } #' @rdname plotDiet #' @export plotDiet.MizerParams <- function(object, species = NULL, + wlim = c(NA, NA), llim = c(NA, NA), size_axis = c("w", "l"), return_data = FALSE, ...) { size_axis <- plot_size_axis(size_axis) - assert_that(is.flag(return_data)) + assert_that(is.flag(return_data), + length(wlim) == 2, + length(llim) == 2) params <- validParams(object) diet <- getDiet(params) plot_diet(params, n = params@initial_n, diet = diet, species = species, - size_axis = size_axis, return_data = return_data) + wlim = wlim, llim = llim, size_axis = size_axis, + return_data = return_data) } -plot_diet <- function(params, n, diet, species, size_axis, return_data) { +plot_diet <- function(params, n, diet, species, wlim, llim, + size_axis, return_data) { size_axis <- plot_size_axis(size_axis) species <- valid_species_arg(params, species, return.logical = TRUE) diet <- diet[species, , , drop = FALSE] @@ -2249,6 +2345,8 @@ plot_diet <- function(params, n, diet, species, size_axis, return_data) { plot_dat$Prey <- factor(plot_dat$Prey, levels = rev(prey)) plot_dat <- plot_dat[plot_dat$Proportion > 0.001, ] + if (!is.na(wlim[1])) plot_dat <- plot_dat[plot_dat$w >= wlim[1], ] + if (!is.na(wlim[2])) plot_dat <- plot_dat[plot_dat$w <= wlim[2], ] # Restrict plot to relevant size ranges where abundance is meaningful # For each predator species, find the maximum size where density is meaningful @@ -2272,6 +2370,9 @@ plot_diet <- function(params, n, diet, species, size_axis, return_data) { plot_dat <- convert_plot_size_axis(plot_dat, params, size_axis, species_col = "Predator") + if (identical(size_axis, "l")) { + plot_dat <- filter_plot_length_limits(plot_dat, llim) + } if (return_data) return(plot_dat) x_var <- plot_size_x_var(size_axis) @@ -2279,7 +2380,7 @@ plot_diet <- function(params, n, diet, species, size_axis, return_data) { intersect(names(params@linecolour), plot_dat$Prey) p <- ggplot(plot_dat) + geom_area(aes(x = .data[[x_var]], y = Proportion, fill = Prey)) + - scale_x_log10() + + scale_x_log10(limits = plot_size_xlim(wlim, size_axis, llim)) + labs(x = plot_size_xlab(size_axis), y = "Proportion") + scale_fill_manual(values = params@linecolour[legend_levels], limits = legend_levels) @@ -2293,9 +2394,11 @@ plot_diet <- function(params, n, diet, species, size_axis, return_data) { #' @return `plotlyDiet()` returns a plotly object. #' @export plotlyDiet <- function(object, species = NULL, + wlim = c(NA, NA), llim = c(NA, NA), size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) - ggplotly(plotDiet(object, species = species, size_axis = size_axis, ...), + ggplotly(plotDiet(object, species = species, wlim = wlim, llim = llim, + size_axis = size_axis, ...), tooltip = plot_size_tooltip(size_axis, before = "Predator", after = c("Proportion", "Prey"))) } diff --git a/man/addPlot.Rd b/man/addPlot.Rd index 74a0d53ac..666b7a4e8 100644 --- a/man/addPlot.Rd +++ b/man/addPlot.Rd @@ -15,6 +15,7 @@ addPlot(plot, x, ...) species = NULL, all.sizes = FALSE, wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -58,6 +59,10 @@ all species.} for the weight (x) axis. Use \code{NA} to refer to the existing minimum or maximum. Only applies to \code{ArraySpeciesBySize}.} +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length (x) axis when \code{size_axis = "l"}. Use \code{NA} to refer to the +existing minimum or maximum. Only applies to \code{ArraySpeciesBySize}.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/animate.Rd b/man/animate.Rd index a44dbb147..50c1f2ac5 100644 --- a/man/animate.Rd +++ b/man/animate.Rd @@ -14,6 +14,7 @@ time_range = NULL, log_x = TRUE, wlim = c(NA, NA), + llim = c(NA, NA), ylim = c(NA, NA), log_y = TRUE, size_axis = c("w", "l"), @@ -35,6 +36,7 @@ animate(x, ...) log_y = TRUE, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), power = 1, total = FALSE, @@ -64,6 +66,10 @@ range. Default is the entire time range of \code{x}.} for the body-size (x) axis. Use \code{NA} to refer to the existing minimum or maximum.} +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length (x) axis when \code{size_axis = "l"}. Use \code{NA} to refer to the +existing minimum or maximum.} + \item{ylim}{A numeric vector of length two providing lower and upper limits for the value (y) axis. Use \code{NA} to refer to the existing minimum or maximum. Limits are applied as Plotly axis ranges, so points outside the diff --git a/man/plot.Rd b/man/plot.Rd index 04914ab8d..fbca0fd3c 100644 --- a/man/plot.Rd +++ b/man/plot.Rd @@ -22,6 +22,7 @@ log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -82,6 +83,7 @@ log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -132,6 +134,10 @@ maximum. Only applies to \code{ArraySpeciesBySize}.} for the value (y) axis. Use \code{NA} to refer to the existing minimum or maximum.} +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length (x) axis when \code{size_axis = "l"}. Use \code{NA} to refer to the +existing minimum or maximum. Only applies to \code{ArraySpeciesBySize}.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/plot2.Rd b/man/plot2.Rd index 2f2b73d75..35c7e671f 100644 --- a/man/plot2.Rd +++ b/man/plot2.Rd @@ -22,6 +22,7 @@ plot2(x, y, ...) log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -60,6 +61,7 @@ plot2(x, y, ...) log = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -98,6 +100,10 @@ maximum. Only applies to \code{ArraySpeciesBySize}.} for the value (y) axis. Use \code{NA} to refer to the existing minimum or maximum.} +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length (x) axis when \code{size_axis = "l"}. Use \code{NA} to refer to the +existing minimum or maximum. Only applies to \code{ArraySpeciesBySize}.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/plotCDF.Rd b/man/plotCDF.Rd index aa78ec21c..6f622d4dc 100644 --- a/man/plotCDF.Rd +++ b/man/plotCDF.Rd @@ -16,6 +16,7 @@ plotCDF(object, ...) geometric_mean = FALSE, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, @@ -35,6 +36,7 @@ plotCDF(object, ...) species = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, @@ -65,6 +67,7 @@ plotlyCDF( normalise = TRUE, log_x = TRUE, log = NULL, + llim = c(NA, NA), size_axis = c("w", "l"), ... ) @@ -101,6 +104,11 @@ for the y axis. Use NA to auto-scale to the data range. Values below 1e-20 are always filtered out from the data regardless of \code{ylim[1]}. Data above \code{ylim[2]} is filtered and the upper axis limit is set accordingly.} +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length axis when \code{size_axis = "l"}. Use \code{NA} to auto-scale to the +data range. Data is filtered to this range and the axis limits are set +accordingly.} + \item{power}{The abundance is plotted as the number density times the weight raised to \code{power}. The default \code{power = 1} gives the biomass density, whereas \code{power = 2} gives the biomass density with respect diff --git a/man/plotCDF2.Rd b/man/plotCDF2.Rd index 6660f2051..8d8989e45 100644 --- a/man/plotCDF2.Rd +++ b/man/plotCDF2.Rd @@ -15,6 +15,7 @@ plotCDF2( log_x = TRUE, log = NULL, resource = FALSE, + llim = c(NA, NA), size_axis = c("w", "l"), ... ) @@ -29,6 +30,7 @@ plotlyCDF2( log_x = TRUE, log = NULL, resource = FALSE, + llim = c(NA, NA), size_axis = c("w", "l"), ... ) @@ -58,6 +60,11 @@ only \code{"x"} and \code{""} are supported. If supplied, this overrides \code{l \item{resource}{A boolean value that determines whether resource is included. Default is FALSE.} +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length axis when \code{size_axis = "l"}. Use \code{NA} to auto-scale to the +data range. Data is filtered to this range and the axis limits are set +accordingly.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/plotDiet.Rd b/man/plotDiet.Rd index 200e34696..3cf9a87bc 100644 --- a/man/plotDiet.Rd +++ b/man/plotDiet.Rd @@ -12,6 +12,8 @@ plotDiet(object, ...) \method{plotDiet}{MizerSim}( object, species = NULL, + wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), return_data = FALSE, ... @@ -20,12 +22,21 @@ plotDiet(object, ...) \method{plotDiet}{MizerParams}( object, species = NULL, + wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), return_data = FALSE, ... ) -plotlyDiet(object, species = NULL, size_axis = c("w", "l"), ...) +plotlyDiet( + object, + species = NULL, + wlim = c(NA, NA), + llim = c(NA, NA), + size_axis = c("w", "l"), + ... +) } \arguments{ \item{object}{An object of class \linkS4class{MizerSim} or @@ -38,6 +49,18 @@ species are selected. A vector of species names, or a numeric vector with the species indices, or a logical vector indicating for each species whether it is to be selected (TRUE) or not.} +\item{wlim}{A numeric vector of length two providing lower and upper limits +for the w axis. Use NA for the default: the lower default is +\code{min(params@w) / 100} when \code{resource = TRUE} (to show some resource below +the fish grid) or \code{min(params@w)} when \code{resource = FALSE}; the upper +default is \code{max(params@w_full)}. Data is filtered to this range and the +axis limits are set accordingly.} + +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length axis when \code{size_axis = "l"}. Use \code{NA} to auto-scale to the +data range. Data is filtered to this range and the axis limits are set +accordingly.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/plotFMort.Rd b/man/plotFMort.Rd index f35e860a9..0a1fd1de0 100644 --- a/man/plotFMort.Rd +++ b/man/plotFMort.Rd @@ -15,6 +15,8 @@ plotFMort(object, ...) time_range, all.sizes = FALSE, highlight = NULL, + wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), return_data = FALSE, ... @@ -25,6 +27,8 @@ plotFMort(object, ...) species = NULL, all.sizes = FALSE, highlight = NULL, + wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), return_data = FALSE, ... @@ -35,6 +39,8 @@ plotlyFMort( species = NULL, time_range, highlight = NULL, + wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), ... ) @@ -60,6 +66,18 @@ outside a species' size range. Default FALSE.} \item{highlight}{Name or vector of names of the species to be highlighted.} +\item{wlim}{A numeric vector of length two providing lower and upper limits +for the w axis. Use NA for the default: the lower default is +\code{min(params@w) / 100} when \code{resource = TRUE} (to show some resource below +the fish grid) or \code{min(params@w)} when \code{resource = FALSE}; the upper +default is \code{max(params@w_full)}. Data is filtered to this range and the +axis limits are set accordingly.} + +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length axis when \code{size_axis = "l"}. Use \code{NA} to auto-scale to the +data range. Data is filtered to this range and the axis limits are set +accordingly.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/plotFeedingLevel.Rd b/man/plotFeedingLevel.Rd index 0d8397a6f..25121cc3f 100644 --- a/man/plotFeedingLevel.Rd +++ b/man/plotFeedingLevel.Rd @@ -16,6 +16,8 @@ plotFeedingLevel(object, ...) highlight = NULL, all.sizes = FALSE, include_critical = FALSE, + wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), return_data = FALSE, ... @@ -27,6 +29,8 @@ plotFeedingLevel(object, ...) highlight = NULL, all.sizes = FALSE, include_critical = FALSE, + wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), return_data = FALSE, ... @@ -38,6 +42,8 @@ plotlyFeedingLevel( time_range, highlight = NULL, include_critical = FALSE, + wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), ... ) @@ -66,6 +72,18 @@ outside a species' size range. Default FALSE.} \item{include_critical}{If TRUE, then the critical feeding level is also plotted. Default FALSE.} +\item{wlim}{A numeric vector of length two providing lower and upper limits +for the w axis. Use NA for the default: the lower default is +\code{min(params@w) / 100} when \code{resource = TRUE} (to show some resource below +the fish grid) or \code{min(params@w)} when \code{resource = FALSE}; the upper +default is \code{max(params@w_full)}. Data is filtered to this range and the +axis limits are set accordingly.} + +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length axis when \code{size_axis = "l"}. Use \code{NA} to auto-scale to the +data range. Data is filtered to this range and the axis limits are set +accordingly.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/plotPredMort.Rd b/man/plotPredMort.Rd index fa12bf73d..ec928a5e6 100644 --- a/man/plotPredMort.Rd +++ b/man/plotPredMort.Rd @@ -15,6 +15,8 @@ plotPredMort(object, ...) time_range, all.sizes = FALSE, highlight = NULL, + wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), return_data = FALSE, ... @@ -25,6 +27,8 @@ plotPredMort(object, ...) species = NULL, all.sizes = FALSE, highlight = NULL, + wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), return_data = FALSE, ... @@ -35,6 +39,8 @@ plotlyPredMort( species = NULL, time_range, highlight = NULL, + wlim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), ... ) @@ -60,6 +66,18 @@ outside a species' size range. Default FALSE.} \item{highlight}{Name or vector of names of the species to be highlighted.} +\item{wlim}{A numeric vector of length two providing lower and upper limits +for the w axis. Use NA for the default: the lower default is +\code{min(params@w) / 100} when \code{resource = TRUE} (to show some resource below +the fish grid) or \code{min(params@w)} when \code{resource = FALSE}; the upper +default is \code{max(params@w_full)}. Data is filtered to this range and the +axis limits are set accordingly.} + +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length axis when \code{size_axis = "l"}. Use \code{NA} to auto-scale to the +data range. Data is filtered to this range and the axis limits are set +accordingly.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/plotRelative.Rd b/man/plotRelative.Rd index 1e555f38f..c197975a5 100644 --- a/man/plotRelative.Rd +++ b/man/plotRelative.Rd @@ -18,6 +18,7 @@ plotRelative(x, y, ...) log_x = TRUE, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -46,6 +47,7 @@ plotRelative(x, y, ...) log_x = TRUE, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -74,6 +76,10 @@ maximum. Only applies to \code{ArraySpeciesBySize}.} for the value (y) axis. Use \code{NA} to refer to the existing minimum or maximum.} +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length (x) axis when \code{size_axis = "l"}. Use \code{NA} to refer to the +existing minimum or maximum. Only applies to \code{ArraySpeciesBySize}.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/plotSpectra.Rd b/man/plotSpectra.Rd index 621884554..cec41b26a 100644 --- a/man/plotSpectra.Rd +++ b/man/plotSpectra.Rd @@ -16,6 +16,7 @@ plotSpectra(object, ...) geometric_mean = FALSE, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, @@ -35,6 +36,7 @@ plotSpectra(object, ...) species = NULL, wlim = c(NA, NA), ylim = c(NA, NA), + llim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, @@ -65,6 +67,7 @@ plotlySpectra( log_x = TRUE, log_y = TRUE, log = NULL, + llim = c(NA, NA), size_axis = c("w", "l"), ... ) @@ -101,6 +104,11 @@ for the y axis. Use NA to auto-scale to the data range. Values below 1e-20 are always filtered out from the data regardless of \code{ylim[1]}. Data above \code{ylim[2]} is filtered and the upper axis limit is set accordingly.} +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length axis when \code{size_axis = "l"}. Use \code{NA} to auto-scale to the +data range. Data is filtered to this range and the axis limits are set +accordingly.} + \item{power}{The abundance is plotted as the number density times the weight raised to \code{power}. The default \code{power = 1} gives the biomass density, whereas \code{power = 2} gives the biomass density with respect diff --git a/man/plotSpectra2.Rd b/man/plotSpectra2.Rd index 3fcfd0bcc..ca9347f7d 100644 --- a/man/plotSpectra2.Rd +++ b/man/plotSpectra2.Rd @@ -14,6 +14,7 @@ plotSpectra2( log_x = TRUE, log_y = TRUE, log = NULL, + llim = c(NA, NA), size_axis = c("w", "l"), ... ) @@ -27,6 +28,7 @@ plotlySpectra2( log_x = TRUE, log_y = TRUE, log = NULL, + llim = c(NA, NA), size_axis = c("w", "l"), ... ) @@ -51,6 +53,11 @@ to logarithmic size bins.} in the same form as the base \code{\link[=plot]{plot()}} argument. For example, \code{"x"}, \code{"y"}, \code{"xy"} or \code{""}. If supplied, this overrides \code{log_x} and \code{log_y}.} +\item{llim}{A numeric vector of length two providing lower and upper limits +for the length axis when \code{size_axis = "l"}. Use \code{NA} to auto-scale to the +data range. Data is filtered to this range and the axis limits are set +accordingly.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/plotSpectraRelative.Rd b/man/plotSpectraRelative.Rd index 8c0488c96..cd19af54f 100644 --- a/man/plotSpectraRelative.Rd +++ b/man/plotSpectraRelative.Rd @@ -10,6 +10,7 @@ plotSpectraRelative( object2, log_x = TRUE, ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), ... ) @@ -19,6 +20,7 @@ plotlySpectraRelative( object2, log_x = TRUE, ylim = c(NA, NA), + llim = c(NA, NA), size_axis = c("w", "l"), ... ) diff --git a/tests/testthat/test-plots.R b/tests/testthat/test-plots.R index d4aa66b4a..f662e82df 100644 --- a/tests/testthat/test-plots.R +++ b/tests/testthat/test-plots.R @@ -310,6 +310,14 @@ test_that("size-based plots support length axes", { params_len@species_params[sp_idx, , drop = FALSE]) expect_equal(spectra_l$l, expected_l) + llim <- stats::quantile(spectra_l$l, c(0.25, 0.75), names = FALSE) + spectra_l_limited <- plotSpectra(params_len, species = species, + resource = FALSE, total = FALSE, + size_axis = "l", llim = llim, + return_data = TRUE) + expect_true(all(spectra_l_limited$l >= llim[1])) + expect_true(all(spectra_l_limited$l <= llim[2])) + spectra_hidden <- plotSpectra(params_len, species = species, resource = TRUE, total = TRUE, size_axis = "l", return_data = TRUE) @@ -323,6 +331,13 @@ test_that("size-based plots support length axes", { expect_true("l" %in% names(plotCDF(params_len, species = species, resource = FALSE, size_axis = "l", return_data = TRUE))) + cdf_l_limited <- plotCDF(params_len, species = species, resource = FALSE, + size_axis = "l", llim = llim, + return_data = TRUE) + expect_true(all(cdf_l_limited$l >= llim[1])) + expect_true(all(cdf_l_limited$l <= llim[2])) + expect_equal(stats::aggregate(value ~ Species, cdf_l_limited, max)$value, + rep(1, length(unique(cdf_l_limited$Species)))) expect_true("l" %in% names(plotSpectra2(params_len, params_len, species = species, resource = FALSE, @@ -348,9 +363,17 @@ test_that("size-based plots support length axes", { expect_true("l" %in% names(plotDiet(params_len, species = species[[1]], size_axis = "l", return_data = TRUE))) + expect_true(all(plotDiet(params_len, species = species[[1]], + size_axis = "l", llim = llim, + return_data = TRUE)$l >= llim[1])) expect_true("l" %in% names(plot(getPredMort(params_len), species = species, size_axis = "l", return_data = TRUE))) + rate_l_limited <- plot(getPredMort(params_len), species = species, + size_axis = "l", llim = llim, + return_data = TRUE) + expect_true(all(rate_l_limited$l >= llim[1])) + expect_true(all(rate_l_limited$l <= llim[2])) expect_true("l" %in% names(plot(getFMort(sim_len), species = species, size_axis = "l", return_data = TRUE))) }) @@ -549,6 +572,10 @@ test_that("axis limits are set correctly", { expect_true(is.na(p$scales$scales[[1]]$limits[1])) expect_equal(p$scales$scales[[1]]$limits[2], 8) + p <- plotSpectra(sim, species = species, resource = FALSE, + size_axis = "l", llim = c(10, 100)) + expect_equal(p$scales$scales[[2]]$limits, c(1, 2)) + # Default wlim lower depends on resource argument p_res <- plotSpectra(sim, species = species, return_data = TRUE) p_nores <- plotSpectra(sim, species = species, resource = FALSE, return_data = TRUE) From de1fea4c5113c534041eb175b2fa88093fbaa38f Mon Sep 17 00:00:00 2001 From: Gustav Delius Date: Sat, 23 May 2026 10:16:27 +0100 Subject: [PATCH 3/3] Reorder ylim and llim parameters in plotting functions for consistency --- R/ArraySpeciesBySize-class.R | 10 ++++----- R/ArrayTimeBySpeciesBySize-class.R | 12 +++++------ R/animateSpectra.R | 12 +++++------ R/plots.R | 33 +++++++++++++++--------------- man/animate.Rd | 2 +- man/plot.Rd | 12 +++++------ man/plot2.Rd | 12 +++++------ man/plotCDF.Rd | 16 +++++++-------- man/plotRelative.Rd | 12 +++++------ man/plotSpectra.Rd | 16 +++++++-------- 10 files changed, 69 insertions(+), 68 deletions(-) diff --git a/R/ArraySpeciesBySize-class.R b/R/ArraySpeciesBySize-class.R index cda89d462..560485979 100644 --- a/R/ArraySpeciesBySize-class.R +++ b/R/ArraySpeciesBySize-class.R @@ -192,8 +192,8 @@ plot.ArraySpeciesBySize <- function(x, species = NULL, all.sizes = FALSE, highlight = NULL, return_data = FALSE, log_x = TRUE, log_y = FALSE, log = NULL, - wlim = c(NA, NA), ylim = c(NA, NA), - llim = c(NA, NA), + wlim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, ...) { @@ -276,8 +276,8 @@ plot2 <- function(x, y, ...) { plot2.ArraySpeciesBySize <- function(x, y, name1 = "First", name2 = "Second", species = NULL, all.sizes = FALSE, log_x = TRUE, log_y = FALSE, log = NULL, - wlim = c(NA, NA), ylim = c(NA, NA), - llim = c(NA, NA), + wlim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, ...) { @@ -342,8 +342,8 @@ plotRelative.ArraySpeciesBySize <- function(x, y, species = NULL, all.sizes = FALSE, log_x = TRUE, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, ...) { diff --git a/R/ArrayTimeBySpeciesBySize-class.R b/R/ArrayTimeBySpeciesBySize-class.R index 5a6fab1fc..9fffcd315 100644 --- a/R/ArrayTimeBySpeciesBySize-class.R +++ b/R/ArrayTimeBySpeciesBySize-class.R @@ -143,8 +143,8 @@ plot.ArrayTimeBySpeciesBySize <- function(x, species = NULL, time = NULL, all.sizes = FALSE, highlight = NULL, return_data = FALSE, log_x = TRUE, log_y = FALSE, log = NULL, - wlim = c(NA, NA), ylim = c(NA, NA), - llim = c(NA, NA), + wlim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, y_ticks = 6, ...) { @@ -187,8 +187,8 @@ plot2.ArrayTimeBySpeciesBySize <- function(x, y, name1 = "First", log_x = TRUE, log_y = FALSE, log = NULL, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -215,8 +215,8 @@ plotRelative.ArrayTimeBySpeciesBySize <- function(x, y, species = NULL, all.sizes = FALSE, log_x = TRUE, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, ...) { @@ -362,8 +362,8 @@ animate.ArrayTimeBySpeciesBySize <- function(x, species = NULL, y_label <- paste0(value_name, " [", units_str, "]") } - animate_plotly(df, params, log_x, log_y, y_label, wlim, ylim, - llim = llim, + animate_plotly(df, params, log_x, log_y, y_label, wlim, llim, + ylim, size_axis = size_axis, frame_duration = frame_duration, transition_duration = transition_duration, diff --git a/R/animateSpectra.R b/R/animateSpectra.R index dbe290bd3..ea7c92290 100644 --- a/R/animateSpectra.R +++ b/R/animateSpectra.R @@ -93,8 +93,8 @@ animate <- function(x, ...) UseMethod("animate") #' @export animate.MizerSim <- function(x, species = NULL, time_range = NULL, log_x = TRUE, log_y = TRUE, - wlim = c(NA, NA), ylim = c(NA, NA), - llim = c(NA, NA), + wlim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), power = 1, total = FALSE, resource = TRUE, background = TRUE, @@ -164,8 +164,8 @@ animate.MizerSim <- function(x, species = NULL, time_range = NULL, } nf <- mutate(nf, value = value * w^power) - animate_plotly(nf, sim@params, log_x, log_y, y_label, wlim, ylim, - llim = llim, + animate_plotly(nf, sim@params, log_x, log_y, y_label, wlim, llim, + ylim, size_axis = size_axis, frame_duration = frame_duration, transition_duration = transition_duration, @@ -178,8 +178,8 @@ animate.MizerSim <- function(x, species = NULL, time_range = NULL, # by individual Species within each legend group — so background species always # appear together and share a single legend entry. animate_plotly <- function(df, params, log_x, log_y, y_label, - wlim = c(NA, NA), ylim = c(NA, NA), - llim = c(NA, NA), + wlim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = "w", frame_duration = 500, transition_duration = 500, easing = "linear") { diff --git a/R/plots.R b/R/plots.R index c9ceeaf6b..d65c3260f 100644 --- a/R/plots.R +++ b/R/plots.R @@ -901,8 +901,8 @@ plotSpectra <- function(object, ...) { plotSpectra.MizerSim <- function(object, species = NULL, time_range, geometric_mean = FALSE, - wlim = c(NA, NA), ylim = c(NA, NA), - llim = c(NA, NA), + wlim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, resource = TRUE, background = TRUE, @@ -953,8 +953,8 @@ plotSpectra.MizerSim <- function(object, species = NULL, #' @rdname plotSpectra #' @export plotSpectra.MizerParams <- function(object, species = NULL, - wlim = c(NA, NA), ylim = c(NA, NA), - llim = c(NA, NA), + wlim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, resource = TRUE, background = TRUE, @@ -993,7 +993,7 @@ plotSpectra.MizerParams <- function(object, species = NULL, plot_spectra <- function(params, n, n_pp, - species, wlim, ylim, llim, power, + species, wlim, llim, ylim, power, total, resource, background, highlight, log_x, log_y, size_axis, return_data) { params <- validParams(params) @@ -1122,8 +1122,8 @@ plotCDF <- function(object, ...) { plotCDF.MizerSim <- function(object, species = NULL, time_range, geometric_mean = FALSE, - wlim = c(NA, NA), ylim = c(NA, NA), - llim = c(NA, NA), + wlim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, resource = FALSE, background = TRUE, @@ -1145,7 +1145,7 @@ plotCDF.MizerSim <- function(object, species = NULL, args <- list(object = object, species = species, geometric_mean = geometric_mean, - wlim = wlim, ylim = c(NA, NA), llim = llim, + wlim = wlim, llim = llim, ylim = c(NA, NA), power = power, total = total, resource = resource, background = background, size_axis = "w", @@ -1163,8 +1163,8 @@ plotCDF.MizerSim <- function(object, species = NULL, #' @rdname plotCDF #' @export plotCDF.MizerParams <- function(object, species = NULL, - wlim = c(NA, NA), ylim = c(NA, NA), - llim = c(NA, NA), + wlim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, resource = FALSE, background = TRUE, @@ -1197,8 +1197,8 @@ plotCDF.MizerParams <- function(object, species = NULL, return_data = return_data) } -plot_cdf <- function(plot_dat, params, power, normalise, log_x, wlim, ylim, - llim, highlight, size_axis, return_data) { +plot_cdf <- function(plot_dat, params, power, normalise, log_x, wlim, llim, + ylim, highlight, size_axis, return_data) { size_axis <- plot_size_axis(size_axis) if (identical(size_axis, "l")) { plot_dat_l <- convert_plot_size_axis(plot_dat, params, size_axis, @@ -1495,13 +1495,13 @@ plotlySpectraRelative <- function(object1, object2, log_x = TRUE, #' @export plotlyCDF <- function(object, species = NULL, time_range, geometric_mean = FALSE, - wlim = c(NA, NA), ylim = c(NA, NA), + wlim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, resource = FALSE, background = TRUE, highlight = NULL, normalise = TRUE, log_x = TRUE, log = NULL, - llim = c(NA, NA), size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) args <- list(object = object, species = species, @@ -1548,12 +1548,13 @@ plotlyCDF2 <- function(object1, object2, name1 = "First", name2 = "Second", #' @export plotlySpectra <- function(object, species = NULL, time_range, geometric_mean = FALSE, - wlim = c(NA, NA), ylim = c(NA, NA), + wlim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, resource = TRUE, background = TRUE, highlight = NULL, log_x = TRUE, log_y = TRUE, - log = NULL, llim = c(NA, NA), + log = NULL, size_axis = c("w", "l"), ...) { size_axis <- plot_size_axis(size_axis) argg <- as.list(environment()) diff --git a/man/animate.Rd b/man/animate.Rd index 50c1f2ac5..bb5b8fe8c 100644 --- a/man/animate.Rd +++ b/man/animate.Rd @@ -35,8 +35,8 @@ animate(x, ...) log_x = TRUE, log_y = TRUE, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), power = 1, total = FALSE, diff --git a/man/plot.Rd b/man/plot.Rd index fbca0fd3c..13f10773a 100644 --- a/man/plot.Rd +++ b/man/plot.Rd @@ -21,8 +21,8 @@ log_y = FALSE, log = NULL, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -82,8 +82,8 @@ log_y = FALSE, log = NULL, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -130,14 +130,14 @@ in the same form as the base \code{\link[=plot]{plot()}} argument. For example, for the weight (x) axis. Use \code{NA} to refer to the existing minimum or maximum. Only applies to \code{ArraySpeciesBySize}.} -\item{ylim}{A numeric vector of length two providing lower and upper limits -for the value (y) axis. Use \code{NA} to refer to the existing minimum or -maximum.} - \item{llim}{A numeric vector of length two providing lower and upper limits for the length (x) axis when \code{size_axis = "l"}. Use \code{NA} to refer to the existing minimum or maximum. Only applies to \code{ArraySpeciesBySize}.} +\item{ylim}{A numeric vector of length two providing lower and upper limits +for the value (y) axis. Use \code{NA} to refer to the existing minimum or +maximum.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/plot2.Rd b/man/plot2.Rd index 35c7e671f..7b3e9d1d6 100644 --- a/man/plot2.Rd +++ b/man/plot2.Rd @@ -21,8 +21,8 @@ plot2(x, y, ...) log_y = FALSE, log = NULL, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -60,8 +60,8 @@ plot2(x, y, ...) log_y = FALSE, log = NULL, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -96,14 +96,14 @@ in the same form as the base \code{\link[=plot]{plot()}} argument. For example, for the weight (x) axis. Use \code{NA} to refer to the existing minimum or maximum. Only applies to \code{ArraySpeciesBySize}.} -\item{ylim}{A numeric vector of length two providing lower and upper limits -for the value (y) axis. Use \code{NA} to refer to the existing minimum or -maximum.} - \item{llim}{A numeric vector of length two providing lower and upper limits for the length (x) axis when \code{size_axis = "l"}. Use \code{NA} to refer to the existing minimum or maximum. Only applies to \code{ArraySpeciesBySize}.} +\item{ylim}{A numeric vector of length two providing lower and upper limits +for the value (y) axis. Use \code{NA} to refer to the existing minimum or +maximum.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/plotCDF.Rd b/man/plotCDF.Rd index 6f622d4dc..6b8dbec50 100644 --- a/man/plotCDF.Rd +++ b/man/plotCDF.Rd @@ -15,8 +15,8 @@ plotCDF(object, ...) time_range, geometric_mean = FALSE, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, @@ -35,8 +35,8 @@ plotCDF(object, ...) object, species = NULL, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, @@ -57,6 +57,7 @@ plotlyCDF( time_range, geometric_mean = FALSE, wlim = c(NA, NA), + llim = c(NA, NA), ylim = c(NA, NA), power = 1, biomass = TRUE, @@ -67,7 +68,6 @@ plotlyCDF( normalise = TRUE, log_x = TRUE, log = NULL, - llim = c(NA, NA), size_axis = c("w", "l"), ... ) @@ -99,16 +99,16 @@ the fish grid) or \code{min(params@w)} when \code{resource = FALSE}; the upper default is \code{max(params@w_full)}. Data is filtered to this range and the axis limits are set accordingly.} -\item{ylim}{A numeric vector of length two providing lower and upper limits -for the y axis. Use NA to auto-scale to the data range. Values below 1e-20 -are always filtered out from the data regardless of \code{ylim[1]}. Data above -\code{ylim[2]} is filtered and the upper axis limit is set accordingly.} - \item{llim}{A numeric vector of length two providing lower and upper limits for the length axis when \code{size_axis = "l"}. Use \code{NA} to auto-scale to the data range. Data is filtered to this range and the axis limits are set accordingly.} +\item{ylim}{A numeric vector of length two providing lower and upper limits +for the y axis. Use NA to auto-scale to the data range. Values below 1e-20 +are always filtered out from the data regardless of \code{ylim[1]}. Data above +\code{ylim[2]} is filtered and the upper axis limit is set accordingly.} + \item{power}{The abundance is plotted as the number density times the weight raised to \code{power}. The default \code{power = 1} gives the biomass density, whereas \code{power = 2} gives the biomass density with respect diff --git a/man/plotRelative.Rd b/man/plotRelative.Rd index c197975a5..e79390d86 100644 --- a/man/plotRelative.Rd +++ b/man/plotRelative.Rd @@ -17,8 +17,8 @@ plotRelative(x, y, ...) all.sizes = FALSE, log_x = TRUE, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -46,8 +46,8 @@ plotRelative(x, y, ...) all.sizes = FALSE, log_x = TRUE, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), size_axis = c("w", "l"), total = FALSE, background = TRUE, @@ -72,14 +72,14 @@ spectra and \code{FALSE} for time series.} for the weight (x) axis. Use \code{NA} to refer to the existing minimum or maximum. Only applies to \code{ArraySpeciesBySize}.} -\item{ylim}{A numeric vector of length two providing lower and upper limits -for the value (y) axis. Use \code{NA} to refer to the existing minimum or -maximum.} - \item{llim}{A numeric vector of length two providing lower and upper limits for the length (x) axis when \code{size_axis = "l"}. Use \code{NA} to refer to the existing minimum or maximum. Only applies to \code{ArraySpeciesBySize}.} +\item{ylim}{A numeric vector of length two providing lower and upper limits +for the value (y) axis. Use \code{NA} to refer to the existing minimum or +maximum.} + \item{size_axis}{Whether to plot size as weight (\code{"w"}, default) or length (\code{"l"}), using the allometric weight-length relationship.} diff --git a/man/plotSpectra.Rd b/man/plotSpectra.Rd index cec41b26a..cd0546010 100644 --- a/man/plotSpectra.Rd +++ b/man/plotSpectra.Rd @@ -15,8 +15,8 @@ plotSpectra(object, ...) time_range, geometric_mean = FALSE, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, @@ -35,8 +35,8 @@ plotSpectra(object, ...) object, species = NULL, wlim = c(NA, NA), - ylim = c(NA, NA), llim = c(NA, NA), + ylim = c(NA, NA), power = 1, biomass = TRUE, total = FALSE, @@ -57,6 +57,7 @@ plotlySpectra( time_range, geometric_mean = FALSE, wlim = c(NA, NA), + llim = c(NA, NA), ylim = c(NA, NA), power = 1, biomass = TRUE, @@ -67,7 +68,6 @@ plotlySpectra( log_x = TRUE, log_y = TRUE, log = NULL, - llim = c(NA, NA), size_axis = c("w", "l"), ... ) @@ -99,16 +99,16 @@ the fish grid) or \code{min(params@w)} when \code{resource = FALSE}; the upper default is \code{max(params@w_full)}. Data is filtered to this range and the axis limits are set accordingly.} -\item{ylim}{A numeric vector of length two providing lower and upper limits -for the y axis. Use NA to auto-scale to the data range. Values below 1e-20 -are always filtered out from the data regardless of \code{ylim[1]}. Data above -\code{ylim[2]} is filtered and the upper axis limit is set accordingly.} - \item{llim}{A numeric vector of length two providing lower and upper limits for the length axis when \code{size_axis = "l"}. Use \code{NA} to auto-scale to the data range. Data is filtered to this range and the axis limits are set accordingly.} +\item{ylim}{A numeric vector of length two providing lower and upper limits +for the y axis. Use NA to auto-scale to the data range. Values below 1e-20 +are always filtered out from the data regardless of \code{ylim[1]}. Data above +\code{ylim[2]} is filtered and the upper axis limit is set accordingly.} + \item{power}{The abundance is plotted as the number density times the weight raised to \code{power}. The default \code{power = 1} gives the biomass density, whereas \code{power = 2} gives the biomass density with respect