R/plotVolume.R

#' Plot volume per day
#'
#' This function plots the calculated volume (reps * weights) per day.
#'
#' @param df A dataframe
#' @param group_workouts Whether to split workouts or show it all grouped. True = Grouped
#' @param fit Fit the data, using `lm` or `loess`
#' @param facet Whether to facet your workouts, defaults to True
#'
#' @return ggplot object
#'
#' @export
#'
#' @importFrom magrittr '%>%'
#' @importFrom ggplot2 ggplot aes geom_point geom_smooth geom_line scale_x_date facet_wrap  geom_text theme position_stack element_text labs
#' @importFrom scales date_format
#' @importFrom dplyr summarise
#' @importFrom lubridate wday
#'
#' @examples
#' df <- simData(25, 1)
#' plotVolume(df, group_workouts = FALSE)
plotVolume <-
  function(df,
             group_workouts = TRUE,
             fit = "lm",
             facet = TRUE) {
    gg <- df %>%
      dplyr::select(date, workoutName, weightKg, reps) %>%
      dplyr::group_by(date, workoutName) %>%
      dplyr::summarise(Volume = sum(reps * weightKg)) %>%
      ggplot2::ggplot(.) +
      ggplot2::scale_x_date(
        date_breaks = "month",
        labels = scales::date_format("%B '%y")
      ) +
      scale_color_HS("main") +
      ggplot2::theme(axis.text.x = element_text(
        angle = 90,
        hjust = 0.95,
        vjust = 0.2
      )) +
      ggplot2::labs(title = "Volume per day", x = NULL, y = "Volume (kg)")
    if (facet) {
      gg <-
        gg + ggplot2::facet_wrap(~workoutName, scales = "fixed")
    }
    if (group_workouts) {
      gg <- gg + ggplot2::geom_point(aes(
        x = date,
        y = Volume
      ), alpha = 1) +
        ggplot2::geom_line(aes(
          x = date,
          y = Volume
        ),
        alpha = 0.4
        )
    } else {
      gg <- gg + ggplot2::geom_point(aes(
        x = date,
        y = Volume,
        color = workoutName
      )) +
        ggplot2::geom_line(aes(
          x = date,
          y = Volume,
          color = workoutName
        ),
        alpha = 0.4
        )
    }
    if (fit == "lm") {
      gg <- gg + ggplot2::geom_smooth(
        aes(
          x = date,
          y = Volume
        ),
        linetype = 2,
        method = "lm",
        color = "black",
        alpha = 0.4,
        se = T
      )
    }
    if (fit == "loess") {
      gg <- gg + ggplot2::geom_smooth(
        aes(
          x = date,
          y = Volume
        ),
        linetype = 2,
        method = "loess",
        color = "black",
        alpha = 0.4,
        se = T
      )
    }
    return(gg)
  }
MarijnJABoer/HeavySetR documentation built on May 22, 2019, 5:31 p.m.