rm(list=ls()) # Load libraries library(readxl) library(UpSetR) library(ggplotify) library(ComplexUpset) library(ggplot2) library(tidyr) library(dplyr) library(tidyverse) # interactions # Read data from Excel data <- read_excel("data-METANETWORKS-QC.xlsx", sheet = "aggregated") # Convert to data.frame and set row names as sites data <- as.data.frame(data) rownames(data) <- data[[1]] data <- data[, -1] # Transpose so each interaction is a row and each site a column data_t <- as.data.frame(t(data)) # Install ggplotify if not already installed # Create colors by domain colores_dominios <- c("Chaco1" = "#d95f02", "Chaco2" = "#d95f02", "Chaco3" = "#d95f02", "Paraná1" = "#1b9e77", "Paraná2" = "#1b9e77", "Paraná3" = "#1b9e77") data_long <- data_t %>% tibble::rownames_to_column("interaccion") %>% pivot_longer(-interaccion, names_to = "sitio", values_to = "presencia") %>% pivot_wider(names_from = sitio, values_from = presencia) # Plot with ComplexUpset ComplexUpset::upset( data_long, intersect = c("Chaco1", "Chaco2", "Chaco3", "Paraná1", "Paraná2", "Paraná3"), name = "Shared interactions", base_annotations=list( 'Intersection size'=intersection_size(text = list(size = 4)) ), themes = upset_modify_themes( list( 'intersect_size'=theme(text = element_text(size = 14)), 'set_size'=theme(text = element_text(size = 14)) ) ) ) # Assuming 'data_t' is your presence table (interactions by site) # Convert to long then tidy wide data_long <- data_t %>% tibble::rownames_to_column("interaccion") %>% pivot_longer(-interaccion, names_to = "sitio", values_to = "presencia") %>% pivot_wider(names_from = sitio, values_from = presencia) # Add column to classify combinations data_long <- data_long %>% mutate( tipo_interseccion = case_when( (Chaco1 + Chaco2 + Chaco3 > 0) & (Paraná1 + Paraná2 + Paraná3 == 0) ~ "Chaco", (Paraná1 + Paraná2 + Paraná3 > 0) & (Chaco1 + Chaco2 + Chaco3 == 0) ~ "Paraná", TRUE ~ "Ambos" ) ) # Color palette colores_tipo <- c("Chaco" = "#d95f02", "Paraná" = "#1b9e77", "Ambos" = "gray30") # Create the plot upset( data_long, intersect = c("Chaco1", "Chaco2", "Chaco3", "Paraná1", "Paraná2", "Paraná3"), name = "Shared interactions", base_annotations=list( 'Intersection size'=intersection_size(aes(fill = tipo_interseccion)) + scale_fill_manual(values = colores_tipo) + theme(legend.position = "right") ) ) # Convert and prepare data data_long <- data_t %>% tibble::rownames_to_column("interaccion") %>% pivot_longer(-interaccion, names_to = "sitio", values_to = "presencia") %>% pivot_wider(names_from = sitio, values_from = presencia) # Classify combinations data_long <- data_long %>% mutate( grupo_color = case_when( (Chaco1 + Chaco2 + Chaco3 > 0) & (Paraná1 + Paraná2 + Paraná3 == 0) ~ "Chaco", (Paraná1 + Paraná2 + Paraná3 > 0) & (Chaco1 + Chaco2 + Chaco3 == 0) ~ "Paraná", TRUE ~ "Chaco/Paraná" ) ) # Color palette colores <- c("Chaco" = "#d95f02", "Paraná" = "#1b9e77", "Chaco/Paraná" = "gray30") # Final plot upset_plot_interactions <- upset( data_long, intersect = c("Chaco1", "Chaco2", "Chaco3", "Paraná1", "Paraná2", "Paraná3"), name = "Shared interactions", base_annotations=list( 'Intersection size'=intersection_size(aes(fill = grupo_color)) + scale_fill_manual(values = colores, name = NULL) + # <- this hides the legend title theme(legend.position = "right") ) ) # Save as PNG ggsave("upset_plotinteractions.png", plot = upset_plot, dpi = 600, width = 10, height = 8, units = "in") # BIRDS # Read data from Excel data <- read_excel("data-birds.xlsx", sheet = "aggregated") # Convert to data.frame and set row names as sites data <- as.data.frame(data) rownames(data) <- data[[1]] data <- data[, -1] # Transpose so each interaction is a row and each site a column data_t <- as.data.frame(t(data)) # Install ggplotify if not already installed # Create colors by domain colores_dominios <- c("Chaco1" = "#d95f02", "Chaco2" = "#d95f02", "Chaco3" = "#d95f02", "Paraná1" = "#1b9e77", "Paraná2" = "#1b9e77", "Paraná3" = "#1b9e77") data_long <- data_t %>% tibble::rownames_to_column("interaccion") %>% pivot_longer(-interaccion, names_to = "sitio", values_to = "presencia") %>% pivot_wider(names_from = sitio, values_from = presencia) # Plot with ComplexUpset ComplexUpset::upset( data_long, intersect = c("Chaco1", "Chaco2", "Chaco3", "Paraná1", "Paraná2", "Paraná3"), name = "Shared bird species", base_annotations=list( 'Intersection size'=intersection_size(text = list(size = 4)) ), themes = upset_modify_themes( list( 'intersect_size'=theme(text = element_text(size = 14)), 'set_size'=theme(text = element_text(size = 14)) ) ) ) # Assuming 'data_t' is your presence table (interactions by site) # Convert to long then tidy wide data_long <- data_t %>% tibble::rownames_to_column("interaccion") %>% pivot_longer(-interaccion, names_to = "sitio", values_to = "presencia") %>% pivot_wider(names_from = sitio, values_from = presencia) # Add column to classify combinations data_long <- data_long %>% mutate( tipo_interseccion = case_when( (Chaco1 + Chaco2 + Chaco3 > 0) & (Paraná1 + Paraná2 + Paraná3 == 0) ~ "Chaco", (Paraná1 + Paraná2 + Paraná3 > 0) & (Chaco1 + Chaco2 + Chaco3 == 0) ~ "Paraná", TRUE ~ "Ambos" ) ) # Color palette colores_tipo <- c("Chaco" = "#d95f02", "Paraná" = "#1b9e77", "Ambos" = "gray30") # Create the plot upset( data_long, intersect = c("Chaco1", "Chaco2", "Chaco3", "Paraná1", "Paraná2", "Paraná3"), name = "Shared bird species", base_annotations=list( 'Intersection size'=intersection_size(aes(fill = tipo_interseccion)) + scale_fill_manual(values = colores_tipo) + theme(legend.position = "right") ) ) # Convert and prepare data data_long <- data_t %>% tibble::rownames_to_column("interaccion") %>% pivot_longer(-interaccion, names_to = "sitio", values_to = "presencia") %>% pivot_wider(names_from = sitio, values_from = presencia) # Classify combinations data_long <- data_long %>% mutate( grupo_color = case_when( (Chaco1 + Chaco2 + Chaco3 > 0) & (Paraná1 + Paraná2 + Paraná3 == 0) ~ "Chaco", (Paraná1 + Paraná2 + Paraná3 > 0) & (Chaco1 + Chaco2 + Chaco3 == 0) ~ "Paraná", TRUE ~ "Chaco/Paraná" ) ) # Color palette colores <- c("Chaco" = "#d95f02", "Paraná" = "#1b9e77", "Chaco/Paraná" = "gray30") # Final plot upset_plot_birds <- upset( data_long, intersect = c("Chaco1", "Chaco2", "Chaco3", "Paraná1", "Paraná2", "Paraná3"), name = "Shared bird species", base_annotations=list( 'Intersection size'=intersection_size(aes(fill = grupo_color)) + scale_fill_manual(values = colores, name = NULL) + # <- this hides the legend title theme(legend.position = "right") ) ) # Save as PNG ggsave("upset_plotbirds.png", plot = upset_plot, dpi = 600, width = 10, height = 8, units = "in") # plants # Read data from Excel data <- read_excel("data-plants.xlsx", sheet = "aggregated") # Convert to data.frame and set row names as sites data <- as.data.frame(data) rownames(data) <- data[[1]] data <- data[, -