R/post_process_plot.R

#' @title Post Process Plot
#' @description Take a list of plots to combine them and arrange them within groups
#' @param plotlist A list of ggplot objects
#' @param output_param A string containing the header name of an output parameter
#' @param var_unit Output parameter unit of measurement
#' @return A grob object showing all plots within group together compared to the output parameter
#' @importFrom tools toTitleCase
#' @importFrom latex2exp TeX
#' @importFrom grid textGrob rectGrob gTree gpar gList
#' @importFrom gridExtra grid.arrange arrangeGrob
#' @importFrom cowplot plot_grid
post_process_plot <- function(plotlist, output_param, var_unit) {
  y_lab_raw <- tools::toTitleCase(gsub("_", " ", output_param))
  y_lab_clean <- TeX(paste("Mean", y_lab_raw, var_unit))
  y_grob <- textGrob(y_lab_clean, rot = 90)

  grid_plot <- cowplot::plot_grid(plotlist = plotlist, nrow = 1)
  ga <- arrangeGrob(grid_plot, left = y_grob)
  gb <- rectGrob(height = 1, width = 1,
                 gp = gpar(lwd = 3, col = "grey77", fill = NA)) # border, no fill
  gt <- gTree(children = gList(ga, gb))
  grid.arrange(gt)
}
dylanjm/meplotR documentation built on May 9, 2019, 1:08 a.m.