# The intervals method
# Script to get the intervals matrix
# Script powered by Jordi Marce-Nogue and Thomas Puschel
#
# Cite: Marce-Nogue, J., De Esteban-Trivigno, S., Puschel, T.A., Fortuny, J., 2017. The intervals method: a new approach to analyse finite element outputs using multivariate statistics. PeerJ 5, e3793. 
# https://doi.org/10.7717/peerj.3793
devtools::install_github("kassambara/factoextra") 
## Downloading GitHub repo kassambara/factoextra@master
## 
##   
   checking for file ‘/private/var/folders/x8/z94xwjjn42v9htnwlzdpq6d40000gn/T/Rtmpc1rOnT/remotes18501414b3c35/kassambara-factoextra-1689fc7/DESCRIPTION’ ...
  
✓  checking for file ‘/private/var/folders/x8/z94xwjjn42v9htnwlzdpq6d40000gn/T/Rtmpc1rOnT/remotes18501414b3c35/kassambara-factoextra-1689fc7/DESCRIPTION’ (469ms)
## 
  
─  preparing ‘factoextra’: (715ms)
## 
  
   checking DESCRIPTION meta-information ...
  
✓  checking DESCRIPTION meta-information
## 
  
─  checking for LF line-endings in source and make files and shell scripts
## 
  
─  checking for empty or unneeded directories
## 
  
─  looking to see if a ‘data/datalist’ file should be added
## 
  
─  building ‘factoextra_1.0.7.999.tar.gz’
## 
  
   
## 
devtools::install_github("kassambara/ggpubr")
## Downloading GitHub repo kassambara/ggpubr@master
## rstatix (NA -> 1aef81d2d...) [GitHub]
## broom   (NA -> 0.5.6       ) [CRAN]
## Downloading GitHub repo kassambara/rstatix@master
## broom (NA -> 0.5.6) [CRAN]
## Installing 1 packages: broom
## 
## The downloaded binary packages are in
##  /var/folders/x8/z94xwjjn42v9htnwlzdpq6d40000gn/T//Rtmpc1rOnT/downloaded_packages
##   
   checking for file ‘/private/var/folders/x8/z94xwjjn42v9htnwlzdpq6d40000gn/T/Rtmpc1rOnT/remotes185013d8a5f50/kassambara-rstatix-1aef81d/DESCRIPTION’ ...
  
✓  checking for file ‘/private/var/folders/x8/z94xwjjn42v9htnwlzdpq6d40000gn/T/Rtmpc1rOnT/remotes185013d8a5f50/kassambara-rstatix-1aef81d/DESCRIPTION’ (439ms)
## 
  
─  preparing ‘rstatix’: (950ms)
## 
  
   checking DESCRIPTION meta-information ...
  
✓  checking DESCRIPTION meta-information
## 
  
─  checking for LF line-endings in source and make files and shell scripts (335ms)
## 
  
─  checking for empty or unneeded directories
## 
  
─  building ‘rstatix_0.4.0.999.tar.gz’
## 
  
   
## 
## Installing 1 packages: broom
## 
## The downloaded binary packages are in
##  /var/folders/x8/z94xwjjn42v9htnwlzdpq6d40000gn/T//Rtmpc1rOnT/downloaded_packages
## Skipping install of 'rstatix' from a github remote, the SHA1 (1aef81d2) has not changed since last install.
##   Use `force = TRUE` to force installation
##   
   checking for file ‘/private/var/folders/x8/z94xwjjn42v9htnwlzdpq6d40000gn/T/Rtmpc1rOnT/remotes18501428d8c81/kassambara-ggpubr-0df7aee/DESCRIPTION’ ...
  
✓  checking for file ‘/private/var/folders/x8/z94xwjjn42v9htnwlzdpq6d40000gn/T/Rtmpc1rOnT/remotes18501428d8c81/kassambara-ggpubr-0df7aee/DESCRIPTION’ (538ms)
## 
  
─  preparing ‘ggpubr’: (1.7s)
## 
  
   checking DESCRIPTION meta-information ...
  
✓  checking DESCRIPTION meta-information
## 
  
─  checking for LF line-endings in source and make files and shell scripts (401ms)
## 
  
─  checking for empty or unneeded directories
## 
  
─  looking to see if a ‘data/datalist’ file should be added
## 
  
─  building ‘ggpubr_0.2.5.999.tar.gz’
## 
  
   
## 
library(FactoMineR)
library(factoextra)
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
stress.distrib = read.csv("matrix-of-intervals.csv", row.names=1, header = TRUE, sep = ",") 

# 1) Multivariate analysis PCA 

col.number = ncol(stress.distrib)
PCA.stress <- PCA(stress.distrib[,1:col.number],  graph = FALSE)


# 2) Define the parameters and create the biplot

# colors by group
group.colors = row.names(stress.distrib)

#colors by variable
interval.number = nrow(PCA.stress$var$coord)
interval.vector = seq(from = 1, to = interval.number, by=1 )
interval.colors = interval.vector
interval.palette = c("blue","cyan","green","chartreuse","yellow","gold","orange","red")

# Biplot: variables coloured by contribution to PCs

library(ggpubr)

fviz_pca_biplot(PCA.stress, 
                mean.point=F,                     # 
                axes.linetype = "solid",
    
                # Fill individuals by groups
                geom.ind=c("point", "text"),
                pointshape = 21, 
                pointsize = 5,            
                col.ind= "black",                 # 
                fill.ind = group.colors,          # 
                alpha.ind = 1,                    # 
                
                # Color variable by intervals
                geom.var = "arrow",
                col.var = interval.colors,
                arrowsize = 0.5,
                
                repel = TRUE) +                   # Avoid label overplotting
  
  
  #fill_palette(group.palette) +
  gradient_color(interval.palette)+
  labs(x = "PC1", y = "PC2")+
  
  
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
  )