R/plot_by_treatment.R

Defines functions plot_by_treatment

#' @export
plot_by_treatment <- function(data, outcome, complete_case = T,
                              type = "boxplot", output = F, summary = T) {

  ##### Error checking #########################################################

  internal_data <- data
  if (missing(outcome)) {
    stop("outcome must be supplied.")
  }
  outcome       <- substitute(outcome)
  check_data(internal_data, outcome, "continuous")
  check_logical(complete_case, "complete_case")
  if (complete_case) {
    internal_data_cc <- internal_complete_case_data(internal_data, outcome)
    if (summary) {
      message(" Converted the data to complete-case form. This removed ",
              nrow(internal_data) - nrow(internal_data_cc),
              " rows from the dataset")
    }
    if (nrow(internal_data_cc) == 0) {
      stop("The complete-case dataset consists of zero outcomes")
    }
    internal_data <- internal_data_cc
  }
  check_belong(type, "type", c("boxplot", "violinplot", "ridgeplot"), 1)
  check_logical(output, "output")
  check_logical(summary, "summary")

  ##### Main computations ######################################################

  if (summary) {
    message("   Beginning construction of plot...")
  }
  plot   <- ggplot2::ggplot(data, ggplot2::aes_string(x = outcome,
                                                      y = "treatment",
                                                      fill = "treatment")) +
              ggthemes::scale_fill_ptol() + theme_xover() +
              ggplot2::ylab("Treatment")
  if (type == "boxplot") {
    plot <- plot + ggplot2::geom_boxplot()
  } else if (type == "violinplot") {
    plot <- plot + ggplot2::geom_violin(scale = "count")
  } else {
    plot <- plot + ggridges::geom_density_ridges()
  }
  print(plot)
  if (summary) {
    message("...completed construction of plot.")
  }


  ##### Outputting #############################################################

  if (output) {
    if (summary) {
      message("   Outputting.")
    }
    return(plot)
  }
}
mjg211/xover documentation built on Oct. 16, 2019, 10:46 a.m.