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
)
A data.frame with "year"
, "length"
, and "frequency"
headers. Extra columns
may aso be utilized to define facets using the rows and cols arguments.
Column to use to define facet rows.
Column to use to define facet columns.
Label for x-axis.
Label for y-axis.
Scaling factor controlling the height of the ridges.
Value controling the transparancy of the ridges.
Function such as ggplot2::scale_colour_viridis_d for defining line colours to use across years.
Function such as ggplot2::scale_fill_viridis_d for defining fill colours to use across years.
Should zeros be replaced by NA values? (i.e., impose blank space in ridge plot).
Returns a ggplot2 object that can be modified further by functions such as ggplot2::theme()
.
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