R/plot_boxplot_facet.R

Defines functions plot_boxplot_facet

Documented in plot_boxplot_facet

#' plot_boxplot_facet
#'
#' plot a box plot
#'
#' @param df_ data frame or tibble
#' @param factor_ factor on the x-axis
#' @param value_var response variable
#' @param facet_var optional: a second factor to split the plot with facet
#' @param xlab xlab
#' @param ylab ylab
#' @param my_title title of the plot
#'
#' @return
#' @export
#'
#' @examples
#' plot_boxplot_facet(pbmc_div_mut, "status", "mutations")
plot_boxplot_facet <- function(df_, factor_, value_var, facet_var=NULL, xlab=NULL, ylab=NULL, my_title=NULL) {
  my_theme <- igfuns::my_theme()
  p <-
    ggplot(df_, aes(x=!!sym(factor_), y=!!sym(value_var), color=!!sym(factor_))) +
    geom_boxplot() +
    my_theme

  if (!is.null(facet_var)) {
    p <- p + facet_grid(cols = vars(!!sym(facet_var)))
  }
  if (!is.null(xlab))
    p <- p + xlab(xlab)
  if (!is.null(ylab))
    p <- p + ylab(ylab)
  if (!is.null(my_title))
    p <- p + ggtitle(my_title)
  p
}
thierrycnam/igfuns documentation built on May 4, 2020, 3:21 a.m.