R/plot_ecdf.R

Defines functions plot_ecdf

Documented in plot_ecdf

#' Plot ECDF
#'
#' Less typing to get \code{ggplot2::stat_ecdf}.
#' Can accept dataframe with bare or quoted name,
#' or vector.
#'
#' @param data Either a vector, or a dataframe in which to find \code{var}.
#' @param var When \code{data} is a dataframe, bare or quoted name of column to use.
#' @export
#' @examples
#' plot_ecdf(mtcars$mpg)
#' plot_ecdf(mtcars, mpg)
#' plot_ecdf(mtcars, "mpg")

#**********************************************************
plot_ecdf = function(data, var = NULL){

  if (is.data.frame(data)){

    # this will do nothing if var is a string
    var <- as.character(substitute(var))

    vector <- data[[var]]

  } else if (is.null(var)){

    vector <- data

  } else {
    stop("Cannot parse inputs.")
  }

  ggplot2::ggplot(data = data.frame(x = vector), ggplot2::aes(x = x)) +
    ggplot2::stat_ecdf() +
    ggplot2::scale_y_continuous(breaks = seq(0, 1, by = 0.2)) +
    ggplot2::labs(y = "Cumulative proportion")
}
#**********************************************************
wepelham3/sack2 documentation built on Aug. 5, 2023, 5:53 a.m.