R/plot_traits.R

Defines functions plot_traits

Documented in plot_traits

#' Plot individual traits through time
#'
#' Plot the densities of each trait across the population and through time using
#' \code{geom_bin2d}.
#'
#' @param data An individual-level dataset containing trait values for each
#' individual (see \code{?read_individuals}).
#' @param burnin_bar Whether to add a vertical bar at time point zero
#'
#' @return A ggplot
#'
#' @seealso \code{read_individuals}
#'
#' @examples
#'
#' \dontrun{
#'
#' root <- system.file("extdata", "sim-example", package = "speciomer")
#' data <- read_individuals(root, "individual_traits", ncol = 3)
#' plot_traits(data)
#'
#' }
#'
#' @export

# Function to plot individual traits through time
plot_traits <- function(data, burnin_bar = TRUE) {

  .data <- NULL # hack for check to pass

  plot <- data %>%
    tidyr::pivot_longer(.data$trait1:.data$trait3, names_to = "trait") %>%
    dplyr::mutate(trait = stringr::str_remove(.data$trait, "trait")) %>%
    dplyr::mutate(trait = recode_traits(.data$trait)) %>%
    ggplot2::ggplot(ggplot2::aes(x = .data$time / 1000, y = .data$value)) +
    ggplot2::geom_bin2d(bins = 100) +
    ggplot2::facet_grid(. ~ trait) +
    ggplot2::scale_fill_continuous(type = "viridis") +
    ggplot2::xlab(parse(text = "'Time ('*10^3~'generations)'")) +
    ggplot2::ylab("Trait value") +
    ggplot2::labs(fill = "Count")

  if (burnin_bar) plot  <- plot +
      ggplot2::geom_vline(xintercept = 0, linetype = 4)

  return(plot)

}
rscherrer/speciomer documentation built on March 11, 2023, 5:37 p.m.