Learn R Programming

descsuppRplots (version 1.0)

w.ggbarstats: Wrapper for ggstatsplot::ggbarstats with optional p-value annotation

Description

This function creates a `ggbarstats` plot from `ggstatsplot`, and optionally adds a p-value annotation using `geom_signif` for two-group comparisons based on values in `ldat`.

Usage

w.ggbarstats(
  data,
  parameter,
  groupby = NULL,
  pval.display = FALSE,
  ldat,
  tip_length = 4e-04,
  pval_y_position = 1.05,
  ...
)

Value

A ggplot object.

Arguments

data

A data frame with the data to be plotted.

parameter

Character. Name of the column in data to the values to be plotted.

groupby

Character (default: NULL). Name of the grouping (categorical) variable in data.

pval.display

Logical. If TRUE and there are exactly two groups in x, a p-value annotation is added.

ldat

A data frame containing the p-value(s) to annotate on the plot. Expected columns: `"p value"` and/or `"adjusted p value"`.

tip_length

Numeric. Length of the annotation tips for geom_signif (default: 0.0004).

pval_y_position

Numeric. Vertical position for the p-value annotation (default: 1.05).

...

Additional arguments passed to ggbarstats.

Author

Fabian Kück

Details

If groupby is not NULL and there are exactly two levels in the grouping variable, and pval.display = TRUE, a p-value from ldat is added between the two groups using geom_signif. If "adjusted p value" is present in ldat, it is used; otherwise, "p value" is used.

Examples

Run this code
## ungrouped plot
p <- w.ggbarstats(mtcars, "gear",
                  bf.message = FALSE,
                  proportion.test = FALSE)
if (FALSE) print(p)

## grouped plot
p <- w.ggbarstats(mtcars, "gear", groupby = "cyl",
                  bf.message = FALSE,
                  proportion.test = FALSE)
if (FALSE) print(p)

## grouped plot with p value annotation for >2 groups
pval <- fisher.test(mtcars$gear, mtcars$cyl)$p.value
pval <- descutils::prettyPvalues(pval, digits = 3, orgbold = FALSE, lhs = "p")
ldat <- data.frame(`p value` = pval, check.names = FALSE)
p <- w.ggbarstats(mtcars, "gear", groupby = "cyl", pval.display = TRUE, ldat = ldat,
                  bf.message = FALSE,
                  proportion.test = FALSE)
if (FALSE) print(p)

## grouped plot with p value annotation for two groups
pval <- fisher.test(mtcars$gear, mtcars$vs)$p.value
pval <- descutils::prettyPvalues(pval, digits = 3, orgbold = FALSE, lhs = "p")
ldat <- data.frame(`p value` = pval, check.names = FALSE)
p <- w.ggbarstats(mtcars, "gear", groupby = "vs", pval.display = TRUE, ldat = ldat,
                  bf.message = FALSE,
                  proportion.test = FALSE)
if (FALSE) print(p)

Run the code above in your browser using DataLab