rm(list=ls()) library(bipartite) library(betapart) library(readxl) library(fields) library(ggplot2) library(vegan) library(pheatmap) library(reshape2) library(gridExtra) library(grid) install.packages("gridExtra") # Read the matrix from the Excel file ruta_archivo <- "data-METANETWORKS-QC.xlsx" # Change to the path of your file datos <- read_excel(ruta_archivo, sheet = "aggregated") # Change "Hoja2" if necessary # Convert all columns to numeric datos[] <- lapply(datos, as.numeric) # Check if the conversion was successful str(datos) # Calculate beta diversity for the Chaco domain using Sørensen and Simpson indices with betapart beta <- beta.pair(datos, index.family = "sorensen") # Check that beta diversity was calculated correctly print(beta$beta.sor) print(beta$beta.sim) print(beta$beta.sne) # Convert the results to distance matrices beta_sor_matrix <- as.matrix(beta$beta.sor) beta_sim_matrix <- as.matrix(beta$beta.sim) beta_sne_matrix <- as.matrix(beta$beta.sne) # Assign site names to rows and columns rownames(beta_sor_matrix) <- c("Chaco1", "Paraná1", "Chaco2", "Paraná2", "Chaco3", "Paraná3") colnames(beta_sor_matrix) <- c("Chaco1", "Paraná1", "Chaco2", "Paraná2", "Chaco3", "Paraná3") rownames(beta_sim_matrix) <- c("Chaco1", "Paraná1", "Chaco2", "Paraná2", "Chaco3", "Paraná3") colnames(beta_sim_matrix) <- c("Chaco1", "Paraná1", "Chaco2", "Paraná2", "Chaco3", "Paraná3") rownames(beta_sne_matrix) <- c("Chaco1", "Paraná1", "Chaco2", "Paraná2", "Chaco3", "Paraná3") colnames(beta_sne_matrix) <- c("Chaco1", "Paraná1", "Chaco2", "Paraná2", "Chaco3", "Paraná3") # Create each heatmap with smaller cells p1 <- pheatmap(beta_sor_matrix, main = "Beta-diversity (Sørensen Index)", display_numbers = TRUE, number_format = "%.2f", # Number format with 2 decimals color = colorRampPalette(c("white", "lightyellow", "orange", "darkorange", "orangered"))(100), cluster_rows = FALSE, cluster_cols = FALSE, legend = TRUE, silent = TRUE, fontsize_number = 8, # Font size of numbers cellheight = 20, # Adjust cell height cellwidth = 30) # Adjust cell width p2 <- pheatmap(beta_sim_matrix, main = "Beta-diversity (Simpson Index)", display_numbers = TRUE, number_format = "%.2f", # Number format with 2 decimals color = colorRampPalette(c("white", "lightyellow", "orange", "darkorange", "orangered"))(100), cluster_rows = FALSE, cluster_cols = FALSE, legend = TRUE, silent = TRUE, fontsize_number = 8, # Font size of numbers cellheight = 20, # Adjust cell height cellwidth = 30) # Adjust cell width p3 <- pheatmap(beta_sne_matrix, main = "Nestedness", display_numbers = TRUE, number_format = "%.2f", # Number format with 2 decimals color = colorRampPalette(c("white", "lightyellow", "orange", "darkorange", "orangered"))(100), cluster_rows = FALSE, cluster_cols = FALSE, legend = TRUE, silent = TRUE, fontsize_number = 8, # Font size of numbers cellheight = 20, # Adjust cell height cellwidth = 30) # Adjust cell width # Arrange the three plots in a single vertical panel grid.arrange(p1$gtable, p2$gtable, p3$gtable, ncol = 1) # Add a general title at the top grid.text("Beta-diversity and Nestedness Analysis", x = 0.5, y = 1, gp = gpar(fontsize = 14, fontface = "bold")) # Save as PNG png("Beta_Diversity_Interactions.png", width = 3000, height = 2200, res = 300) # Adjust size and resolution # Create each heatmap with smaller cells and individual titles with smaller font p1 <- pheatmap(beta_sor_matrix, main = "βsor", display_numbers = TRUE, number_format = "%.2f", # Number format with 2 decimals color = colorRampPalette(c("white", "lightyellow", "orange", "darkorange", "orangered"))(100), cluster_rows = FALSE, cluster_cols = FALSE, legend = TRUE, silent = TRUE, fontsize = 7, # Reduce overall text size (including title) fontsize_number = 8, # Font size of numbers cellheight = 20, # Adjust cell height cellwidth = 30) # Adjust cell width p2 <- pheatmap(beta_sim_matrix, main = "βsim", display_numbers = TRUE, number_format = "%.2f", # Number format with 2 decimals color = colorRampPalette(c("white", "lightyellow", "orange", "darkorange", "orangered"))(100), cluster_rows = FALSE, cluster_cols = FALSE, legend = TRUE, silent = TRUE, fontsize = 7, # Reduce overall text size (including title) fontsize_number = 8, # Font size of numbers cellheight = 20, # Adjust cell height cellwidth = 30) # Adjust cell width p3 <- pheatmap(beta_sne_matrix, main = "βsne", display_numbers = TRUE, number_format = "%.2f", # Number format with 2 decimals color = colorRampPalette(c("white", "lightyellow", "orange", "darkorange", "orangered"))(100), cluster_rows = FALSE, cluster_cols = FALSE, legend = TRUE, silent = TRUE, fontsize = 7, # Reduce overall text size (including title) fontsize_number = 8, # Font size of numbers cellheight = 20, # Adjust cell height cellwidth = 30) # Adjust cell width # Arrange the three plots in a single vertical panel grid.arrange(p1$gtable, p2$gtable, p3$gtable, ncol = 1) # Add a general title at the top grid.text("A)", x = 0.30, y = 0.95, gp = gpar(fontsize = 10, fontface = "bold")) dev.off() #### BIRDS rm(list=ls()) library(bipartite) library(betapart) library(readxl) library(fields) library(ggplot2) library(vegan) library(pheatmap) library(reshape2) library(gridExtra) library(grid) install.packages("gridExtra") # Read the matrix from the Excel file ruta_archivo <- "data-birds.xlsx" # Change to the path of your file datos <- read_excel(ruta_archivo, sheet = "aggregated") # Change "Hoja2" if necessary # Convert all columns to numeric datos[] <- lapply(datos, as.numeric) # Check if the conversion was successful str(datos) # Calculate beta diversity for the Chaco domain using Sørensen and Simpson indices with betapart beta <- beta.pair(datos, index.family = "sorensen") # Check that beta diversity was calculated correctly print(beta$beta.sor) print(beta$beta.sim) print(beta$beta.sne) # Load required libraries library(pheatmap) # Plot heatmap for Sørensen pheatmap(beta$beta.sor, main = "Beta-diversity (Sørensen Index)", display_numbers = TRUE, color = colorRampPalette(c("white", "blue"))(50), cluster_rows = TRUE, cluster_cols = TRUE, legend = TRUE) # Plot heatmap for Simpson pheatmap(beta$beta.sim, main = "Beta-diversity (Simpson Index)", display_numbers = TRUE, color = colorRampPalette(c("white", "red"))(50), cluster_rows = TRUE, cluster_cols = TRUE, legend = TRUE) # Plot heatmap for Nestedness pheatmap(beta$beta.sne, main = "Nestedness", display_numbers = TRUE, color = colorRampPalette(c("white", "green"))(50), cluster_rows = TRUE, cluster_cols = TRUE, legend = TRUE) # Convert the results to distance matrices beta_sor_matrix <- as.matrix(beta$beta.sor) beta_sim_matrix <- as.matrix(beta$beta.sim) beta_sne_matrix <- as.matrix(beta$beta.sne) # Assign site names to rows and columns rownames(beta_sor_matrix) <- c("Chaco1", ...