Violin Plots to Compare Within Group (Repeated Measures)
Source:R/jjwithinstats.b.R
jjwithinstatsClass.RdCreates violin plots for within-subjects (repeated measures) analysis using ggstatsplot::ggwithinstats. Compares 2-4 measurements from the same subjects with statistical testing and pairwise comparisons. Ideal for biomarker tracking, treatment response monitoring, and longitudinal clinical studies.
Details
Data Requirements:
Wide format required (one row per subject)
Each column represents a different time point or condition
Complete data required for paired analysis (listwise deletion)
Minimum 3 subjects with complete data across all measurements
Statistical Tests:
Parametric: Repeated measures ANOVA (assumes normality)
Nonparametric: Friedman test (no distribution assumptions)
Robust: Uses trimmed means (resistant to outliers)
Bayesian: Provides evidence strength via Bayes Factors
Clinical Presets:
Biomarker: Optimized for laboratory biomarker tracking (nonparametric)
Treatment: Optimized for treatment response monitoring (parametric with pairwise)
Laboratory: Optimized for clinical lab values (robust)
Performance Optimization
The function implements sophisticated caching:
Data preparation cached based on variable selection and data content
Options cached separately to minimize reprocessing
Plot state management prevents unnecessary regeneration
Checkpoint calls before expensive operations for responsiveness
Clinical Validation
The function performs comprehensive data quality checks:
Validates paired design requirements (complete cases)
Detects small sample sizes (< 10 subjects)
Identifies potential outliers (> 10% outliers)
Warns about skewed data for parametric tests
Alerts to high missing data rates (> 50%)
References
Patil, I. (2021). Visualizations with statistical details: The 'ggstatsplot' approach. Journal of Open Source Software, 6(61), 3167. doi:10.21105/joss.03167
See also
ggwithinstats for the underlying plotting function
Super classes
jmvcore::Analysis -> ClinicoPath::jjwithinstatsBase -> jjwithinstatsClass
Methods
Inherited methods
jmvcore::Analysis$.createImage()jmvcore::Analysis$.createImages()jmvcore::Analysis$.createPlotObject()jmvcore::Analysis$.load()jmvcore::Analysis$.render()jmvcore::Analysis$.save()jmvcore::Analysis$.savePart()jmvcore::Analysis$.setCheckpoint()jmvcore::Analysis$.setParent()jmvcore::Analysis$.setReadDatasetHeaderSource()jmvcore::Analysis$.setReadDatasetSource()jmvcore::Analysis$.setResourcesPathSource()jmvcore::Analysis$.setStatePathSource()jmvcore::Analysis$addAddon()jmvcore::Analysis$asProtoBuf()jmvcore::Analysis$check()jmvcore::Analysis$init()jmvcore::Analysis$optionsChangedHandler()jmvcore::Analysis$postInit()jmvcore::Analysis$print()jmvcore::Analysis$readDataset()jmvcore::Analysis$run()jmvcore::Analysis$serialize()jmvcore::Analysis$setError()jmvcore::Analysis$setStatus()jmvcore::Analysis$translate()ClinicoPath::jjwithinstatsBase$initialize()
Examples
if (FALSE) { # \dontrun{
# Basic within-subjects analysis
data(iris)
iris_wide <- data.frame(
Subject = 1:50,
Baseline = iris$Sepal.Length[1:50],
Month3 = iris$Sepal.Width[1:50] * 2.5,
Month6 = iris$Petal.Length[1:50] * 1.8
)
jjwithinstats(
data = iris_wide,
dep1 = "Baseline",
dep2 = "Month3",
dep3 = "Month6",
typestatistics = "parametric",
pairwisecomparisons = TRUE,
centralityplotting = TRUE,
pointpath = TRUE
)
# Clinical biomarker tracking (nonparametric)
jjwithinstats(
data = clinical_data,
dep1 = "CRP_baseline",
dep2 = "CRP_week4",
dep3 = "CRP_week12",
clinicalpreset = "biomarker",
typestatistics = "nonparametric",
pairwisecomparisons = TRUE,
mytitle = "C-Reactive Protein Levels During Treatment"
)
# Robust analysis with publication plot
jjwithinstats(
data = lab_data,
dep1 = "ALT_pre",
dep2 = "ALT_post",
typestatistics = "robust",
centralityplotting = TRUE,
centralitytype = "robust",
addGGPubrPlot = TRUE,
ggpubrPlotType = "paired",
ggpubrAddStats = TRUE
)
} # }