R/plot_violin_facet.R

Defines functions plot_violin_facet

Documented in plot_violin_facet

#' plot_violin_facet
#'
#' Plot a violin plot with ggplot facet_grid to assess data distribution based on a qualitative variable.
#'
#' @param factor_ qualitative variable/factor
#' @param facet_var factor to use for facet_grid
#' @param value_var explanatory variable
#' @param xlab xlab
#' @param ylab ylab
#' @param my_title title for the plot
#' @param df_ data frame
#' @param adjust_ smoothing parameter
#'
#' @importFrom stats median
#' @return
#' @export
#'
#' @examples
plot_violin_facet <- function(df_, factor_, value_var, facet_var=NULL, adjust_=1, 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_violin(adjust=adjust_) +
      my_theme +
      geom_boxplot(width = 0.1, fill="black", outlier.color = NA) +
      stat_summary(fun.y=median, geom="point", fill="white", shape=21, size=2.5)

  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.