I am working with a hierarchical model (lmerMod) that has an outcome and two categorical variables, cat1 and cat2. I want to make an interaction plot using ggplot2:: that visualizes the model coefficients and takes into account two specific complexities:
Pairwise comparisons: I need to compare the coefficients for each level of cat2 (Control, T1, T2, T3) against each other within each level of cat1 (1 and 2).
Statistical significance: I want to highlight the statistical significance of these comparisons using asterisks or some similar notation. Examples are ggsignif or ggpval packages, or ggpubr::stat_compare_means() function.
Requirements:
- The geometry should be
pointrange. cat1should be on the x-axis.cat2should define the color aesthetic.- I want to use the
lmerTestpackage for model fitting (to get correct p-values).
Challenges:
- Using
ggeffects::hypothesis_test()takes a very long time for large datasets (N=46,000, G1=4,000, G2=40). - I struggle with
ggplotsolutions for having brackets (significance stars can be places withgeom_text()andifelse()I guess)
Reproducible Example:
Here's a simple R example using a toy dataset:
# Load required packages
library(lmerTest)
library(ggplot2)
# Create toy data
set.seed(123)
N <- 1000 # Increased sample size
G1 <- 20 # Increased number of levels for G1
G2 <- 10 # Increased number of levels for G2
df <- data.frame(
outcome = rnorm(N),
cat1 = factor(rep(1:2, each = N / 2)),
cat2 = factor(rep(c("Control", "T1", "T2", "T3"), each = N / 4)),
G1 = factor(rep(1:G1, each = N / G1)),
G2 = factor(rep(1:G2, each = N / G2))
)
# Fit the hierarchical model
fit <- lmerTest::lmer(outcome ~ cat1 * cat2 + (1|G1) + (1|G2), data = df)
summary(fit)
# Attempt to make the plot
interactions::cat_plot(model = fit, pred = cat1, modx = cat2,
interval.geom = "linerange", colors = "Set1")
# ... (I want significance stars and brackets connecting the coefficients: this is where I am stuck)

