Skip to contents

ggmid() is an S3 generic function for creating various visualizations from MID-related objects using ggplot2. For "mid" objects (i.e., fitted MID models), it visualizes one or more component functions specified by the terms argument.

Usage

ggmid(object, ...)

# S3 method for class 'mid'
ggmid(
  object,
  terms = term.labels(object, order = 1L),
  type = c("effect", "data", "compound"),
  theme = NULL,
  intercept = FALSE,
  main.effects = FALSE,
  data = NULL,
  limits = c(NA, NA),
  jitter = NULL,
  resolution = c(100L, 100L),
  lumped = TRUE,
  ...
)

# S3 method for class 'mid'
autoplot(object, ...)

Arguments

object

a "mid" object to be visualized.

...

optional parameters passed to the main plotting layer.

terms

a character vector or a formula specifying the component functions to be plotted. If a formula is provided (e.g., ~ x + y + x:y), it is automatically parsed to extract the relevant terms.

type

the plotting style. One of "effect", "data" or "compound".

theme

a character string or object defining the color theme. See color.theme for details.

intercept

logical. If TRUE, the intercept is added to the MID values.

main.effects

logical. If TRUE, main effects are included in the interaction plot.

data

a data frame to be plotted with the corresponding MID values. If not provided, data is automatically extracted based on the function call.

limits

a numeric vector of length two specifying the limits of the plotting scale. NA values are replaced by the minimum and/or maximum MID values.

jitter

a numeric value specifying the amount of jitter for the data points.

resolution

an integer or vector of two integers specifying the resolution of the raster plot for interactions.

lumped

logical. If TRUE, uses the lumped factor levels; if FALSE, uses the original levels from the data. Always FALSE when main.effects = TRUE.

Value

ggmid.mid() returns a "ggplot" object if a single term is specified, or a list of "ggplot" objects if multiple terms are specified.

Details

For "mid" objects, ggmid() creates a "ggplot" object that visualizes a component function of the fitted MID model.

The type argument controls the visualization style. The default, type = "effect", plots the component function itself. In this style, the plotting method is automatically selected based on the effect's type: a line plot for quantitative main effects; a bar plot for qualitative main effects; and a raster plot for interactions. The type = "data" option creates a scatter plot of data, colored by the values of the component function. The type = "compound" option combines both approaches, plotting the component function alongside the data points.

Examples

data(diamonds, package = "ggplot2")
set.seed(42)
idx <- sample(nrow(diamonds), 1e4)
mid <- interpret(price ~ (carat + cut + color + clarity)^2, diamonds[idx, ])
#> 'model' not passed: response variable in 'data' is used

# Plot a quantitative main effect
ggmid(mid, "carat")


# Plot a qualitative main effect
ggmid(mid, "clarity")


# Plot an interaction effect with data points and a raster layer
ggmid(mid, "carat:clarity", type = "compound", data = diamonds[idx, ])


# Use a different color theme
ggmid(mid, "clarity:color", theme = "RdBu")