On this page

Plot the mean length of each age class of a fitted model by time step, with optional observed means from age readings.

Usage

plot_length_at_age(
  fit,
  observed = NULL,
  basis = c("population", "survey"),
  years = NULL,
  ages = NULL,
  facet_by = c("age", "none"),
  line_size = NULL,
  line_color = NULL,
  line_type = "solid",
  point_size = NULL,
  observed_color = NULL,
  observed_name = "Age readings",
  facet_ncol = NULL,
  facet_scales = "free",
  return_data = FALSE,
  title = NULL,
  xlab = NULL,
  ylab = NULL,
  font_family = NULL,
  title_size = NULL,
  axis_title_size = NULL,
  axis_text_size = NULL,
  strip_text_size = NULL,
  legend_text_size = NULL,
  x_breaks = NULL,
  base_theme = NULL,
  title_hjust = NULL
)

Arguments

ArgumentDefaultDescription
fitrequired

An ACL or ALSCL fit, or a saved fit read with read_assessment().

observedNULL

NULL or a data frame of observed mean length at age from age readings, drawn as points. It needs the columns age (an age class of the fit, as in fit$age), mean_length and year (the time step, on the scale of fit$year), and may have sd_length (or sd), drawn as bars of one standard deviation on each side of the mean, or lower and upper, drawn as the bars of an interval. The output of length_at_age() has these columns.

basisc("population", "survey")

Character. "population" (default) gives mean length at age in the population, and "survey" mean length at age in the survey catch, with numbers at length multiplied by the survey catchability at length used in the fit. Use "survey" to compare with age readings of fish caught in the survey. Letter case does not matter.

yearsNULL

NULL (all time steps) or the time steps to draw, as values of fit$year.

agesNULL

NULL (all age classes) or the age classes to draw, as values of fit$age.

facet_byc("age", "none")

Character. "age" (default) draws one panel per age class; "none" draws one panel with one line per age class, colored by age class. Letter case does not matter.

line_sizeNULL

Numeric or NULL. Line width. NULL inherits the global line_size setting.

line_colorNULL

Character or NULL. Line color with facet_by = "age"; NULL inherits the global line_color setting. With facet_by = "none" the lines take the colors of the global palette.

line_type"solid"

Character. Line type, such as "solid" or "dashed". Default "solid".

point_sizeNULL

Numeric or NULL. Size of the observed points; NULL inherits the global point_size setting (default 2.5).

observed_colorNULL

Character or NULL. Color of the observed points with facet_by = "age"; NULL inherits the global observed_color setting. With facet_by = "none" the points take the color of their age class.

observed_name"Age readings"

Character. Name of the observed points in the legend and the caption. Default is "Age readings".

facet_ncolNULL

Integer or NULL. Number of columns of panels. NULL chooses it from the number of panels, for example 5 × 3 for 15 panels and 6 × 4 for 23.

facet_scales"free"

Character. Axis scales of the panels, "fixed", "free", "free_x" or "free_y". Letter case does not matter.

return_dataFALSE

Logical. TRUE returns a list with the plot and the plotted data, described under the return value. Default FALSE.

titleNULL

NULL (default) draws the default title, FALSE, NA or "" draws no title, and a character string draws that string; see acl_theme_set() for the default titles and the show_title setting.

xlabNULL

Character or NULL. Label of the x axis. NULL uses the global theme setting.

ylabNULL

Character or NULL. Label of the y axis. NULL uses the global theme setting.

font_familyNULL

Character or NULL. Font family of the text, which must be installed on the computer. NULL uses the global theme setting (default "sans").

title_sizeNULL

Numeric or NULL. Size of the plot title in pt. NULL uses the global theme setting (default 14).

axis_title_sizeNULL

Numeric or NULL. Size of the axis titles in pt. NULL uses the global theme setting (default 12).

axis_text_sizeNULL

Numeric or NULL. Size of the axis tick labels in pt. NULL uses the global theme setting (default 10).

strip_text_sizeNULL

Numeric or NULL. Size of the panel labels in pt. NULL uses the global theme setting (default 10).

legend_text_sizeNULL

Numeric or NULL. Size of the legend text in pt. NULL uses the global theme setting (default 10).

x_breaksNULL

Numeric vector or NULL. Positions of the x-axis breaks. NULL inherits the global x_breaks setting when at least one of its values lies on this axis; otherwise the breaks are chosen automatically.

base_themeNULL

Character or NULL. Name of the base ggplot2 theme, such as "bw", "minimal" or "theme_bw". NULL uses the global theme setting. Letter case does not matter.

title_hjustNULL

Numeric or NULL. Horizontal position of the title, 0 for left, 0.5 for centered and 1 for right. NULL uses the global theme setting.

Value

A ggplot object, or a list with plot and data when return_data = TRUE. data has one row per age class and time step, with the columns Year, Age, AgeClass (the panel label), mean_length and sd_length.

Details

The figure shows the mean length of each age class of a fitted model against the time step, from length_at_age(), and optionally the observed mean length at age by time step from age readings. Use it to see how the lengths at age of the model change over time and to compare them with age readings. plot_VB() shows the mean of the same values over the time steps against age.

For an ALSCL fit the values come from the estimated numbers by length bin, age class and time step, so they change over time with growth, recruitment and mortality at length. For an ACL fit they come from the age-length key, which is the same in every time step. The lines are then horizontal on the population basis, and on the survey basis they change only when the supplied survey catchability changes over time. Each length bin is represented by its midpoint, so the mean of an age class with a large share of its fish in the first or the last length bin is drawn toward the midpoint of that bin; for a comparison with age readings, first assign the lengths of the aged fish to the length bins of the model. The oldest age class is a plus group and is marked with a plus sign. The title uses the theme key length_at_age_time, and the axis labels use xlab$year and ylab$growth; the package holds no unit for length, so give it in ylab.

Additional details from R help

The values are those of length_at_age(). For an ALSCL fit they come from the estimated numbers by length bin, age class and time step, so they change over time with growth, recruitment and mortality at length. For an ACL fit they come from the age-length key, which is the same in every time step, so the lines are horizontal on the population basis, and on the survey basis they change only when the supplied survey catchability changes over time. Each length bin is represented by its midpoint, so the mean of an age class with a large share of its fish in the first or the last length bin is drawn toward the midpoint of that bin.

The oldest age class is a plus group and is marked with a plus sign. The title uses the theme key length_at_age_time, and the axis labels use xlab$year and ylab$growth. The package holds no unit for length; give it in ylab, for example ylab = "Length (cm)".

Examples

Run the setup before the example. Fitting examples require an R-compatible C++ toolchain; simulation and export examples may write files.

Example setup
library(ALSCL)
library(ggplot2)
library(patchwork)
data("YTF_example")
x <- YTF_example
inputs <- x[c("data.CatL", "data.wgt", "data.mat")]
dat <- x$data.CatL
a <- run_acl(x, silent = TRUE)
b <- run_alscl(x, silent = TRUE)
# Use objects defined above
plot_length_at_age(b, ages = c(2, 5, 8, 11))
# Survey basis, with mean length at age from age readings by time step (made up here)
readings <- subset(length_at_age(b, basis = "survey"), age %in% c(2, 5) & year %% 2 == 0)
readings$mean_length <- readings$mean_length + rep(c(-1, 1), length.out = nrow(readings))
plot_length_at_age(b, observed = readings, basis = "survey", ages = c(2, 5))
Example figure
Example figure: plot_length_at_age(b, ages = c(2, 5, 8, 11), line_size = 0.6)

See also

Search documentation