From b8e8c007a6ccc22b9c299a596c88c99d81071d5b Mon Sep 17 00:00:00 2001
From: Francesco Sabatini <francesco.sabatini@idiv.de>
Date: Wed, 11 Nov 2020 21:59:17 +0100
Subject: [PATCH] Panel 2 abg vs inter X Corr

---
 04_Additional_Figs.R | 147 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 133 insertions(+), 14 deletions(-)

diff --git a/04_Additional_Figs.R b/04_Additional_Figs.R
index f20b409..5e24309 100644
--- a/04_Additional_Figs.R
+++ b/04_Additional_Figs.R
@@ -79,13 +79,24 @@ extract.results <- function(z){
     return(cor.res)
   }
   
+  get.minmax <- function(z){
+    tag <- "OTU richness in each community \\(at final step 5000\\):$"
+    minmax.lines <- z[grep(pattern=tag, x=z)+3]
+    minmax.res <- NULL
+    if(length(minmax.lines)>0){
+      minmax.res <- unlist(regmatches(minmax.lines, gregexpr("[[:digit:]]+", minmax.lines)))
+      names(minmax.res) <- paste("richness.W", c("min", "max"), sep=".")}
+    return(minmax.res)
+  }
+  
   
   bind.res <- function(zz){
     return(c(get.abg(zz, "W"),
            get.abg(zz, "Y"),
            get.abg(zz, "X"), 
            get.cor(zz, "Y"), 
-           get.cor(zz, "W")))
+           get.cor(zz, "W"), 
+           get.minmax(zz)))
   }
   
   tmp <- lapply(z, bind.res)
@@ -95,6 +106,22 @@ extract.results <- function(z){
     #mutate(dataset=rep(names(tmp), lapply(tmp, length))))
 }
 
+## Data parses 
+do.the.parse <- function(toparse) {
+  x2 <- split.by.dataset(toparse)
+  x3 <- lapply(x2, split.trait.env.combos)
+  lapply(x3, extract.results) %>% 
+    bind_rows %>% 
+    dplyr::relocate(dataset) %>% 
+    pivot_longer(-dataset, names_to="metric", values_to="value") %>%
+    filter(!is.na(value)) %>% 
+    separate(dataset, into=c("dataset", "trait", "env"), sep=" - ") %>% 
+    dplyr::relocate(metric, .after=dataset) %>% 
+    separate(metric, into=c("metric", "matrix", "with", "PCOA"), sep="\\.") %>% 
+    mutate(value=as.numeric(value))
+}
+
+
 ## Function to create Figure SXXV
 create.panel <- function(x){
   gg.betaW <- ggplot(data=x %>% 
@@ -144,7 +171,7 @@ create.panel <- function(x){
 
 
 
-### Parse output file ####
+### Figure SXXV - comparison beta vs cor ####
 mypath <- "_data/Experiment_30Oct2020_FactorInteraction&TraitCorr_XY_SampleSize_Main=040_Inter=00_Corr=00_v21169"
 myfiles <- list.files(path=mypath, pattern = "FinalSimulatedData.txt", recursive = T, full.names = T)
 myfiles <- myfiles[grepl("_new", x=myfiles)]
@@ -152,18 +179,7 @@ myfiles <- myfiles[grepl("_new", x=myfiles)]
 for(i in 1:length(myfiles)){
   toparse <- myfiles[i]
   sampleN <- regmatches(toparse, gregexpr("N=[[:digit:]]+", toparse))[[1]]
-  x2 <- split.by.dataset(toparse)
-  x3 <- lapply(x2, split.trait.env.combos)
-
-  div.summary <- lapply(x3, extract.results) %>% 
-    bind_rows %>% 
-    dplyr::relocate(dataset) %>% 
-    pivot_longer(-dataset, names_to="metric", values_to="value") %>%
-      filter(!is.na(value)) %>% 
-    separate(dataset, into=c("dataset", "trait", "env"), sep=" - ") %>% 
-    dplyr::relocate(metric, .after=dataset) %>% 
-    separate(metric, into=c("metric", "matrix", "with", "PCOA"), sep="\\.") %>% 
-    mutate(value=as.numeric(value))
+  div.summary <- do.the.parse(toparse)
 
   gg.out <- create.panel(div.summary)
   ggsave(filename = paste0("_pics/R1/FigSXXZ_Panel_BetaCor_", sampleN, ".png"), 
@@ -171,3 +187,106 @@ for(i in 1:length(myfiles)){
 }
 
 
+#### Figure SXXK - Comparison abg across combinatios Inter X Corr (Main=0.3) ####
+
+create.panel2 <- function(xx, i, tit){
+  require(ggpubr)
+  # alpha, min, mean, max
+  gg.alpha <- ggplot(data=xx %>% 
+                        filter(matrix=="W") %>% 
+                        filter(metric %in% c("richness", "alpha")) %>% 
+                        mutate(with=ifelse(!is.na(with), paste0("OTU Rich (", with, ")"), with)) %>% 
+                        mutate(with=ifelse(is.na(with), "Eq. OTU (mean)", with))) + 
+     geom_density(aes(value, group=with, col=with), show.legend=FALSE)+
+     stat_density(aes(x=value, colour=with),
+                  geom="line",position="identity") + 
+     scale_color_brewer(palette="Dark2", name=NULL) +
+     theme_classic() + 
+     xlab("Alpha diversity (OTU Richness)") + 
+     theme(legend.position = c(0.65, 0.9)#, 
+           #legend.text = element_text(size=7)
+           ) + 
+    xlim(c(-.1,100)) + 
+    ylim(c(0,0.25))
+
+  
+  # beta 
+  gg.beta <- ggplot(data=xx %>% 
+                      filter(matrix=="W") %>% 
+                      filter(metric %in% "beta")) +
+    geom_density(aes(value)) + 
+    theme_classic() + 
+    xlab("Beta diversity") + 
+    ylab(NULL) + 
+    xlim(c(0,4)) + 
+    ylim(c(0,1.15))
+  #beta.proportional 
+  gg.propbeta <- gg.beta %+%
+    (xx %>% 
+       filter(matrix=="W") %>% 
+       filter(metric %in% "propbeta")) + 
+    xlab("Proportional Beta Diversity") + 
+    xlim(c(0,0.7)) + 
+    ylim(c(0,4))
+  #gamma
+  gg.gamma <- gg.beta %+% 
+    (xx %>% 
+       filter(matrix=="W") %>% 
+       filter(metric %in% "gamma")) +
+    xlab("Gamma diversity")  + 
+    xlim(c(0,100)) + 
+    ylim(c(0,0.05))
+  
+  gg.title <- as_ggplot(text_grob(
+    label=tit,
+    just = "centre", rot=90
+  ))
+  
+  if(i!=1){
+    gg.alpha <- gg.alpha + 
+    theme(legend.position="none")
+    }
+  
+  
+  if(i!=3){
+    gg.alpha <- gg.alpha + 
+      xlab(NULL) + 
+      theme(axis.text.x = element_blank())
+    gg.beta <- gg.beta + 
+      xlab(NULL)+ 
+      theme(axis.text.x = element_blank())
+    gg.propbeta <- gg.propbeta + 
+      xlab(NULL)+ 
+      theme(axis.text.x = element_blank())
+    gg.gamma <- gg.gamma + 
+      xlab(NULL)+ 
+      theme(axis.text.x = element_blank())
+  }
+  
+  gg.panel <- cowplot::plot_grid(gg.title, gg.alpha, gg.beta, gg.propbeta, gg.gamma, 
+                                 nrow=1, rel_widths = c(0.08, 1,.94, .94, .94))
+  return(gg.panel)
+}
+
+
+mypath <- "_data/Experiment_30Oct2020_FactorInteraction&TraitCorr_XY_DataExamples/"
+myfiles <- list.files(path=mypath, pattern = "FinalSimulatedData.txt", recursive = T, full.names = T)
+
+panel.list <- list()
+for(i in 1:length(myfiles)){
+  toparse <- myfiles[i]
+  Inter <- regmatches(toparse, gregexpr("Inter=[[:digit:]]+", toparse))[[1]]
+  Corr <- regmatches(toparse, gregexpr("Corr=[[:digit:]]+", toparse))[[1]]
+  div.summary <- do.the.parse(toparse) 
+  panel.list[[i]] <- create.panel2(div.summary, i=i,tit=paste(Inter, Corr))
+  }
+
+gg.SXXK <- cowplot::plot_grid(plotlist=panel.list, nrow=3)
+ggsave(filename = "_pics/R1/FigSXXK_Panel_abg.png", 
+       width=10, height=6, device="png", dpi = 300, plot = gg.SXXK)
+
+
+
+
+
+
-- 
GitLab