Skip to contents

The correlation structure between samples in complex study designs can be decomposed into the contribution of multiple dimensions of variation. variancePartition provides a statistical and visualization framework to interpret sources of variation. Here I describe a visualization of the correlation structure between samples for a single gene.

In the example dataset described in the main vignette, samples are correlated because they can come from the same individual or the same tissue. The function plotCorrStructure() shows the correlation structure caused by each variable as well and the joint correlation structure. Figure 1 shows the correlation between samples from the same individual where (a) shows the samples sorted based on clustering of the correlation matrix and (b) shows the original order. Figure 1 c) and d) shows the same type of plot except demonstrating the effect of tissue. The total correlation structure from summing individual and tissue correlation matricies is shown in Figure 2. The code to generate these plots is shown below.

Plot variance structure

# Fit linear mixed model and examine correlation stucture
# for one gene
data(varPartData)

form <- ~ Age + (1 | Individual) + (1 | Tissue)

fitList <- fitVarPartModel(geneExpr[1:2, ], form, info)

# focus on one gene
fit <- fitList[[1]]

By Individual

Reorder samples

# Figure 1a
# correlation structure based on similarity within Individual
# reorder samples based on clustering
plotCorrStructure(fit, "Individual")

Original order of samples

# Figure 1b
# use original order of samples
plotCorrStructure(fit, "Individual", reorder = FALSE)

By Tissue

Reorder samples

# Figure 1c
# correlation structure based on similarity within Tissue
# reorder samples based on clustering
plotCorrStructure(fit, "Tissue")

Original order of samples

# Figure 1d
# use original order of samples
plotCorrStructure(fit, "Tissue", reorder = FALSE)

By Individual and Tissue

Reorder samples

# Figure 2a
# correlation structure based on similarity within
# Individual *and* Tissue, reorder samples based on clustering
plotCorrStructure(fit)

Original order of samples

# Figure 2b
# use original order of samples
plotCorrStructure(fit, reorder = FALSE)

Session Info

## R version 4.4.1 (2024-06-14)
## Platform: aarch64-apple-darwin23.5.0
## Running under: macOS Sonoma 14.6.1
## 
## Matrix products: default
## BLAS:   /Users/gabrielhoffman/prog/R-4.4.1/lib/libRblas.dylib 
## LAPACK: /opt/homebrew/Cellar/r/4.4.0_1/lib/R/lib/libRlapack.dylib;  LAPACK version 3.12.0
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## time zone: America/New_York
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] variancePartition_1.36.1 BiocParallel_1.40.0      limma_3.62.1            
## [4] ggplot2_3.5.1            knitr_1.48              
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.2.1    dplyr_1.1.4         bitops_1.0-9       
##  [4] fastmap_1.2.0       digest_0.6.37       lifecycle_1.0.4    
##  [7] statmod_1.5.0       magrittr_2.0.3      compiler_4.4.1     
## [10] rlang_1.1.4         sass_0.4.9          tools_4.4.1        
## [13] utf8_1.2.4          yaml_2.3.10         htmlwidgets_1.6.4  
## [16] plyr_1.8.9          KernSmooth_2.23-24  withr_3.0.2        
## [19] purrr_1.0.2         numDeriv_2016.8-1.1 BiocGenerics_0.52.0
## [22] desc_1.4.3          grid_4.4.1          aod_1.3.3          
## [25] fansi_1.0.6         caTools_1.18.3      colorspace_2.1-1   
## [28] scales_1.3.0        gtools_3.9.5        iterators_1.0.14   
## [31] MASS_7.3-61         cli_3.6.3           mvtnorm_1.3-2      
## [34] rmarkdown_2.29      ragg_1.3.3          generics_0.1.3     
## [37] reshape2_1.4.4      minqa_1.2.8         cachem_1.1.0       
## [40] stringr_1.5.1       splines_4.4.1       parallel_4.4.1     
## [43] matrixStats_1.4.1   vctrs_0.6.5         boot_1.3-31        
## [46] Matrix_1.7-1        jsonlite_1.8.9      pbkrtest_0.5.3     
## [49] systemfonts_1.1.0   jquerylib_0.1.4     tidyr_1.3.1        
## [52] glue_1.8.0          pkgdown_2.1.1       nloptr_2.1.1       
## [55] codetools_0.2-20    stringi_1.8.4       gtable_0.3.6       
## [58] EnvStats_3.0.0      lme4_1.1-35.5       lmerTest_3.1-3     
## [61] munsell_0.5.1       tibble_3.2.1        remaCor_0.0.18     
## [64] pillar_1.9.0        htmltools_0.5.8.1   gplots_3.2.0       
## [67] R6_2.5.1            textshaping_0.4.0   Rdpack_2.6.1       
## [70] evaluate_1.0.1      lattice_0.22-6      Biobase_2.66.0     
## [73] highr_0.11          rbibutils_2.3       backports_1.5.0    
## [76] RhpcBLASctl_0.23-42 broom_1.0.7         fANCOVA_0.6-1      
## [79] corpcor_1.6.10      bslib_0.8.0         Rcpp_1.0.13-1      
## [82] nlme_3.1-166        xfun_0.49           fs_1.6.5           
## [85] pkgconfig_2.0.3