Fitted-Model-Based Annotations :: Cheat Sheet

‘ggpmisc’ 1.0.0

Pedro J. Aphalo

2026-07-05

Basics

ggpmisc follows the grammar of graphics implemented in ggplot2, based on the idea that many different data visualizations can be built by combining the same components: a data set, a coordinate system, and geoms—visual marks that represent data or summaries derived from data. These elements are complemented by stats that compute data summaries to be passed to geoms and scales that describe the mapping of data into graphical elements.

There are multiple variations of each element of the grammar, providing a vocabulary. Thus, the grammar allows us to ‘speak/write’ a graph from composable elements, instead of being limited to a predefined set of charts. ‘ggpmisc’ adds new stats and scales, expanding the vocabulary while remaining consistent with the grammar. ‘ggpmisc’ relies on geoms from packages ‘ggpmisc’ and ‘ggplot2’ for its defaults, while also compatible with geoms from other R packages including ‘ggtext’, ‘marquee’, ‘xdvir’, ‘ggrepel’ and ‘gganimate’.

If you are not already familiar with the grammar of graphics and ggplot2 you should visit the ggplot2 Cheat Sheet first, and afterwards come back to this Cheat Sheet.

Differently to ggplot2, no matching geometries with the new stats as their default are provided. The plot layers described here are always added with a stat, and when necessary, their default geom argument overridden.

library(ggpmisc)

Most of the layer functions in ggpmisc aim at making it easier to add to plots information derived from model fitting, tests of significance or statistical summaries. All the stats from ‘ggpmisc’ do computations by data group except for stat_fit_tb() and stat_multcomp() that do computations by plot panel.

The statistics that return predicted values for regressions return x and y where one of the variables is a sequence of numbers for the explanatory variables and the other contains the predictions based on them; depending on the orientation or formula, ymin and ymax, or xmin and xmax, give the lower and upper confidence limits for the fitted line or curve.

The statistics returning fitted or residual values return these values as variables y.fitted or x.fitted, y.resid or x.resid, weights and posterior.weights. Variables x and y contain the observed values. When present, weights are the prior weights, and posterior.weights are posterior weights, those actually used by the model fit function, possibly computed by it.

The statistics that return text labels for annotating plots, return in x and y as coordinates of the text or label annotaions the values passed as arguments to parameters label.x and label.y, or values computed based on them. The character strings are returned as variables with names ending in .label. These variables can be used in mappings created with aes(), use_label() and f_use_labels(). The difference is that use_label() and f_use_label() accept short names for the labels, recognize them as computed by a stat and combine them into a single character string. For example, use_label("eq", "R2", "n", sep = ", ") is equivalent to aes(label = paste(after_stat("eq.label", "rr.label", "n.label), sep = ", "), saving some typing. Numeric values for the parameter estimates are also returned, making possible 1) to assemble labels in user code within a call to aes() and 2) to map outcomes to additional aesthetics such as fontface or colour based on a threshold.

Correlation

Fitted models

The statistics for fitted models come in matched pairs, one that adds a plot layer with one or more curves and confidence band(s), and one that annotates the plot with the fitted model equation and/or other parameter estimates. These depend on the type of fitted model and include \(R^2\), \(F\), \(P\), \(AIC\), \(BIC\), \(n\), and in many cases also the fitted-model equation. The curve plotting stats fulfil a role similar to ggplot2::stat_smooth() while the statistics for textual annotations have no equivalent in ‘ggplot2’.

ANOVA or summary tables

Multiple comparisons

Aesthetic mappings

Peaks and valleys

Formatted character labels are returned both for x and y coordinates. Both numeric, time and dates can be mapped to x (or y with orientation = "y").

Volcano and quadrant plots

These plots are frequently used with gene expression data, and each of the many genes labelled based on the ternary outcome from a statistical test. Data are usually, in addition transformed. ‘ggpmisc’ provides several variations on continuous, colour, fill and shape scales, with defaults set as needed. Scales support log fold-change (logFC) on multiple logarithm bases both for input and for output, false discovery ratio (FDR), P-value (Pvalue) and binary or ternary test outcomes (outcome).

Utility functions

Most of the functions used to generate formatted labels in the statistics and scales listed above are also exported. However, several of them now have equivalents in recent versions of R package ‘scales’.


Learn more at docs.r4photobiology.info/ggpmisc/.