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
24 changes: 18 additions & 6 deletions R/plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,18 @@ utils::globalVariables(c("time", "value", "Species", "w", "gear", "Age",
#' @param xtrans Transformation for the x-axis. Often "log10" may be useful
#' instead of the default of "identity".
#' @param ytrans Transformation for the y-axis.
#' @param xlim A numeric vector of length two providing lower and upper limits
#' for the x axis. 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 y axis. Use NA to refer to the existing minimum or maximum.
#' @param y_ticks The approximate number of ticks desired on the y axis
#' @param highlight Name or vector of names of the species to be highlighted.
#' @return A ggplot2 object
#' @keywords internal
#' @export
plotDataFrame <- function(frame, params, style = "line", xlab = waiver(),
ylab = waiver(), xtrans = "identity", ytrans = "identity",
xlim = c(NA, NA), ylim = c(NA, NA),
y_ticks = 6, highlight = NULL, legend_var = NULL,
wrap_var = NULL, wrap_scale = NULL) {
assert_that(is.data.frame(frame),
Expand Down Expand Up @@ -162,13 +167,16 @@ plotDataFrame <- function(frame, params, style = "line", xlab = waiver(),
ybreaks <- waiver()
if (ytrans == "log10") ybreaks <- log_breaks(n = y_ticks)

# Set up axis limits. NA values mean auto-scale to data range.
# The reason why below `group = species` is included in `ggplot()`
# rather than in `geom_line` is because that puts it first in the
# plotly tooltips, due to a bug in plotly.
p <- ggplot(frame, aes(group = .data[[group_var]])) +
scale_y_continuous(trans = ytrans, breaks = ybreaks,
labels = prettyNum, name = ylab) +
scale_x_continuous(trans = xtrans, name = xlab)
labels = prettyNum, name = ylab,
limits = ylim) +
scale_x_continuous(trans = xtrans, name = xlab,
limits = xlim)

switch(style,
"line" = {
Expand Down Expand Up @@ -240,7 +248,8 @@ log_breaks <- function(n = 6) {
#' time series.
#' @param ylim A numeric vector of length two providing lower and upper limits
#' for the y axis. Use NA to refer to the existing minimum or maximum. Any
#' values below 1e-20 are always cut off.
#' values below 1e-20 are always cut off. Data is filtered to this range and
#' the axis limits are set accordingly.
#' @param total A boolean value that determines whether the total biomass from
#' all species is plotted as well. Default is FALSE.
#' @inheritParams plotSpectra
Expand Down Expand Up @@ -321,7 +330,7 @@ plotBiomass <- function(sim, species = NULL,
if (return_data) return(plot_dat)

plotDataFrame(plot_dat, params, xlab = "Year", ylab = "Biomass [g]",
ytrans = "log10",
ytrans = "log10", ylim = ylim,
y_ticks = y_ticks, highlight = highlight,
legend_var = "Legend")
}
Expand Down Expand Up @@ -557,10 +566,12 @@ plotlyYieldGear <- function(sim, species = NULL,
#' If TRUE then the average of the abundances over the
#' time range is a geometric mean instead of the default arithmetic mean.
#' @param wlim A numeric vector of length two providing lower and upper limits
#' for the w axis. Use NA to refer to the existing minimum or maximum.
#' for the w axis. Use NA to refer to the existing minimum or maximum. 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 refer to the existing minimum or maximum. Any
#' values below 1e-20 are always cut off.
#' values below 1e-20 are always cut off. Data is filtered to this range and
#' the axis limits are set accordingly.
#' @param power The abundance is plotted as the number density times the weight
#' raised to `power`. The default \code{power = 1} gives the biomass
#' density, whereas \code{power = 2} gives the biomass density with respect
Expand Down Expand Up @@ -745,6 +756,7 @@ plot_spectra <- function(params, n, n_pp,

plotDataFrame(plot_dat, params, xlab = "Size [g]", ylab = y_label,
xtrans = "log10", ytrans = "log10",
xlim = wlim, ylim = ylim,
highlight = highlight, legend_var = "Legend")
}

Expand Down
48 changes: 24 additions & 24 deletions tests/testthat/_snaps/plots/plot-spectra.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions tests/testthat/test-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,28 @@ test_that("plotDiet restricts to meaningful abundance ranges", {
expect_true(max_w_in_data <= w_max_meaningful)
}
})

# Test axis limits are properly set ----
test_that("axis limits are set correctly", {
# Test ylim in plotBiomass
p <- plotBiomass(sim, species = species, ylim = c(1e3, 1e6))
expect_equal(p$scales$scales[[1]]$limits, c(3, 6))

# Test with NA values
p <- plotBiomass(sim, species = species, ylim = c(NA, 1e6))
expect_equal(p$scales$scales[[1]]$limits[2], 6)
expect_true(is.na(p$scales$scales[[1]]$limits[1]))

# Test wlim and ylim in plotSpectra
p <- plotSpectra(sim, species = species, wlim = c(1, 100), ylim = c(1e5, 1e8))
# x-axis is first scale, y-axis is second
expect_equal(p$scales$scales[[2]]$limits, c(0, 2))
expect_equal(p$scales$scales[[1]]$limits, c(5, 8))

# Test with NA values in wlim
p <- plotSpectra(sim, species = species, wlim = c(10, NA), ylim = c(NA, 1e8))
expect_equal(p$scales$scales[[2]]$limits[1], 1)
expect_equal(p$scales$scales[[2]]$limits[2], log10(max(params@w_full)))
expect_equal(p$scales$scales[[1]]$limits[1], -20)
expect_equal(p$scales$scales[[1]]$limits[2], 8)
})
Loading