stat_mixedModel: Add results linear mixed model to plot

View source: R/stat_mixedModel.R

stat_mixedModelR Documentation

Add results linear mixed model to plot

Description

Uses lme4::lmer() and emmeans::emmeans() to calcuate significanes. First a mixed model is build with lmer using y ~ x + (1|group). In a second step, significanes are calcuated using pairs(emmeans(model ~ x)). Only works if the x-axis is a discrete scale.

Usage

stat_mixedModel(
  mapping = NULL,
  data = NULL,
  geom = GeomStat,
  position = "identity",
  na.rm = FALSE,
  show.legend = NA,
  size = 10,
  hide.ns = TRUE,
  tick.length = 0.02,
  format.fun = pvalue,
  vjust = 0,
  step.increase = 0.05,
  colour = "black",
  inherit.aes = TRUE,
  ...
)

Arguments

mapping

Set of aesthetic mappings created by aes() or aes_(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (e.g. ~ head(.x, 10)).

geom

the geometric object to use display the data. Set to GeomStat and usually no need to change.

position

Position adjustment, either as a string, or the result of a call to a position adjustment function.

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.

size

Fontsize for the annotation

hide.ns

Should p-values lower than 0.05 be removed? Default TRUE

tick.length

Length of the ticks in the p-value brackets. Default 0.02

format.fun

A function used to format the p value. Default scales::pvalue

vjust

A numeric vector specifying vertical justification. Passed on to textGrob.

step.increase

Amount of increase between two brackets. Default 0.05

colour

Colour of the bracket and label

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().

...

Further arguments passed on to the layer in params

Value

A ggplot layer

Examples

library(ggplot2)
library(ggsimplestats)

theme_set(ggthemes::theme_few())

df <- data.frame(patient=as.factor(rep(1:5, each=4)),
                 drug=as.factor(rep(1:4, times=5)),
                 response=c(30, 28, 16, 34,
                            14, 18, 10, 22,
                            24, 20, 18, 30,
                            38, 34, 20, 44,
                            26, 28, 14, 30))
ggplot(df, aes(x=drug, y=response, color=patient)) +
  geom_point(position = position_dodge(width = .2)) +
  stat_mixedModel(aes(group = patient))

maximilian-heeg/ggsimplestats documentation built on June 9, 2022, 9:44 p.m.