Inference layers

The inference layers of infereportR draw statistics but do not compute them: they run named infercalcR procedures and label the results with the reporting functions of the package. Every estimate, interval, test statistic, p-value and adjustment comes from infercalcR::run_inference() and infercalcR::adjust_inference(), and every number in a label is a display string from report_table(). The layers choose positions, draw the geometry and keep the complete result objects with the figure, so that they can be inspected and reported.

library(ggplot2)
library(infereportR)

Five layers

All five layers run per panel when the plot is built. Rows with non-finite x or y are dropped first, as ggplot2 does for drawing. Like the theme, every layer takes mode = "light" or "dark" and transparent; pass the same values as to theme_figure() so that the ink matches the background.

Means with confidence intervals

stat_means() draws the mean and its confidence interval for every x position. The numbers are the estimate and interval of the one-sample t procedure (t_mean_one_sample), used as an interval estimate, not as a test.

base <- ggplot(PlantGrowth, aes(group, weight)) +
  geom_point(shape = 1, position = position_jitter(width = 0.08, height = 0, seed = 1)) +
  theme_figure() +
  labs(x = "Group", y = "Dried weight (g)")

base + stat_means(conf.level = 0.95)

Dried plant weight for three groups: open circles for the plants and a point with a vertical line for each group mean and its 95% confidence interval.

Pairwise comparisons

stat_compare() compares x groups pairwise with a named two-sample procedure, adjusts the family of comparisons as declared and draws one bracket per pair. For the pair c(A, B) the contrast is B minus A. With reference, every other level is compared with that level; with comparisons, exactly the listed pairs; with neither, all pairs in level order.

base +
  stat_means() +
  stat_compare(method = "t_means_two_independent_welch", reference = "ctrl",
               adjust = "holm")

The plant weights with two brackets above the groups, comparing each treatment with the control; each bracket is labeled with a Holm-adjusted p-value.

The label is the p-value by default. label = "estimate_ci" shows the difference with its interval, "estimate_ci_p" both, and "stars" replaces p-values below 0.05 with stars. A p-value of 0.05 or more keeps its display string; it is never shown as “ns”.

base +
  stat_compare(method = "t_means_two_independent_welch",
               comparisons = list(c("trt1", "trt2")), adjust = "none",
               label = "estimate_ci_p")

The same comparison drawn for one explicit pair, labeled with the mean difference, its confidence interval and the p-value.

The supported methods are "t_means_two_independent_welch", "t_means_two_independent_student", "rank_sum_mann_whitney" and "brunner_munzel".

The brackets are stacked above the data in fractions of the panel’s y range: the first sits first = 0.10 above the highest value, each further one step = 0.20 higher, which leaves room for one label line between them. headroom (in steps, 1 by default) reserves room above the top bracket, so the y scale includes its label and nothing is clipped; with headroom = 2 a label at the top of the panel, such as an omnibus test, clears the top bracket.

Omnibus tests

stat_omnibus() runs one omnibus test per panel, with y as the outcome and the x positions as groups, and writes the estimand, test statistic and p-value in the top left corner. The methods are "anova_one_way", "anova_one_way_welch" and "kruskal_wallis". Degrees of freedom are not part of the label, because report_table() has no display string for them; they are in the result.

base +
  stat_means() +
  stat_omnibus(method = "anova_one_way")

The plant weights with group means and a text label in the top left corner giving the F statistic and p-value of a one-way analysis of variance.

Correlations

stat_correlation() runs one correlation per panel and labels the coefficient, its interval and the p-value. The methods are "correlation_pearson", "correlation_spearman" and "correlation_kendall". With fit = TRUE it also draws the fitted mean line of infercalcR’s linear prediction model (prediction_model_linear) with its pointwise confidence band from infercalcR::predict_inference(interval = "mean"), as dotted lines without fill. The fit treats every row as an independent unit, and the line is never fitted with lm() directly. With fit = TRUE the function returns two layers, which + adds together. The label stands top left by default; the association is negative here, so it is moved to the free top right corner.

ggplot(mtcars, aes(wt, mpg)) +
  geom_point(shape = 1) +
  stat_correlation(method = "correlation_pearson", fit = TRUE,
                   label_x = Inf, hjust = 1) +
  theme_figure() +
  labs(x = "Weight (1000 lb)", y = "Fuel economy (mpg)")

Fuel economy against weight for 32 cars, with a solid fitted line, two dotted confidence lines and a label giving Pearson's r, its interval and the p-value.

label = "estimate_ci_p_n" writes the label on two lines, the coefficient with its interval on the first and the p-value with the number of pairs on the second, both formatted by report_table(). infercalcR computes a correlation from 4 pairs on; the fitted line of fit = TRUE needs at least 20 pairs per panel. Spearman and Kendall correlations come without an interval in infercalcR 0.1.0, and the label then leaves the interval out.

The label of such a figure, read back from the layer data, is:

two_line <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(shape = 1) +
  stat_correlation(method = "correlation_pearson", label = "estimate_ci_p_n",
                   label_x = Inf, hjust = 1)
cat(layer_data(two_line, 2L)$label, sep = "\n")
#> r = -0.868 [-0.934, -0.744]
#> p < 0.001, n = 32

The label layers place their text at the panel edges (-Inf and Inf), 1 mm inside the panel. Always draw them together with a data layer, such as the points here, so that ggplot2 has data to train the position scales on.

Paired designs

stat_paired() draws a design with two measurements per unit, such as before and after: x is discrete with exactly two levels (first “before”, second “after”), and the new aesthetic id names the unit. The layer runs one paired procedure per panel, "t_means_paired" or "signed_rank_wilcoxon", on the units with both values, so the contrast is after minus before. It draws one thin line per unit, open points at both ends, the two level means joined by a heavy line and, right of the second level, a label with the change, its interval and the p-value. The means are geometry; every printed number is a report_table() display string. Here are the two drugs of the sleep data of Student (1908), given to the same ten people:

sleep_long <- transform(sleep, drug = factor(group, labels = c("Drug 1", "Drug 2")))

ggplot(sleep_long, aes(drug, extra, id = ID)) +
  stat_paired(method = "t_means_paired", unit = "h") +
  coord_cartesian(clip = "off") +
  theme_figure() +
  theme(plot.margin = margin(2, 21, 2, 2, unit = "mm")) +
  labs(x = NULL, y = "Extra sleep (h)")

Slopegraph of extra sleep under drug 1 and drug 2: one thin line per person, a heavy line joining the two means and a label with the mean change in hours, its confidence interval and the p-value.

The label sits beside the second level and extends beyond the panel, hence clip = "off" and the wider right margin.

Figure builders

Two functions build a complete figure from inference results rather than adding a layer. figure_forest() takes a named list of infercalcR results, one contrast each, and draws a forest plot laid out for a slide slot (slot = "slide_full", 134 x 44 mm, by default): a label, n and the estimate with its interval as text columns, square markers with area proportional to n and a dotted line at the null value. Here the tooth growth difference between orange juice and ascorbic acid, overall and per dose:

welch <- infercalcR::method_spec("t_means_two_independent_welch")
difference <- function(d) {
  infercalcR::run_inference(welch, x = d$len[d$supp == "OJ"], y = d$len[d$supp == "VC"])
}
by_dose <- lapply(split(ToothGrowth, ToothGrowth$dose), difference)
results <- c(list("All doses" = difference(ToothGrowth)),
             setNames(by_dose, paste(names(by_dose), "mg/day")))

figure_forest(results, xlab = "Orange juice minus ascorbic acid (mm)",
              header = c("Dose", "n", "Difference [95% CI]"))

Forest plot with four rows, all doses and the three doses, each with n, a square marker with a horizontal confidence interval and the difference with its interval as text; a dotted vertical line marks no difference.

The first result is the overall row, set in bold above a hairline (overall = 1). figure_correlation_matrix() runs one named correlation procedure for every pair of variables and shows the strength as circle area above the diagonal (filled for positive, open for negative) and the values below it:

figure_correlation_matrix(mtcars, vars = c("mpg", "wt", "hp", "qsec"),
                          method = "correlation_spearman")

Correlation matrix of four mtcars variables: circles above the diagonal, values below, variable names on the diagonal.

Both builders keep their results like the layers do, so inference_results() and figure_table() work on them.

Explicit procedures and declared adjustments

The suite never chooses a procedure from the data. stat_compare(), stat_omnibus(), stat_correlation(), stat_paired() and the two figure builders have no default method, so leaving it out is an error when the layer is created:

res <- try(stat_compare(reference = "ctrl", adjust = "holm"), silent = TRUE)
cat(res)
#> Error : method is missing: choose the procedure explicitly; infereportR never selects a test from the data

With more than one comparison, the multiplicity adjustment must be declared; adjust = "none" is allowed and documents the choice. With explicit comparisons, the layer checks this when it is created:

res <- try(stat_compare(method = "t_means_two_independent_welch",
                        comparisons = list(c("ctrl", "trt1"), c("ctrl", "trt2"))),
           silent = TRUE)
cat(res)
#> Error : adjust is missing: declare the multiplicity adjustment; 'none' is allowed

With reference or all pairs, the number of comparisons is only known when the plot is built. ggplot2 then reports “Problem while computing stat”, with the infereportR condition as the cause:

p <- base + stat_compare(method = "t_means_two_independent_welch")
res <- try(ggplot_build(p), silent = TRUE)
cat(res)
#> Error in stat_compare(method = "t_means_two_independent_welch") : 
#>   Problem while computing stat.
#> ℹ Error occurred in the 2nd layer.
#> Caused by error:
#> ! adjust is missing: declare the multiplicity adjustment; 'none' is allowed

The error conditions have classes such as infereportR_invalid_input, so code can catch them with tryCatch().

Facets: one analysis per panel

Every panel is its own analysis: stat_means() estimates per panel and x level, stat_compare() forms one comparison family per panel, and stat_omnibus() and stat_correlation() run once per panel. Here each supplement has its own Holm family of three dose comparisons:

tooth <- ToothGrowth
tooth$dose <- factor(tooth$dose)
tooth$supp <- factor(tooth$supp, labels = c("Orange juice", "Ascorbic acid"))

facetted <- ggplot(tooth, aes(dose, len)) +
  geom_point(shape = 1, position = position_jitter(width = 0.08, height = 0, seed = 1)) +
  stat_means() +
  stat_compare(method = "t_means_two_independent_welch", adjust = "holm") +
  facet_wrap(~supp) +
  theme_figure() +
  labs(x = "Dose (mg/day)", y = "Tooth length (mm)")
facetted

Tooth length by vitamin C dose in two panels, orange juice and ascorbic acid; each panel shows means with intervals and three brackets with Holm-adjusted p-values.

The results behind the figure

inference_results() builds the plot once and returns every infercalcR result its layers computed, named by layer, method and panel:

results <- inference_results(facetted)
names(results)
#> [1] "infereportR:means:t_mean_one_sample:panel1:0.5"          
#> [2] "infereportR:means:t_mean_one_sample:panel1:1"            
#> [3] "infereportR:means:t_mean_one_sample:panel1:2"            
#> [4] "infereportR:means:t_mean_one_sample:panel2:0.5"          
#> [5] "infereportR:means:t_mean_one_sample:panel2:1"            
#> [6] "infereportR:means:t_mean_one_sample:panel2:2"            
#> [7] "infereportR:compare:t_means_two_independent_welch:panel1"
#> [8] "infereportR:compare:t_means_two_independent_welch:panel2"

These are ordinary infercalcR result objects. infercalcR and the reporting functions read them as they would read any other result, for example as a sentence for the manuscript:

oj <- results[["infereportR:compare:t_means_two_independent_welch:panel1"]]
infercalcR::result_data(oj)[, c("row_id", "estimate", "p.value", "p.adjusted")]
#>   row_id estimate      p.value   p.adjusted
#> 1  1-0.5     9.47 8.784919e-05 1.756984e-04
#> 2  2-0.5    12.83 1.323784e-06 3.971352e-06
#> 3    2-1     3.36 3.919514e-02 3.919514e-02
cat(report_text(oj, p = "adjusted"), sep = "\n\n")
#> t_means_two_independent_welch; mean difference (x minus reference or y); 1 minus 0.5: estimate 9.47; 95% interval [5.52, 13.4] (Welch t; identity; pointwise); statistic 5.05; p < 0.001 (adjusted); recorded adjustment: holm; n = 20; excluded = 0.
#> 
#> t_means_two_independent_welch; mean difference (x minus reference or y); 2 minus 0.5: estimate 12.8; 95% interval [9.32, 16.3] (Welch t; identity; pointwise); statistic 7.82; p < 0.001 (adjusted); recorded adjustment: holm; n = 20; excluded = 0.
#> 
#> t_means_two_independent_welch; mean difference (x minus reference or y); 2 minus 1: estimate 3.36; 95% interval [0.189, 6.53] (Welch t; identity; pointwise); statistic 2.25; p = 0.039 (adjusted); recorded adjustment: holm; n = 20; excluded = 0.

One table: figure_table()

figure_table() passes every result through report_table() and binds the rows, with the layer and panel each one came from. By default (p = "auto") it reports adjusted p-values where a result has an adjustment and raw ones elsewhere.

tab <- figure_table(facetted)
tab[tab$figure_layer == 3, c("figure_panel", "contrast", "estimate_display",
                             "interval_display", "p_display")]
#>    figure_panel    contrast estimate_display interval_display p_display
#> 7             1 1 minus 0.5             9.47     [5.52, 13.4] p < 0.001
#> 8             1 2 minus 0.5             12.8     [9.32, 16.3] p < 0.001
#> 9             1   2 minus 1             3.36    [0.189, 6.53] p = 0.039
#> 10            2 1 minus 0.5             8.79     [6.31, 11.3] p < 0.001
#> 11            2 2 minus 0.5             18.2     [14.4, 21.9] p < 0.001
#> 12            2   2 minus 1             9.37     [5.69, 13.1] p < 0.001

The report_figure() bundle

report_figure() adds the theme, the black-and-white scales, the raw observations, the means and the tests you name, in one call. It adds no test unless its procedure is named:

bundle <- ggplot(PlantGrowth, aes(group, weight)) +
  report_figure(compare = "t_means_two_independent_welch", reference = "ctrl",
                adjust = "holm", omnibus = "anova_one_way") +
  labs(x = "Group", y = "Dried weight (g)")
bundle

Plant weights with means, a one-way analysis of variance label and two Holm-adjusted comparisons with the control.

With points = "jitter" (the default) the means are drawn a little to the right of the raw observations, so the two never overlap. With an omnibus test, report_figure() reserves two steps of headroom above the brackets, so the omnibus label at the top of the panel clears the top bracket label.

Precomputed results with stat_inference()

Results computed elsewhere can be drawn on the same figure with stat_inference(), which takes a finished result and explicit bracket positions. Here infercalcR runs a Brunner-Munzel test of trt2 against trt1:

pg <- PlantGrowth
bm <- infercalcR::run_inference(
  infercalcR::method_spec("brunner_munzel"),
  x = pg$weight[pg$group == "trt2"], y = pg$weight[pg$group == "trt1"],
  result_id = "bm-trt2-trt1")
row_id <- infercalcR::result_data(bm)$row_id
positions <- data.frame(row_id = row_id, group1 = "trt1", group2 = "trt2",
                        y_position = 6.6)

base +
  stat_means() +
  stat_inference(bm, positions, groups = levels(pg$group),
                 text_size = 9 / .pt)

Plant weights with means and one bracket between the two treatment groups, labeled with the p-value of a Brunner-Munzel test computed before plotting.

The precomputed result is not part of inference_results(), which returns only what the inference layers computed. vignette("comparison-families", package = "infereportR") shows the same family drawn both ways and explains when to use which.

Sources

The calculations are those of infercalcR (Heller, https://github.com/CTTIR/infercalcR) and the display strings those of the reporting functions of infereportR. The ggproto objects follow the public ggplot2 extension guide, vignette("extending-ggplot2", package = "ggplot2"). The sleep data are those of Student (1908), The probable error of a mean, Biometrika 6(1), 1-25, as shipped with R. See the README for the full list of sources.

Acknowledgements and sources

This package is part of an inference suite (infercalcR, inferguidR and infereportR) that is deeply inspired by DescrTab2 [1], developed at the Institute of Medical Biometry (IMBI), Heidelberg University, by Jan Meis, Lukas Baumann, Maximilian Pilz and Lukas Sauer. Its test-choice tree, its validation statement and its practice of computing each test with an established implementation are the model for how the suite chooses, computes and reports statistical tests. DescrTab2 was archived on CRAN on 18 October 2025; its source code remains available on GitHub. The methods-consulting pages of the University of Zurich [2], with their decision tree, decision assistant, worked examples and explanations, inspired the guidance in the suite. Power and sample-size planning and the distribution calculator follow G*Power 3.1 [3, 4]. We thank the authors of these works for making them openly available. The package README cites all software the package uses.

  1. Meis J, Baumann L, Pilz M, Sauer L (2022). DescrTab2: Publication Quality Descriptive Statistics Tables. R package version 2.1.16. https://doi.org/10.32614/CRAN.package.DescrTab2. Source code, including the development version 2.1.26: https://github.com/imbi-heidelberg/DescrTab2.
  2. Universität Zürich, Methodenberatung. Datenanalyse mit SPSS: decision tree (Entscheidbaum), decision assistant (Entscheidassistent) and method pages with examples and explanations. https://www.methodenberatung.uzh.ch/de/datenanalyse_spss.html. Accessed 3 October 2026.
  3. Faul F, Erdfelder E, Lang A-G, Buchner A (2007). G*Power 3: A flexible statistical power analysis program for the social, behavioral, and biomedical sciences. Behavior Research Methods, 39(2), 175-191. https://doi.org/10.3758/BF03193146.
  4. Faul F, Erdfelder E, Buchner A, Lang A-G (2009). Statistical power analyses using G*Power 3.1: Tests for correlation and regression analyses. Behavior Research Methods, 41(4), 1149-1160. https://doi.org/10.3758/BRM.41.4.1149.