Skip to content

Commit

Permalink
Add function for producing ridge plots from annual length frequency data
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulRegular committed Jun 13, 2022
1 parent e4067e5 commit f8e0f61
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -26,6 +27,7 @@ Imports:
rmarkdown,
ggplot2,
ggthemes,
ggridges,
magrittr,
flextable,
officer,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -15,6 +16,7 @@ export(word_stacfis)
import(bookdown)
import(flextable)
import(ggplot2)
import(ggridges)
import(ggthemes)
import(showtext)
importFrom(magrittr,"%>%")
Expand Down
75 changes: 75 additions & 0 deletions R/figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

}

2 changes: 1 addition & 1 deletion SCR/short_presentation/NAFOdown.Rmd
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
73 changes: 73 additions & 0 deletions man/plot_lengths.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f8e0f61

Please sign in to comment.