R/basic_plots.R

Defines functions plotRawData plotRateVsTrt

Documented in plotRateVsTrt plotRawData

#-----------------Tested Functions - Mac OS and Windows
#
#' A Function to plot rate vs desired treatment.
#'
#' This function plots rates against the desired treatment.
#' @param data A data frame generated by the calsSpeed function. It should have the summarized treatments and respective GR values.
#' @param x should indicate the treatment column name for the x axis (e.g., "GermTemp", "GermWP" or others).
#' @param y should indicate the rate column name for the y axis if different than GR50 (e.g., "GR10", "GR90", etc).
#' @keywords plot rates Temperature
#' @export
#' @examples
#' "foo"

plotRateVsTrt <- function(data, x = "TrtID", y = "GR50") {

  # check if data is valid
  if (!is.data.frame(data)) stop("Data is not a valid data frame.")
  if (!is.element(x, names(data))) stop("X-axis column '", x, "' not found in data frame.")
  if (!is.element(y, names(data))) stop("Y-axis column '", y, "' not found in data frame.")

  # make sure x axis and color are treated as discrete
  data[[x]] <- as.character(data[[x]])

  ggplot2::ggplot(data, ggplot2::aes_string(x = x, y = y, color = x)) +
    ggplot2::geom_point(shape = 19, size = 2) +
    ggplot2::expand_limits(x = 0, y = 0) +
    theme_scatter_plot
}


#' A Function to plot the cumulative raw data selected.
#'
#' This function plots the raw data previously selected. It will plot raw cumulative curves over time.
#' @param data A data frame with time course data for one or two treatments.
#' @param color The treatment to be mapped to color on the plot.
#' @param shape The treatment to be mapped to shape on the plot.
#' @param line Should the line overlay be added?
#' @param cum.time Column name for cumulative elapsed time.
#' @param cum.frac Column name for cumulative fraction germinated.
#' @keywords plot raw data
#' @export
#' @examples
#' "foo"

plotRawData <- function(data, color = NULL, shape = NULL, line = TRUE, cum.time = "CumTime", cum.frac = "CumFraction") {

  # check if data is valid
  if (!is.data.frame(data)) stop("Data is not a valid data frame.")
  if (!is.null(color) && !is.element(color, names(data))) stop("Color column '", color, "' not found in data frame.")
  if (!is.null(shape) && !is.element(shape, names(data))) stop("Shape column '", shape, "' not found in data frame.")
  if (is.null(cum.time) || !is.element(cum.time, names(data))) stop("Cumulative time column '", cum.time, "' not found in the data frame.")
  if (is.null(cum.frac) || !is.element(cum.frac, names(data))) stop("Cumulative fraction column '", cum.frac, "' not found in the data frame.")

  # make sure color and shape are treated as discrete
  if (!is.null(color)) data[[color]] <- as.character(data[[color]])
  if (!is.null(shape)) data[[shape]] <- as.character(data[[shape]])

  # Plot raw data
  plt <- ggplot2::ggplot(data, ggplot2::aes_string(x = cum.time, y = cum.frac, color = color, shape = shape )) +
    ggplot2::scale_y_continuous(labels = scales::percent, expand = c(0, 0), limits = c(0, 1.02)) +
    ggplot2::scale_x_continuous(expand = c(0, 0)) +
    ggplot2::expand_limits(x = 0, y = 0) +
    ggplot2::labs(
      x = "Time",
      y = "Cumulative fraction germinated (%)") +
    theme_scatter_plot
  if (is.null(shape)) {
    plt <- plt + ggplot2::geom_point(shape = 19, size = 2)
  } else {
    plt <- plt + ggplot2::geom_point()
  }
  if (line == TRUE) plt <- plt + ggplot2::geom_line()
  plt
}


# ggplot package theme ----------------------------------------------------------------------------------
theme_scatter_plot <- ggplot2::theme(
  legend.background = ggplot2::element_blank(),
  legend.key = ggplot2::element_blank(),
  legend.title = ggplot2::element_text(size = 12, color = "black"),
  legend.text = ggplot2::element_text(size = 12, color = "black"),
  panel.background = ggplot2::element_blank(),
  panel.grid.minor.y = ggplot2::element_blank(),
  panel.grid.major.x = ggplot2::element_blank(),
  panel.grid.major.y = ggplot2::element_blank(),
  panel.grid.minor.x = ggplot2::element_blank(),
  panel.border = ggplot2::element_rect(colour = "grey50", fill = NA, size = 0.5),
  strip.background = ggplot2::element_rect(colour = "black", fill = "white"),
  axis.ticks = ggplot2::element_line(color = "black", size = 0.5),
  axis.text = ggplot2::element_text(size = 12, color = "black"),
  axis.title = ggplot2::element_text(size = 14, color = "black", face = "bold"),
  axis.title.x = ggplot2::element_text(margin = ggplot2::margin(t = 10, r = 0, b = 0, l = 0)))




#----------------------New Development - Under Testing
pedrobello/pbtm documentation built on May 17, 2022, 3:37 a.m.