Function for producing a faceted ridge plot of annual length frequencies using the ggridges package.

plot_lengths(
  data,
  rows = NULL,
  cols = NULL,
  xlab = "Length (cm)",
  ylab = "Year",
  scale = 5,
  alpha = 0.8,
  scale_color = scale_color_viridis_d,
  scale_fill = scale_fill_viridis_d,
  drop_zeros = TRUE
)

Arguments

data

A data.frame with "year", "length", and "frequency" headers. Extra columns may aso be utilized to define facets using the rows and cols arguments.

rows

Column to use to define facet rows.

cols

Column to use to define facet columns.

xlab

Label for x-axis.

ylab

Label for y-axis.

scale

Scaling factor controlling the height of the ridges.

alpha

Value controling the transparancy of the ridges.

scale_color

Function such as ggplot2::scale_colour_viridis_d for defining line colours to use across years.

scale_fill

Function such as ggplot2::scale_fill_viridis_d for defining fill colours to use across years.

drop_zeros

Should zeros be replaced by NA values? (i.e., impose blank space in ridge plot).

Value

Returns a ggplot2 object that can be modified further by functions such as ggplot2::theme().

Examples


library(ggplot2)

## Simulate some data
set.seed(3)
samples_per_year <- 1000
years <- 2000:2022
lambdas <- rnorm(length(years), mean = 25, sd = 10)
data <- lapply(seq_along(years), function(i) {
    lengths <- rpois(samples_per_year, lambda = lambdas[i])
    lf <- table(lengths)
    data.frame(year = years[i], length = as.numeric(names(lf)), frequency = as.numeric(unname(lf)))
})
data <- do.call(rbind, data)

## Using defaults
plot_lengths(data)
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead


## Customize using ggplot functions
plot_lengths(data, alpha = 0.1, scale = 2) +
scale_color_manual(values = rep("grey30", length(unique(data$year)))) +
scale_fill_manual(values = rep("grey30", length(unique(data$year))))
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.
#> Scale for fill is already present.
#> Adding another scale for fill, which will replace the existing scale.
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead
#> Warning: font family 'Cambria' not found, will use 'sans' instead