Analyzing Interaction Effects and Multi-Factor Grouping
Author
Abdullah Al Shamim
Published
February 27, 2026
What is a Two-Way ANOVA?
A Two-Way ANOVA examines the effect of two independent categorical variables on a continuous dependent variable. Crucially, it also tests for interaction effects—determining if the effect of one factor depends on the level of the other.
1. Environment Setup & Typography
We use the showtext package to bring professional Google Fonts into our R visualizations.
Code
library(tidyverse)library(ggthemes)library(multcompView)library(stats)library(showtext)# Enable Poppins font for a modern lookfont_add_google("Poppins", "poppins")showtext_auto()# Set global theme with custom typographytheme_set(theme_test(base_size =15) +theme(text =element_text(family ="poppins")))
2. Data Preparation
We use the ToothGrowth dataset. In this analysis, we examine how Supplement Type (supp) and Dose (dose) together affect tooth length.
If the interaction or main effects are significant, we perform a Tukey HSD test to compare all possible group combinations. We then use Compact Letter Display (CLD) to simplify the visual communication of these differences.
Code
# Perform Multiple Mean Comparisontukey_result <-TukeyHSD(anova_result)# Extract significance letteringgroup_lettering <-multcompLetters4(anova_result, tukey_result)# Convert to dataframe for merginggroup_lettering_df <-data.frame(Letters = group_lettering$`supp:dose`$Letters) %>%rownames_to_column("group")
5. Descriptive Statistics & Merging
We calculate the mean and standard deviation for each factor combination to prepare our bar plot.