Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ stability of steady states.

## Bug fixes

- `plotCDF()`, `plotCDF2()`, `plotSpectra2()`, and `plotSpectraRelative()` now
support `size_axis = "l"` together with `total = TRUE`. The total spectrum and
cumulative distributions are summed across species on the length axis.

- `ArrayResourceBySize` and `ArrayTimeByResourceBySize` plotting methods
(`plot2()`, `plotRelative()`, `addPlot()`) now support `size_axis = "l"` and
`per_log_size`, providing symmetry with species size-spectrum plotting methods.

- `plotFeedingLevel()` and `plotlyFeedingLevel()` have been refactored to
delegate directly to `plot()` / `plotHover()` on `ArraySpeciesBySize` while
maintaining full support for `include_critical = TRUE` and non-clipping
proportion coordinate scaling.

- `plotYield()` for a single simulation now delegates directly to
`plot(getYield(object))`. The `sim2` argument of `plotYield()` is deprecated in
favour of `plot2(getYield(sim1), getYield(sim2))`.

- `plot(getFluxGradient(params), size_axis = "l")` now converts its values to a
density with respect to length, and labels them `cm^-1/year`. The flux
gradient is a rate of change of a number density, but it was not recognised as
Expand Down
84 changes: 64 additions & 20 deletions R/ArrayResourceBySize-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,28 +239,41 @@ plot2.ArrayResourceBySize <- function(x, y, name1 = "First", name2 = "Second",
ylim = c(NA, NA),
total = FALSE, background = TRUE,
y_ticks = 6,
wlim = c(NA, NA), ...) {
wlim = c(NA, NA), llim = c(NA, NA),
size_axis = c("w", "l"),
per_log_size = NULL, ...) {
check_plot2_compatible(x, y, "ArrayResourceBySize")
compare_array_metadata(x, y)
warn_unused_resource_args(species, total, background)
size_axis <- plot_size_axis(size_axis)
check_per_log_size(x, per_log_size)
log_y <- array_log_y(x, log_y, log, !missing(log_y))
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 = "Value")
y_label <- array_y_label(x, default = "Value", size_axis = size_axis,
per_log_size = per_log_size)
plot_dat1 <- prepare_ArrayResourceBySize_plot_data(x, wlim = wlim)
plot_dat2 <- prepare_ArrayResourceBySize_plot_data(y, wlim = wlim)

ylim <- array_ylim(x, ylim, log_y, c(plot_dat1[[2]], plot_dat2[[2]]))

plotComparisonDataFrame(plot_dat1, plot_dat2, params,
name1 = name1, name2 = name2,
xlab = "Weight (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, llim),
ylim = ylim,
y_ticks = y_ticks, legend_var = "Legend",
size_axis = size_axis,
density_wrt = array_density_wrt(x),
per_log_size = per_log_size)
}

#' @rdname plotRelative
Expand All @@ -271,22 +284,27 @@ plotRelative.ArrayResourceBySize <- function(x, y, species = NULL,
ylim = c(NA, NA),
total = FALSE,
background = TRUE,
wlim = c(NA, NA), ...) {
wlim = c(NA, NA),
llim = c(NA, NA),
size_axis = c("w", "l"), ...) {
check_plot2_compatible(x, y, "ArrayResourceBySize")
compare_array_metadata(x, y)
warn_unused_resource_args(species, total, background)
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_ArrayResourceBySize_plot_data(x, wlim = wlim)
plot_dat2 <- prepare_ArrayResourceBySize_plot_data(y, wlim = wlim)

plotRelativeDataFrame(plot_dat1, plot_dat2, params,
xlab = "Weight (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, llim),
ylim = ylim,
legend_var = "Legend", size_axis = size_axis)
}

#' @rdname addPlot
Expand All @@ -299,22 +317,36 @@ addPlot.ArrayResourceBySize <- function(plot, x, species = NULL,
linetype = "dashed",
linewidth = 0.8,
alpha = 1,
wlim = c(NA, NA), ...) {
wlim = c(NA, NA),
llim = c(NA, NA),
size_axis = c("w", "l"),
per_log_size = NULL, ...) {
if (!inherits(plot, "ggplot")) {
stop("The `plot` argument must be a ggplot object.")
}
assert_that(is.number(linewidth),
is.number(alpha),
alpha >= 0,
alpha <= 1,
length(wlim) == 2)
length(wlim) == 2,
length(llim) == 2)
warn_unused_resource_args(species, total, background)
size_axis <- plot_size_axis(size_axis)
check_per_log_size(x, per_log_size)

plot <- deep_copy(plot)
plot_dat <- prepare_ArrayResourceBySize_plot_data(x, wlim = wlim)
params <- attr(x, "params")
plot_dat <- convert_plot_density_axis(plot_dat, params, size_axis,
density_wrt = array_density_wrt(x),
per_log_size = per_log_size)
if (identical(size_axis, "l")) {
plot_dat <- filter_plot_length_limits(plot_dat, llim)
}
x_var <- plot_size_x_var(size_axis)
y_var <- names(plot_dat)[2]
check_addPlot_compatible(plot, x_var = "w", y_var = y_var,
units = attr(x, "units"))
check_addPlot_compatible(plot, x_var = x_var, y_var = y_var,
units = array_units(x, size_axis, per_log_size))

# A resource array is a single line, so there is nothing to distinguish by
# colour. Mapping colour to the "Resource" legend level would rely on the
Expand All @@ -330,7 +362,7 @@ addPlot.ArrayResourceBySize <- function(plot, x, species = NULL,
}
}

mapping <- aes(x = .data[["w"]], y = .data[[y_var]],
mapping <- aes(x = .data[[x_var]], y = .data[[y_var]],
group = .data[["Species"]])
if (is.null(linetype)) {
mapping$linetype <- rlang::quo(.data[["Legend"]])
Expand Down Expand Up @@ -656,7 +688,10 @@ plot2.ArrayTimeByResourceBySize <- function(x, y, name1 = "First",
total = FALSE, background = TRUE,
y_ticks = 6,
time = NULL,
wlim = c(NA, NA), ...) {
wlim = c(NA, NA),
llim = c(NA, NA),
size_axis = c("w", "l"),
per_log_size = NULL, ...) {
check_plot2_compatible(x, y, "ArrayTimeByResourceBySize")
slice1 <- ArrayTimeByResourceBySize_slice(x, time = time)
slice2 <- ArrayTimeByResourceBySize_slice(y, time = time)
Expand All @@ -665,7 +700,8 @@ plot2.ArrayTimeByResourceBySize <- function(x, y, name1 = "First",
species = species, log_x = log_x, log_y = log_y,
log = log, ylim = ylim, total = total,
background = background, y_ticks = y_ticks,
wlim = wlim, ...)
wlim = wlim, llim = llim, size_axis = size_axis,
per_log_size = per_log_size, ...)
}

#' @rdname plotRelative
Expand All @@ -677,14 +713,17 @@ plotRelative.ArrayTimeByResourceBySize <- function(x, y, species = NULL,
total = FALSE,
background = TRUE,
time = NULL,
wlim = c(NA, NA), ...) {
wlim = c(NA, NA),
llim = c(NA, NA),
size_axis = c("w", "l"), ...) {
check_plot2_compatible(x, y, "ArrayTimeByResourceBySize")
slice1 <- ArrayTimeByResourceBySize_slice(x, time = time)
slice2 <- ArrayTimeByResourceBySize_slice(y, time = time)

plotRelative.ArrayResourceBySize(slice1, slice2, species = species,
log_x = log_x, ylim = ylim, total = total,
background = background, wlim = wlim, ...)
background = background, wlim = wlim,
llim = llim, size_axis = size_axis, ...)
}

#' @rdname addPlot
Expand All @@ -698,12 +737,17 @@ addPlot.ArrayTimeByResourceBySize <- function(plot, x, species = NULL,
linewidth = 0.8,
alpha = 1,
time = NULL,
wlim = c(NA, NA), ...) {
wlim = c(NA, NA),
llim = c(NA, NA),
size_axis = c("w", "l"),
per_log_size = NULL, ...) {
slice <- ArrayTimeByResourceBySize_slice(x, time = time)
addPlot.ArrayResourceBySize(plot, slice, species = species, total = total,
background = background, colour = colour,
linetype = linetype, linewidth = linewidth,
alpha = alpha, wlim = wlim, ...)
alpha = alpha, wlim = wlim, llim = llim,
size_axis = size_axis,
per_log_size = per_log_size, ...)
}

#' @rdname animate
Expand Down
3 changes: 3 additions & 0 deletions R/ArraySpeciesBySize-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ plot2.ArraySpeciesBySize <- function(x, y, name1 = "First", name2 = "Second",
compare_array_metadata(x, y)
size_axis <- plot_size_axis(size_axis)
check_per_log_size(x, per_log_size)
log_y <- array_log_y(x, log_y, log, !missing(log_y))
log_axes <- parsePlotLog(log, log_x = log_x, log_y = log_y)
log_x <- log_axes$log_x
log_y <- log_axes$log_y
Expand All @@ -551,6 +552,8 @@ plot2.ArraySpeciesBySize <- function(x, y, name1 = "First", name2 = "Second",
y, species = species, all.sizes = all.sizes, wlim = wlim,
total = total, background = background)

ylim <- array_ylim(x, ylim, log_y, c(plot_dat1[[2]], plot_dat2[[2]]))

plotComparisonDataFrame(plot_dat1, plot_dat2, params,
name1 = name1, name2 = name2,
xlab = plot_size_xlab(size_axis), ylab = y_label,
Expand Down
3 changes: 3 additions & 0 deletions R/ArrayTimeBySpecies-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ plot2.ArrayTimeBySpecies <- function(x, y, name1 = "First", name2 = "Second",
tlim = c(NA, NA), ...) {
check_plot2_compatible(x, y, "ArrayTimeBySpecies")
compare_array_metadata(x, y)
log_y <- array_log_y(x, log_y, log, !missing(log_y))
log_axes <- parsePlotLog(log, log_x = log_x, log_y = log_y)
log_x <- log_axes$log_x
log_y <- log_axes$log_y
Expand All @@ -294,6 +295,8 @@ plot2.ArrayTimeBySpecies <- function(x, y, name1 = "First", name2 = "Second",
y, species = species, tlim = tlim,
ylim = ylim, total = total, background = background)

ylim <- array_ylim(x, ylim, log_y, c(plot_dat1[[2]], plot_dat2[[2]]))

plotComparisonDataFrame(plot_dat1, plot_dat2, params,
name1 = name1, name2 = name2,
xlab = "Year", ylab = y_label,
Expand Down
1 change: 1 addition & 0 deletions R/ArrayTimeBySpeciesBySize-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ animate.ArrayTimeBySpeciesBySize <- function(x, species = NULL,
length(wlim) == 2, length(llim) == 2, length(ylim) == 2)
size_axis <- plot_size_axis(size_axis)
check_per_log_size(x, per_log_size)
log_y <- array_log_y(x, log_y, log, !missing(log_y))
log_axes <- parsePlotLog(log, log_x = log_x, log_y = log_y)
log_x <- log_axes$log_x
log_y <- log_axes$log_y
Expand Down
Loading