diff --git a/DESCRIPTION b/DESCRIPTION index 8ade961..feb0444 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,6 +6,7 @@ Authors@R: role = c("aut", "cre")), person("Bob", "Rogers", role = "aut"), person("Laura", "Wheeland", role = "ctb"), + person("Andrea", "Perreault", role = "ctb"), person("Sean", "Anderson", role = "ctb", comment = "Development of csasdown"), person("Chris", "Grandin", role = "ctb", comment = "Development of csasdown"), person("Chester", "Ismay", role = "ctb", comment = "Development of thesisdown"), @@ -26,6 +27,7 @@ Imports: rmarkdown, ggplot2, ggthemes, + ggridges, magrittr, flextable, officer, diff --git a/NAMESPACE b/NAMESPACE index cfa109a..566557c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,6 +7,7 @@ export(.nafo_lwd) export(.nafo_width) export(draft) export(nafo_summary_table) +export(plot_lengths) export(theme_nafo) export(theme_nafotabs) export(word_scr) @@ -15,6 +16,7 @@ export(word_stacfis) import(bookdown) import(flextable) import(ggplot2) +import(ggridges) import(ggthemes) import(showtext) importFrom(magrittr,"%>%") diff --git a/R/figures.R b/R/figures.R index 3d58d6b..160bc7d 100644 --- a/R/figures.R +++ b/R/figures.R @@ -88,3 +88,78 @@ NULL #' @rdname nafo-vals .nafo_asp <- 6.4 / 11.5 + + + +#' Generate ridges plot from annual length frequency data +#' +#' @description Function for producing a faceted ridge plot of annual length frequencies using the ggridges package. +#' +#' @param 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. +#' @param rows Column to use to define facet rows. +#' @param cols Column to use to define facet columns. +#' @param xlab Label for x-axis. +#' @param ylab Label for y-axis. +#' @param scale Scaling factor controlling the height of the ridges. +#' @param alpha Value controling the transparancy of the ridges. +#' @param scale_color Function such as [ggplot2::scale_colour_viridis_d] for defining line +#' colours to use across years. +#' @param scale_fill Function such as [ggplot2::scale_fill_viridis_d] for defining fill +#' colours to use across years. +#' @param drop_zeros Should zeros be replaced by NA values? (i.e., impose blank space in ridge plot). +#' +#' @return Returns a ggplot2 object that can be modified further by functions such as [ggplot2::theme()]. +#' +#' @export +#' +#' @examples +#' +#' ## 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) +#' +#' ## 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)))) +#' +#' +#' @import ggridges +#' +plot_lengths <- function(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) { + + if (drop_zeros) data$freq[data$freq == 0] <- NA + + ggplot(data) + + theme_ridges(grid = TRUE, center_axis_labels = TRUE, font_size = 3.5) + + geom_density_ridges(aes(x = as.numeric(length), y = as.factor(year), + height = as.numeric(frequency), fill = as.factor(year), + color = as.factor(year)), + stat = "identity", scale = scale, alpha = alpha, show.legend = FALSE) + + facet_grid(rows = rows, cols = cols, scales = "free_y", space = "free_y") + + scale_color() + scale_fill() + + labs(x = xlab, y = ylab)+ + theme_nafo()+ + theme(panel.background = element_blank(), + panel.grid = element_blank(), + panel.spacing.x = unit(0,"line")) + +} + diff --git a/SCR/short_presentation/NAFOdown.Rmd b/SCR/short_presentation/NAFOdown.Rmd index ff5c84e..c03780a 100644 --- a/SCR/short_presentation/NAFOdown.Rmd +++ b/SCR/short_presentation/NAFOdown.Rmd @@ -1,7 +1,7 @@ --- title: "NAFOdown" subtitle: "An R Markdown Template for Producing NAFO Scientific Council Documents" -author: "Paul M. Regular, Bob Rodgers, Laura Wheeland, Sean C. Anderson" +author: "Paul M. Regular, Bob Rogers, Laura Wheeland, Sean C. Anderson" date: "`r Sys.Date()`" output: ioslides_presentation: diff --git a/man/plot_lengths.Rd b/man/plot_lengths.Rd new file mode 100644 index 0000000..f168f7f --- /dev/null +++ b/man/plot_lengths.Rd @@ -0,0 +1,73 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/figures.R +\name{plot_lengths} +\alias{plot_lengths} +\title{Generate ridges plot from annual length frequency data} +\usage{ +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{ +\item{data}{A data.frame with \code{"year"}, \code{"length"}, and \code{"frequency"} headers. Extra columns +may aso be utilized to define facets using the rows and cols arguments.} + +\item{rows}{Column to use to define facet rows.} + +\item{cols}{Column to use to define facet columns.} + +\item{xlab}{Label for x-axis.} + +\item{ylab}{Label for y-axis.} + +\item{scale}{Scaling factor controlling the height of the ridges.} + +\item{alpha}{Value controling the transparancy of the ridges.} + +\item{scale_color}{Function such as \link[ggplot2:scale_viridis]{ggplot2::scale_colour_viridis_d} for defining line +colours to use across years.} + +\item{scale_fill}{Function such as \link[ggplot2:scale_viridis]{ggplot2::scale_fill_viridis_d} for defining fill +colours to use across years.} + +\item{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 \code{\link[ggplot2:theme]{ggplot2::theme()}}. +} +\description{ +Function for producing a faceted ridge plot of annual length frequencies using the ggridges package. +} +\examples{ + +## 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) + +## 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)))) + + +}