plot_violin: Plot distribution and results of statistical tests

View source: R/plot-statistics-single.R

plot_violinR Documentation

Plot distribution and results of statistical tests

Description

These functions visualize the distribution of numerical variables via box- and violinplots while simultaneously allowing for statistical tests. See details for more.

Usage

plot_violin(
  df,
  variables = NULL,
  across = NULL,
  across.subset = NULL,
  relevel = TRUE,
  test.pairwise = NULL,
  test.groupwise = NULL,
  ref.group = NULL,
  step.increase = 0.1,
  vjust = 0,
  scales = "free",
  nrow = NULL,
  ncol = NULL,
  display.facets = TRUE,
  display.points = FALSE,
  pt.alpha = 0.8,
  pt.color = "black",
  pt.num = 100,
  pt.shape = 19,
  pt.size = 1.5,
  clrp = "milo",
  clrp.adjust = NULL,
  verbose = TRUE,
  ...
)

plot_violinplot(
  df,
  variables = NULL,
  across = NULL,
  across.subset = NULL,
  relevel = TRUE,
  test.pairwise = NULL,
  test.groupwise = NULL,
  ref.group = NULL,
  step.increase = 0.1,
  vjust = 0,
  scales = "free",
  nrow = NULL,
  ncol = NULL,
  display.facets = TRUE,
  display.points = FALSE,
  pt.alpha = 0.8,
  pt.color = "black",
  pt.num = 100,
  pt.shape = 19,
  pt.size = 1.5,
  clrp = "milo",
  clrp.adjust = NULL,
  verbose = TRUE,
  ...
)

plot_boxplot(
  df,
  variables = NULL,
  across = NULL,
  across.subset = NULL,
  relevel = TRUE,
  test.pairwise = NULL,
  test.groupwise = NULL,
  ref.group = NULL,
  step.increase = 0.1,
  vjust = 0,
  scales = "free",
  nrow = NULL,
  ncol = NULL,
  display.facets = TRUE,
  display.points = FALSE,
  pt.alpha = 0.8,
  pt.color = "black",
  pt.num = 100,
  pt.shape = 19,
  pt.size = 1.5,
  clrp = "milo",
  clrp.adjust = NULL,
  verbose = TRUE,
  ...
)

plot_vioboxplot(
  df,
  variables = NULL,
  across = NULL,
  across.subset = NULL,
  relevel = TRUE,
  test.pairwise = NULL,
  test.groupwise = NULL,
  ref.group = NULL,
  step.increase = 0.1,
  box.width = 0.25,
  vjust = 0,
  scales = "free",
  nrow = NULL,
  ncol = NULL,
  display.facets = TRUE,
  display.points = FALSE,
  pt.alpha = 0.8,
  pt.color = "black",
  pt.num = 100,
  pt.shape = 19,
  pt.size = 1.5,
  clrp = "milo",
  clrp.adjust = NULL,
  fill = NA,
  verbose = TRUE,
  ...
)

Arguments

df

A data.frame.

variables

Character vector. Specifies the variables of interest. If set to NULL all valid variables of the input data.frame are considered (as long as the total number does not exceeds the limit).

If there are more variables you are interested in than they are variables you are not interested in specify those that you are not interested in prefixed with an '-'. Variables prefixed that way are discarded and the remaining are kept.

across

Character value. Specifies the discrete variable in the data.frame across which the variables of interest are to be analyzed or displayed.

across.subset

Character vector. The groups of interest that the grouping variable denoted in across contains.

If there are more groups you are interested in than they are groups you are not interested in specify those that you are not interested in prefixed with an '-'. Variables prefixed that way are discarded and the remaining are kept.

relevel

Logical value. If set to TRUE the input order of across.subset and variables determines the order in which the groups of interest are displayed.

test.pairwise

Character value or NULL. If character, one of 't.test', 'wilcox.test'.

test.groupwise

Character value or NULL. If character, one of 'anova', 'kruskal.test'.

ref.group

Character value. Specifies the reference group for the pairwise statistical test. Must be among the groups the variable specified in across contains. If set to NULL the first group found is taken.

step.increase

Numeric value. Denotes the increase in fraction of total height for every additional comparison to minimize overlap.

vjust

Numeric value. Denotes the relative, vertical position of the results of the test denoted in test.groupwise. Negative input highers, positive input lowers the position.

scales, nrow, ncol

Given to ggplot2::facet_wrap(). Affects the way the subplots are displayed.

display.facets

Logical value. Only relevant if across is set to NULL. Denotes if a subplot for each variable is supposed to be created.

display.points

Logical value. If set to TRUE points are used additionally to display the results.

clrp

Character value. Specifies the color palette to be used to represent groups of discrete variables. Run validColorPalettes() to obtain valid input options.

clrp.adjust

Named character vector or NULL. If character, it adjusts the colors that are used to represent the groups. Names of the input vector must refer to the group and the respective named element denotes the color with which to represent the group.

verbose

Logical. If set to TRUE informative messages regarding the computational progress will be printed.

(Warning messages will always be printed.)

...

Additional arguments given to the respective ggplot2::geom_<plot_type>() function.

Details

Argument variables accepts only values that refer to numerical variables. Use vjust and step.increase to move the results of statistical tests in order to keep the plot aesthetically pleasing.

Value

Returns a ggplot-object that can be additionally customized according to the rules of the ggplot2-framework.

Examples

#Not run:

library(tidyerse)

df <- mtcars

df$cluster_kmeans <-
  stats::kmeans(x = mtcars, centers = 4)$cluster %>%
  base::as.factor()

plot_violin(df)

plot_violin(df, variables = c("qsec", "wt", "hp"))

plot_violin(df,
            variables = c("qsec", "wt", "hp"),
            display.facets = FALSE)

plot_violin(df,
            variables = c("qsec", "wt", "hp"),
            across = "cluster_kmeans",
            ncol = 1)

kueckelj/confuns documentation built on June 28, 2024, 9:19 a.m.