Learn R Programming

descsuppRplots (version 1.0)

w.ggbetweenstats: Wrapper for ggstatsplot::ggbetweenstats with optional p-value annotation and date formatting

Description

This function creates a `ggbetweenstats` plot from `ggstatsplot`, and optionally adds a p-value annotation using `geom_signif` for two-group comparisons based on values in `ldat`. If requested, it can also format the y-axis as dates.

Usage

w.ggbetweenstats(
  data,
  parameter,
  groupby = NULL,
  pval.display = FALSE,
  ldat,
  ...
)

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 groupby, 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"`.

...

Additional arguments passed to ggbetweenstats.

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. If wasDate is TRUE, the y-axis labels are formatted as dates.

Examples

Run this code
## ungrouped plot
p <- w.ggbetweenstats(iris, "Sepal.Length")
if (FALSE) print(p)

## grouped plot
p <- w.ggbetweenstats(iris, "Sepal.Length", groupby = "Species",
                      bf.message = FALSE, pairwise.display = "none")
if (FALSE) print(p)

## grouped plot with p value annotation for >2 groups
pval <- anova(lm(Sepal.Length ~ Species, data = iris))['Species', 'Pr(>F)']
pval <- descutils::prettyPvalues(pval, digits = 3, orgbold = FALSE, lhs = "p")
ldat <- data.frame(`p value` = pval, check.names = FALSE)
p <- w.ggbetweenstats(iris, "Sepal.Length", groupby = "Species", pval.display = TRUE, ldat = ldat,
                      bf.message = FALSE, pairwise.display = "none")
if (FALSE) print(p)

## setup simple data with only two groups
iris2 <- iris[iris$Species %in% c("setosa", "virginica"),]

## grouped plot with p value annotation for two groups
pval <- t.test(Sepal.Length ~ Species, data = iris2)$p.value
pval <- descutils::prettyPvalues(pval, digits = 3, orgbold = FALSE, lhs = "p")
ldat <- data.frame(`p value` = pval, check.names = FALSE)
p <- w.ggbetweenstats(iris2, "Sepal.Length", groupby = "Species", pval.display = TRUE, ldat = ldat,
                      bf.message = FALSE, pairwise.display = "none")
if (FALSE) print(p)

Run the code above in your browser using DataLab