Data analysis for Langevin manuscript

impact of avian guano on cliffs

overview of analysis steps:

  1. Import and wrangle data
  2. Calculated indiciators i.e. richness and diversity
  3. Use paired t tests to test for differences in N and indicators by nest/no nest
  4. NMDS so assess plant community composition
  5. Rank abundance species lists and indicator species analysis

notes to future self -

core of analysis was with only cliff face plots.

ecological indicators were calculated for each sammple quadrat, then averaged to the plot level (2 quad per plot)

NMDS was with only plots below nests

Step 0 get set up

library required packages

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
#install.packages("vegan")
library(vegan)
## Loading required package: permute
## Loading required package: lattice
## This is vegan 2.6-4
#install.packages("BiodiversityR")
library(BiodiversityR)
## Loading required package: tcltk
## BiodiversityR 2.15-4: Use command BiodiversityRGUI() to launch the Graphical User Interface; 
## to see changes use BiodiversityRGUI(changeLog=TRUE, backward.compatibility.messages=TRUE)
#install.packages("indicspecies")
library(indicspecies)
#install.packages("ggpubr")
library(ggpubr)
#install.packages("RColorBrewer")
library(RColorBrewer)

Also set working directory.

Step 1 import and wrangle data

read in the core files we will be working with

cover = quad level cover for all species sp_code = species code for each of the species #

cover = read.csv("Raw data input/All Quadrats - Short Cod Master.csv", header = T)
sp_codes = read.csv("Raw data input/Species Codes - Species Codes.csv", header = T)

Remove quads with no veg cover

# cover$total_cover <- cover$BryoC + cover$LichC + cover$VascC
# summary(cover$total_cover)
# 
# cover_low <- cover %>%
#   filter(total_cover < 1)
# 
# cover <- cover %>% 
#   filter(total_cover > 1)

add meaningful cols for abio data

i.e. translate cliff position to a word value instead of a number. can always go back to number later.

# first tank the currrent columns and rename them 
cover <- cover %>%
  mutate(
    quad_num = Transect.Code,
    Site_num = Site,
    plot_num = plot,
    clfpos_num = clfpos,
    L3R4_num = L3R4)
# site - translate numbers to meaningful values 
# SNA (Snake Mountain) # 1 
# BUZ (Buzzards Roost) # 2
# PIL (Pilot Mountain # 3
# TAB (Table Rock) # 4
# BLC (Big Lost Cove) # 5
# CHM (Chimney Rock) # 6
# NRS (New River State Park) # 7
# GOA (Goat Rock) # 8
# BLC2 (Big Lost Cove site 2) # 9
cover <- cover %>%
  mutate(Site_name = case_when(
    Site_num == 1 ~ "SNA", Site_num == 2 ~ "BUZ", Site_num == 3 ~ "PIL",
    Site_num == 4 ~ "TAB", Site_num == 5 ~ "BLC", Site_num == 6 ~ "CHM",
    Site_num == 7 ~ "NRS",Site_num == 8 ~ "GOA", Site_num == 9 ~ "BLC2",
    TRUE ~ as.character(Site_num)  # Handle other values
  ))

# quad pairs
# for L3R4 make 3= L; 4 = R
cover <- cover %>%
  mutate(Quad_pos = case_when(
    L3R4_num == 3 ~ "L",
    L3R4_num == 4 ~ "R",
    TRUE ~ as.character(L3R4)  # Handle other values
  ))

#for clfpos, create new column called cliff position based on clfpos
# 8 = Edge, # 1, 2, 3, 4, = Face, # 15 = Talus
cover <- cover %>%
  mutate(Cliff_Position = case_when(
    clfpos == 8 ~ "Edge",
    clfpos %in% c(1, 2, 3, 4) ~ "Face",
    clfpos == 15 ~ "Talus",
    TRUE ~ as.character(clfpos)))

# then assign plot_location based on clfpos
# Edge = 8
# P1  1, # P2 2, # P3 3, # P4 4, # TALUS 15
cover <- cover %>%
  mutate(Plot_Location = case_when(
    clfpos == 8 ~ "Edge",  clfpos == 1 ~ "P1",
    clfpos == 2 ~ "P2",  clfpos == 3 ~ "P3",
    clfpos == 4 ~ "P4",  clfpos == 15 ~ "TALUS",
    TRUE ~ as.character(clfpos)  # Handle other values
  ))

#  NP1P2 is poop or no poop
#1 = control #2 = nest 
cover <- cover %>%
  mutate(Control_or_treatment = 
           ifelse(NP1P2 == 1, "Control", 
                  ifelse(NP1P2 == 2, "Nest",as.character(NP1P2))))
#Nest Position: This is the postion of the sample (quadrat) relative to the nest (or their equivalent on the control)
cover <- cover %>%
  mutate(Nest_Position = case_when(
    cover$plot %in% c(
      2, 3, 4, 7, 8, 9, 10, 13, 14, 15, 17, 18, 19, 20, 23, 24, 25, 29, 30, 31, 34, 35, 38, 39, 40, 41, 43, 44, 45, 48, 49, 50, 53, 54, 55, 56, 58, 59, 60, 63, 67, 68, 71, 72, 73, 76, 77, 78, 79, 82, 83, 84, 85, 88, 89, 90
    ) ~ "below_nest",
    TRUE ~ "above_nest"))

filter out edge, and talus plots also remove the few above nest plots

#save a version of cover with the edge and talus plots
cover_OG_withEdgeTalus <- cover 

cover <- cover %>%filter(Cliff_Position != "Edge")
cover <- cover %>%filter(Cliff_Position != "Talus")

Create columns for unique ID based on transect, plot or quad

# create unique identified to each quad - make this the first column 
# merge site_name, Control_or_treament, plot number, quad number
# seperate values by underscores
cover <- cover %>%
  mutate(Quad_Identifier = 
           paste(Site_name, Control_or_treatment, plot_num, quad_num, Quad_pos, sep = "_")) %>%
  select(Quad_Identifier, everything())

# create unique identified for each plot 
# merge site_name, Control_or_treament, plot number, 
cover <- cover %>%
  mutate(Plot_Identifier = 
           paste(Site_name, Control_or_treatment, plot_num, sep = "_")) %>%
  select(Plot_Identifier, everything())

# identify transect pairs
cover <- cover %>%
  mutate(T_Pair_identifer = paste(Site_name, Cliff_Position, sep = "_")) %>%
  select(T_Pair_identifer, everything())

#identify transect names
cover <- cover %>%
  mutate(Transect_name = paste(Site_name, Control_or_treatment, sep = "_")) %>%
  select(Transect_name, everything())

Create a plot metadata / abio sheet for future reference

also seperate out cover only and link by quad and plot ID

# select these cols 
plot_metadata <- cover %>%
  select(
    Transect_name, T_Pair_identifer, Quad_Identifier, Plot_Identifier,
    Site_name,  Quad_pos,  Cliff_Position,
    Plot_Location,Nest_Position, Control_or_treatment, 
    plot_num,quad_num)

# select those same cols and add on 
plot_enviro <- cover %>%
  select(
    Transect_name, T_Pair_identifer, Quad_Identifier, Plot_Identifier,
    Site_name,  Quad_pos,  Cliff_Position,
    Plot_Location,Nest_Position, Control_or_treatment, 
    plot_num,quad_num, 
    Heter, SL, AS, wpn, wpw, transdist, NH4, NO31, NO32)

# create subset data with just species cover values and Quad ID to use for future indicies calculations
cover_all <- cover %>%
  select(Quad_Identifier, Ssp1:Ssp192)

summarize number of plots, transects, etc for methods

# Calculate counts of plots, transects, and quadrats
count_summary <- cover %>%
  summarise(
    Number_of_Plots = n_distinct(Plot_Identifier),
    Number_of_Transects = n_distinct(Quad_Identifier),
    Number_of_Quadrats = n_distinct(quad_num))
count_summary
##   Number_of_Plots Number_of_Transects Number_of_Quadrats
## 1              62                 122                122
# Calculate counts by "Cliff_Position" and "Control_or_treatment"
count_by_cliff_control <- plot_metadata %>%
  group_by(Control_or_treatment, Cliff_Position) %>%
  summarise(Coun_quadst = n_distinct(quad_num))
## `summarise()` has grouped output by 'Control_or_treatment'. You can override
## using the `.groups` argument.
count_by_cliff_control
## # A tibble: 2 × 3
## # Groups:   Control_or_treatment [2]
##   Control_or_treatment Cliff_Position Coun_quadst
##   <chr>                <chr>                <int>
## 1 Control              Face                    62
## 2 Nest                 Face                    60
# Calculate counts by "Cliff_Position" and "Control_or_treatment"
count_by_plot_control <- plot_metadata %>%
  group_by(Control_or_treatment, Cliff_Position, Plot_Location) %>%
  summarise(Coun_quadst = n_distinct(quad_num))
## `summarise()` has grouped output by 'Control_or_treatment', 'Cliff_Position'.
## You can override using the `.groups` argument.
count_by_plot_control
## # A tibble: 8 × 4
## # Groups:   Control_or_treatment, Cliff_Position [2]
##   Control_or_treatment Cliff_Position Plot_Location Coun_quadst
##   <chr>                <chr>          <chr>               <int>
## 1 Control              Face           P1                     18
## 2 Control              Face           P2                     18
## 3 Control              Face           P3                     18
## 4 Control              Face           P4                      8
## 5 Nest                 Face           P1                     18
## 6 Nest                 Face           P2                     18
## 7 Nest                 Face           P3                     16
## 8 Nest                 Face           P4                      8
# Calculate counts sucessful traps 
# Count the number of "ND" values for NH4 by "Control_or_treatment"
count_ND_NH4 <- plot_enviro %>%
  group_by(Control_or_treatment, Cliff_Position) %>%
  summarise(ND_NH4 = sum(NH4 == "ND"))
## `summarise()` has grouped output by 'Control_or_treatment'. You can override
## using the `.groups` argument.
# Count the number of "ND" values for NO32 by "Control_or_treatment"
count_ND_NO32 <- plot_enviro %>%
  group_by(Control_or_treatment, Cliff_Position) %>%
  summarise(ND_NO32 = sum(NO32 == "ND"))
## `summarise()` has grouped output by 'Control_or_treatment'. You can override
## using the `.groups` argument.
# Merge the two data frames to combine the counts
count_ND_NH4_NO32 <- full_join(count_ND_NH4, count_ND_NO32, by = c("Control_or_treatment", "Cliff_Position"))
count_ND_NH4_NO32 <- full_join(count_ND_NH4_NO32, count_by_cliff_control, by = c("Control_or_treatment", "Cliff_Position"))


# Print the resulting data frame
count_ND_NH4_NO32
## # A tibble: 2 × 5
## # Groups:   Control_or_treatment [2]
##   Control_or_treatment Cliff_Position ND_NH4 ND_NO32 Coun_quadst
##   <chr>                <chr>           <int>   <int>       <int>
## 1 Control              Face               24      24          62
## 2 Nest                 Face               24      24          60
write.csv(count_ND_NH4_NO32, "Intermediate data files/count_of_quadrats_by_trt_trapsucess.csv", row.names = F)

Subset quad data into analysis groups: all taxa, lichen, bryo, plants

also remove the algae

# transofrm the cover_all to long format for subsetting 
cover_all_long <- cover_all %>%
  pivot_longer(cols = starts_with("Ssp"), 
               names_to = "species_code", 
               values_to = "species_cover")
# Remove rows with species_cover equal to 0
cover_all_long <- cover_all_long %>%
  filter(species_cover != 0)


# prepare to assign taxa to each species 
# Remove spaces and periods from the "Number" column in the sp_codes data frame
sp_codes <- sp_codes %>%
  mutate(Number = gsub("[ .]", "", Number))

# Remove spaces and periods from the "species_code" column in the cover_all_long data frame
cover_all_long <- cover_all_long %>%
  mutate(species_code = gsub("[ .]", "", species_code))

# Join the cleaned data frames
cover_all_long_with_taxa <- cover_all_long %>%
  left_join(sp_codes, by = c("species_code" = "Number"))

# summary(as.factor(cover_all_long_with_taxa$taxa)) # cool 

# remove the algae
cover_all_long_with_taxa <- cover_all_long_with_taxa %>%
  filter(taxa != "A")

Subset data based on taxa, spread back to wide

alltaxa_long <- cover_all_long_with_taxa

# Subset data based on taxa
lichens_long <- alltaxa_long %>% filter(taxa == "L")
plants_long <- alltaxa_long %>% filter(taxa == "V")
bryo_long <- alltaxa_long %>% filter(taxa == "B")


# Spread back to wide format
alltaxa_wide <- alltaxa_long %>%
  select(-taxa,-Species, -Notes) %>%
  pivot_wider(names_from = species_code, values_from = species_cover,
              values_fill = 0)

lichens_wide <- lichens_long %>%
  select(-taxa,-Species, -Notes) %>%
  pivot_wider(names_from = species_code, values_from = species_cover, 
              values_fill = 0)

plants_wide <- plants_long %>%
  select(-taxa,-Species, -Notes) %>%
  pivot_wider(names_from = species_code, values_from = species_cover, 
              values_fill = 0)

bryo_wide <- bryo_long %>%
  select(-taxa,-Species, -Notes) %>%
  pivot_wider(names_from = species_code, values_from = species_cover, 
              values_fill = 0)

Step 2 calculate indicators

Calculate cover, eveness, richness and divestiy for each funcational group at each quad

# create output df 
indic <- alltaxa_wide %>% select(Quad_Identifier)
l_indic <- lichens_wide %>%   select(Quad_Identifier)
p_indic <- plants_wide %>%  select(Quad_Identifier)
b_indic <- bryo_wide %>%  select(Quad_Identifier)

# turn wide format species cover to matrix by assigning the quad ID col to row names
all.matrix <- alltaxa_wide %>%  column_to_rownames(var = "Quad_Identifier")
lichen.matrix <- lichens_wide %>%  column_to_rownames(var = "Quad_Identifier")
plant.matrix <- plants_wide %>%  column_to_rownames(var = "Quad_Identifier")
bryo.matrix <- bryo_wide %>%  column_to_rownames(var = "Quad_Identifier")
  

### calculate indicators for each taxa group
# all 
indic$All_Richness <- rowSums(all.matrix > 0)
indic$All_Shannon <- diversity(all.matrix)
indic$All_Simpson <- diversity(all.matrix, index = "simpson")
indic$All_Cover <- rowSums(all.matrix)
indic$All_Evenness <- indic$All_Shannon / log(indic$All_Richness)
# lichens 
l_indic$Lichen_Richness <- rowSums(lichen.matrix > 0)
l_indic$Lichen_Shannon <- diversity(lichen.matrix)
l_indic$Lichen_Simpson <- diversity(lichen.matrix, index = "simpson")
l_indic$Lichen_Cover <- rowSums(lichen.matrix)
l_indic$Lichen_Evenness <- l_indic$Lichen_Shannon / log(l_indic$Lichen_Richness)
# plants
p_indic$Plant_Richness <- rowSums(plant.matrix > 0)
p_indic$Plant_Shannon <- diversity(plant.matrix)
p_indic$Plant_Simpson <- diversity(plant.matrix, index = "simpson")
p_indic$Plant_Cover <- rowSums(plant.matrix)
p_indic$Plant_Evenness <- p_indic$Plant_Shannon / log(p_indic$Plant_Richness)
# bryos 
b_indic$Bryo_Richness <- rowSums(bryo.matrix > 0)
b_indic$Bryo_Shannon <- diversity(bryo.matrix)
b_indic$Bryo_Simpson <- diversity(bryo.matrix, index = "simpson")
b_indic$Bryo_Cover <- rowSums(bryo.matrix)
b_indic$Bryo_Evenness <- b_indic$Bryo_Shannon / log(b_indic$Bryo_Richness)


# merge all the indicator df by the row name which is Quad_Identifier
# Merge data frames based on Quad_Identifier
merged_indic <- merge(indic, l_indic, by = "Quad_Identifier", all = TRUE)
merged_indic <- merge(merged_indic, p_indic, by = "Quad_Identifier", all = TRUE)
merged_indic <- merge(merged_indic, b_indic, by = "Quad_Identifier", all = TRUE)
# If there are NAs, replace them with 0
merged_indic[is.na(merged_indic)] <- 0

# Create data frames for the two new plots with indicator values filled with 0
new_plots <- data.frame(
  Quad_Identifier = c("GOA_nest_76_151_L", "BLC2_control_91_182_R"),
  All_Richness = 0,  All_Shannon = 0,  All_Simpson = 0,  All_Cover = 0,  All_Evenness = 0,
  Lichen_Richness = 0,  Lichen_Shannon = 0,  Lichen_Simpson = 0,  Lichen_Cover = 0,  Lichen_Evenness = 0,
  Plant_Richness = 0,  Plant_Shannon = 0,  Plant_Simpson = 0,  Plant_Cover = 0,  Plant_Evenness = 0,
  Bryo_Richness = 0,  Bryo_Shannon = 0, Bryo_Simpson = 0,  Bryo_Cover = 0,  Bryo_Evenness = 0)

# Concatenate the new_plots with merged_indic
merged_indic <- rbind(merged_indic, new_plots)

# add in metadata
merged_indic <- merged_indic %>%
  left_join(plot_metadata, by = "Quad_Identifier")

quad_level_indicators <- merged_indic

#save this as csv file 
#write.csv(merged_indic, "Intermediate data files/all_indicators.csv")

Average indicators to plot level

# add a plot-level indicator
head(merged_indic)
##        Quad_Identifier All_Richness All_Shannon All_Simpson All_Cover
## 1  BLC_Control_48_95_L            6    1.363419   0.6831276      27.0
## 2  BLC_Control_48_96_R            8    1.620561   0.7387958      47.0
## 3  BLC_Control_49_97_R           11    1.357773   0.5954556      77.2
## 4  BLC_Control_49_98_L            6    1.164674   0.5827219      65.0
## 5 BLC_Control_50_100_L            7    1.246507   0.6286003      58.2
## 6  BLC_Control_50_99_R            6    1.152739   0.5558633      57.0
##   All_Evenness Lichen_Richness Lichen_Shannon Lichen_Simpson Lichen_Cover
## 1    0.7609385               5       1.118541      0.5467128         17.0
## 2    0.7793249               7       1.550585      0.7277883         46.0
## 3    0.5662354               9       1.252040      0.5740048         75.2
## 4    0.6500169               6       1.164674      0.5827219         65.0
## 5    0.6405779               7       1.246507      0.6286003         58.2
## 6    0.6433560               5       0.999133      0.5082305         54.0
##   Lichen_Evenness Plant_Richness Plant_Shannon Plant_Simpson Plant_Cover
## 1       0.6949883              1             0             0          10
## 2       0.7968431              1             0             0           1
## 3       0.5698281              1             0             0           1
## 4       0.6500169              0             0             0           0
## 5       0.6405779              0             0             0           0
## 6       0.6207963              0             0             0           0
##   Plant_Evenness Bryo_Richness Bryo_Shannon Bryo_Simpson Bryo_Cover
## 1              0             0            0            0          0
## 2              0             0            0            0          0
## 3              0             1            0            0          1
## 4              0             0            0            0          0
## 5              0             0            0            0          0
## 6              0             1            0            0          3
##   Bryo_Evenness Transect_name T_Pair_identifer Plot_Identifier Site_name
## 1             0   BLC_Control         BLC_Face  BLC_Control_48       BLC
## 2             0   BLC_Control         BLC_Face  BLC_Control_48       BLC
## 3             0   BLC_Control         BLC_Face  BLC_Control_49       BLC
## 4             0   BLC_Control         BLC_Face  BLC_Control_49       BLC
## 5             0   BLC_Control         BLC_Face  BLC_Control_50       BLC
## 6             0   BLC_Control         BLC_Face  BLC_Control_50       BLC
##   Quad_pos Cliff_Position Plot_Location Nest_Position Control_or_treatment
## 1        L           Face            P1    below_nest              Control
## 2        R           Face            P1    below_nest              Control
## 3        R           Face            P2    below_nest              Control
## 4        L           Face            P2    below_nest              Control
## 5        L           Face            P3    below_nest              Control
## 6        R           Face            P3    below_nest              Control
##   plot_num quad_num
## 1       48       95
## 2       48       96
## 3       49       97
## 4       49       98
## 5       50      100
## 6       50       99
merged_indic$Plot_Identifier <- as.factor(merged_indic$Plot_Identifier)

# Summarize indicator values to the plot level
plot_average_indic <- merged_indic %>%
  group_by(Plot_Identifier) %>%
  summarize(
    All_Richness = mean(All_Richness),
    All_Shannon = mean(All_Shannon),
    All_Simpson = mean(All_Simpson),
    All_Cover = mean(All_Cover),
    All_Evenness = mean(All_Evenness),
    Lichen_Richness = mean(Lichen_Richness),
    Lichen_Shannon = mean(Lichen_Shannon),
    Lichen_Simpson = mean(Lichen_Simpson),
    Lichen_Cover = mean(Lichen_Cover),
    Lichen_Evenness = mean(Lichen_Evenness),
    Plant_Richness = mean(Plant_Richness),
    Plant_Shannon = mean(Plant_Shannon),
    Plant_Simpson = mean(Plant_Simpson),
    Plant_Cover = mean(Plant_Cover),
    Plant_Evenness = mean(Plant_Evenness),
    Bryo_Richness = mean(Bryo_Richness),
    Bryo_Shannon = mean(Bryo_Shannon),
    Bryo_Simpson = mean(Bryo_Simpson),
    Bryo_Cover = mean(Bryo_Cover),
    Bryo_Evenness = mean(Bryo_Evenness)
  )

# Keep only one row per unique Plot_Identifier
plot_metadata_unique <- plot_metadata %>% distinct(Plot_Identifier, .keep_all = TRUE)

plot_average_indic <- plot_average_indic %>%
  left_join(plot_metadata_unique, by = "Plot_Identifier")

# remove rows with NA in the control col
# Remove rows with NA in the Control_or_treatment column
plot_average_indic <- plot_average_indic[!is.na(plot_average_indic$Control_or_treatment), ]

# Check which Plot_Identifier are not common between plot_metadata and plot_average_indic

write.csv(plot_average_indic, "Intermediate data files/plot_level_indicators.csv")

Step 3 stats tests and figs for paired t tests

Tests for differences in N

average plot level N

# Create N_enviro by filtering rows where NH4 is not equal to "ND"
NH4_enviro <- plot_enviro %>%
  filter(NH4 != "ND")
N03_enviro <- plot_enviro %>%
  filter(NO32 != "ND")

Now we need to ensure that only pairs with both sucessful trap data are compared means by plot and transect levels

# Convert NH4 and NO32 to numeric
NH4_enviro$NH4 <- as.numeric(NH4_enviro$NH4)
N03_enviro$NO32 <- as.numeric(N03_enviro$NO32)

#calclate transect means
transect_nh4 <- NH4_enviro %>%
  group_by(Transect_name) %>%
  summarize(transect_mean_nh4 = mean(NH4))
t_nh4 <- transect_nh4 %>%
  mutate(Transect_name = as.character(Transect_name)) %>%
  separate(Transect_name, into = c("site", "control_or_treatment"), sep = "_") 
t_nh4$Transect_name <- transect_nh4$Transect_name

transect_no3 <- N03_enviro %>%
  group_by(Transect_name) %>%
  summarize(transect_mean_no3 = mean(NO32))
t_no3 <- transect_no3 %>%
  mutate(Transect_name = as.character(Transect_name)) %>%
  separate(Transect_name, into = c("site", "control_or_treatment"), sep = "_") 
t_no3$Transect_name <- transect_no3$Transect_name


# average N to plot levels 
p_NH4 <- NH4_enviro %>%
  group_by(Plot_Identifier) %>%
  summarize(Avg_NH4 = mean(NH4))
plot_NH4 <- p_NH4 %>%
  mutate(Plot_Identifier = as.character(Plot_Identifier)) %>%
  separate(Plot_Identifier, into = c("site", "control_or_treatment", "plot_num"), sep = "_") 
plot_NH4$Plot_Identifier <- p_NH4$Plot_Identifier

p_NO3 <- N03_enviro %>%
  group_by(Plot_Identifier) %>%
  summarize(avg_NO32 = mean(NO32))  
plot_NO3 <- p_NO3 %>%
  mutate(Plot_Identifier = as.character(Plot_Identifier)) %>%
  separate(Plot_Identifier, into = c("site", "control_or_treatment", "plot_num"), sep = "_") 
plot_NO3$Plot_Identifier <- p_NO3$Plot_Identifier

Test for differences in N At both plot and transect level using paired and unpaired t tests

# need to remove transects which do not have pairs
t_no3 <- t_no3 %>% filter(site != "CHM" & site != "PIL")
# need to remove transects which do not have pairs
t_nh4 <- t_nh4 %>% filter(site != "PIL")


# Test for differences in transect-level data (paired)
no3_ttest_transect_paired <- t.test(transect_mean_no3 ~ control_or_treatment, data = t_no3, paired = TRUE)
no3_ttest_transect_unpaired <- t.test(transect_mean_no3 ~ control_or_treatment, data = t_no3, paired = FALSE)

nh4_ttest_transect_paired <- t.test(transect_mean_nh4 ~ control_or_treatment, data = t_nh4, paired = TRUE)
nh4_ttest_transect_unpaired <- t.test(transect_mean_nh4 ~ control_or_treatment, data = t_nh4, paired = FALSE)

# Create data frames for transect-level results
transect_results_paired <- data.frame(
  Variable = c("NO3", "NH4"),
  Test = "Paired t-test",
  Mean_Control = c(no3_ttest_transect_paired$estimate[1], nh4_ttest_transect_paired$estimate[1]),
  mean_nest = c(no3_ttest_transect_paired$estimate[2], nh4_ttest_transect_paired$estimate[2]),
  P_Value = c(no3_ttest_transect_paired$p.value, nh4_ttest_transect_paired$p.value)
)

transect_results_unpaired <- data.frame(
  Variable = c("NO3", "NH4"),
  Test = "Unpaired t-test",
  Mean_Control = c(no3_ttest_transect_unpaired$estimate[1], nh4_ttest_transect_unpaired$estimate[1]),
  mean_nest = c(no3_ttest_transect_unpaired$estimate[2], nh4_ttest_transect_unpaired$estimate[2]),
  P_Value = c(no3_ttest_transect_unpaired$p.value, nh4_ttest_transect_unpaired$p.value)
)

# Test for differences in plot-level data (paired)
no3_ttest_plot_paired <- t.test(avg_NO32 ~ control_or_treatment, data = plot_NO3, paired = TRUE)
no3_ttest_plot_unpaired <- t.test(avg_NO32 ~ control_or_treatment, data = plot_NO3, paired = FALSE)

nh4_ttest_plot_paired <- t.test(Avg_NH4 ~ control_or_treatment, data = plot_NH4, paired = TRUE)
nh4_ttest_plot_unpaired <- t.test(Avg_NH4 ~ control_or_treatment, data = plot_NH4, paired = FALSE)

# Create data frames for plot-level results
plot_results_paired <- data.frame(
  Variable = c("NO3", "NH4"),
  Test = "Paired t-test",
  Mean_Control = c(no3_ttest_plot_paired$estimate[1], nh4_ttest_plot_paired$estimate[1]),
  mean_nest = c(no3_ttest_plot_paired$estimate[2], nh4_ttest_plot_paired$estimate[2]),
  P_Value = c(no3_ttest_plot_paired$p.value, nh4_ttest_plot_paired$p.value)
)

plot_results_unpaired <- data.frame(
  Variable = c("NO3", "NH4"),
  Test = "Unpaired t-test",
  Mean_Control = c(no3_ttest_plot_unpaired$estimate[1], nh4_ttest_plot_unpaired$estimate[1]),
  mean_nest = c(no3_ttest_plot_unpaired$estimate[2], nh4_ttest_plot_unpaired$estimate[2]),
  P_Value = c(no3_ttest_plot_unpaired$p.value, nh4_ttest_plot_unpaired$p.value)
)



######## save the output tables
# Combine the results into two output tables
N_results_paired <- rbind(transect_results_paired, plot_results_paired)
N_results_unpaired <- rbind(transect_results_unpaired, plot_results_unpaired)

# Combine the results with the 'Test Type' column
N_test_combined_results <- bind_rows(N_results_paired, N_results_unpaired)
N_test_combined_results
##   Variable            Test  Mean_Control    mean_nest    P_Value
## 1      NO3   Paired t-test -1.980319e-06           NA 0.34936726
## 2      NH4   Paired t-test -5.502129e-07           NA 0.07889930
## 3      NO3   Paired t-test -1.111711e-06           NA 0.27665261
## 4      NH4   Paired t-test -3.598220e-07           NA 0.06100970
## 5      NO3 Unpaired t-test  1.972884e-06 3.953204e-06 0.23881810
## 6      NH4 Unpaired t-test  3.731589e-07 9.233719e-07 0.05506528
## 7      NO3 Unpaired t-test  2.003926e-06 3.115636e-06 0.29226573
## 8      NH4 Unpaired t-test  4.027068e-07 7.625289e-07 0.06017415
# note that for the paired tests, only mean differences are reported
write.csv(N_test_combined_results, "Output figs and tables/N ttest results.csv", row.names = F)

Figure for N

# Convert NH4 data from g to ug
NH4_enviro$NH4_ug <- NH4_enviro$NH4 * 1e6

# Calculate min and max values for NH4_ug
nh4_min <- min(NH4_enviro$NH4_ug)
nh4_max <- max(NH4_enviro$NH4_ug)

# Convert NO3 data from g to ug
N03_enviro$NO32_ug <- N03_enviro$NO32 * 1e6

# Calculate min and max values for NO32_ug
no3_min <- min(N03_enviro$NO32_ug)
no3_max <- max(N03_enviro$NO32_ug)






# Define the Dark2 color palette with as many colors as needed
dark2_palette <- brewer.pal(8, "Dark2")  # You can adjust the number of colors as needed

# Create a box plot of NH4 levels with customized y-axis limits and ug labels
nh4_fig <- ggplot(NH4_enviro, aes(x = Control_or_treatment, y = NH4_ug, fill = Control_or_treatment)) +
  geom_boxplot() +
  labs(x = "Control or Treatment", y = expression("NH"[4] ~ (ug/cm^2))) +
  scale_y_continuous(labels = scales::number_format(scale = 1e-6)) +
  scale_fill_manual(values = dark2_palette) +  # Use the Dark2 color palette
  theme_classic() +
  ylim(0, 15) +
  theme(legend.position = "none")  # Remove the legend
## Scale for y is already present.
## Adding another scale for y, which will replace the existing scale.
nh4_fig
## Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).

# Create a box plot of NO3 levels with customized y-axis limits and ug labels
no3_fig <- ggplot(N03_enviro, aes(x = Control_or_treatment, y = NO32_ug, fill = Control_or_treatment)) +
  geom_boxplot() +
  labs(x = "Control or Treatment", y = expression("NO"[3] ~ (ug/cm^2))) +
  scale_y_continuous(labels = scales::number_format(scale = 1e-6)) +
  scale_fill_manual(values = dark2_palette) +  # Use the Dark2 color palette
  theme_classic() +
  ylim(0, 15) +
  theme(legend.position = "none")  # Remove the legend
## Scale for y is already present.
## Adding another scale for y, which will replace the existing scale.
no3_fig

# Combine the NH4 and NO3 box plots
combined_n_plot <- ggarrange(
  nh4_fig,  no3_fig,
  ncol = 2, nrow = 1, 
  common.legend = FALSE)
## Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).
# Display the combined plot
combined_n_plot

# Save the combined plot as an image
ggsave("Output figs and tables/combined_n_plot.png", plot = combined_n_plot, width = 8, height = 4, dpi = 300)

Summary table with means and SE

tests for differences in plot level indicators by poop

I think there is an issue with there being some empty data or plots??

# Define the indicator variables
indicator_variables <- c("All_Shannon", "All_Simpson", "All_Cover", "All_Evenness",
                         "Lichen_Richness", "Lichen_Shannon", "Lichen_Simpson", "Lichen_Cover", "Lichen_Evenness",
                         "Plant_Richness", "Plant_Shannon", "Plant_Simpson", "Plant_Cover", "Plant_Evenness",
                         "Bryo_Richness", "Bryo_Simpson", "Bryo_Cover", "Bryo_Evenness")

# Create a data frame to store the paired t-test results
results_df_paired <- data.frame(Variable = indicator_variables,
                                p_value = numeric(length(indicator_variables)),
                                t_statistic = numeric(length(indicator_variables)),
                                degrees_of_freedom = numeric(length(indicator_variables)),
                                conf_interval = character(length(indicator_variables)),
                                mean_difference = numeric(length(indicator_variables)))

# Create a data frame to store the non-paired t-test results
results_df <- data.frame(Variable = indicator_variables,
                        p_value = numeric(length(indicator_variables)),
                        t_statistic = numeric(length(indicator_variables)),
                        degrees_of_freedom = numeric(length(indicator_variables)),
                        conf_interval = character(length(indicator_variables)),
                        mean_difference = numeric(length(indicator_variables)))

# Perform paired and non-paired t-tests for each indicator variable and save the results
for (i in 1:length(indicator_variables)) {
  variable <- indicator_variables[i]
  plot_average_indic$Control_or_treatment <- as.factor(plot_average_indic$Control_or_treatment)
  
  # Paired t-test
  res_paired <- t.test(plot_average_indic[[variable]] ~ Control_or_treatment, 
                       data = plot_average_indic, paired = TRUE)
  
  # Non-paired t-test
  res <- t.test(plot_average_indic[[variable]] ~ Control_or_treatment, data = plot_average_indic)
  
  results_df_paired$p_value[i] <- res_paired$p.value
  results_df_paired$t_statistic[i] <- res_paired$statistic
  results_df_paired$degrees_of_freedom[i] <- res_paired$parameter
  results_df_paired$conf_interval[i] <- paste(res_paired$conf.int[1], res_paired$conf.int[2], sep = " - ")
  results_df_paired$mean_difference[i] <- res_paired$estimate
  
  results_df$p_value[i] <- res$p.value
  results_df$t_statistic[i] <- res$statistic
  results_df$degrees_of_freedom[i] <- res$parameter
  results_df$conf_interval[i] <- paste(res$conf.int[1], res$conf.int[2], sep = " - ")
  results_df$mean_difference[i] <- res$estimate
}
## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length

## Warning in results_df$mean_difference[i] <- res$estimate: number of items to
## replace is not a multiple of replacement length
results_df_paired
##           Variable    p_value t_statistic degrees_of_freedom
## 1      All_Shannon 0.10495230   1.6718587                 30
## 2      All_Simpson 0.10528274   1.6701996                 30
## 3        All_Cover 0.79035434  -0.2682349                 30
## 4     All_Evenness 0.36061311   0.9283942                 30
## 5  Lichen_Richness 0.05671790   1.9818954                 30
## 6   Lichen_Shannon 0.06261036   1.9338850                 30
## 7   Lichen_Simpson 0.06012180   1.9536595                 30
## 8     Lichen_Cover 0.39506424  -0.8628526                 30
## 9  Lichen_Evenness 0.12825088   1.5642358                 30
## 10  Plant_Richness 0.51095283   0.6652821                 30
## 11   Plant_Shannon 0.84018138   0.2034186                 30
## 12   Plant_Simpson 0.75608434   0.3134812                 30
## 13     Plant_Cover 0.62216620   0.4979268                 30
## 14  Plant_Evenness 0.51069763   0.6656869                 30
## 15   Bryo_Richness 0.60693283   0.5199230                 30
## 16    Bryo_Simpson 0.71573481  -0.3676237                 30
## 17      Bryo_Cover 0.13738976   1.5263823                 30
## 18   Bryo_Evenness 0.89390173  -0.1345048                 30
##                               conf_interval mean_difference
## 1   -0.0334478244999803 - 0.335380626749656      0.15096640
## 2   -0.0131798835451799 - 0.131506368425532      0.05916324
## 3       -25.374425980664 - 19.4828130774382     -2.94580645
## 4    -0.042096562930675 - 0.112269775437644      0.03508661
## 5    -0.0240766364375026 - 1.60472179772783      0.79032258
## 6  -0.00813621616176478 - 0.298474237043392      0.14516901
## 7   -0.00285414822176339 - 0.12870551954658      0.06292569
## 8      -25.7582964468183 - 10.4573287048828     -7.65048387
## 9   -0.0189819811280552 - 0.143207983108934      0.06211300
## 10   -0.267068922796073 - 0.525133438925105      0.12903226
## 11   -0.103496245307525 - 0.126394270712645      0.01144901
## 12  -0.059987354474083 - 0.0817423360052674      0.01087749
## 13     -3.53626888112508 - 5.81659146177024      1.14016129
## 14  -0.0841162850874729 - 0.165469900695506      0.04067681
## 15    -0.330583846798216 - 0.55639029841112      0.11290323
## 16 -0.0861224955717563 - 0.0598469505147911     -0.01313777
## 17     -1.20474335679817 - 8.33377561486268      3.56451613
## 18    -0.124649599548425 - 0.10924519927714     -0.00770220
results_df
##           Variable   p_value t_statistic degrees_of_freedom
## 1      All_Shannon 0.2139215   1.2569513           56.73442
## 2      All_Simpson 0.1744291   1.3786510           47.73617
## 3        All_Cover 0.8238691  -0.2236793           52.86223
## 4     All_Evenness 0.3739828   0.8992944           39.23051
## 5  Lichen_Richness 0.2313405   1.2093298           59.23962
## 6   Lichen_Shannon 0.2000526   1.2961309           58.13301
## 7   Lichen_Simpson 0.1556666   1.4410076           51.23431
## 8     Lichen_Cover 0.4704838  -0.7276449           46.43967
## 9  Lichen_Evenness 0.1315454   1.5401859           39.22000
## 10  Plant_Richness 0.5786634   0.5583812           59.96053
## 11   Plant_Shannon 0.8434649   0.1983324           59.16445
## 12   Plant_Simpson 0.7598566   0.3071364           57.11793
## 13     Plant_Cover 0.6609226   0.4408717           58.80223
## 14  Plant_Evenness 0.5172409   0.6519222           53.53019
## 15   Bryo_Richness 0.6580872   0.4447995           59.18110
## 16    Bryo_Simpson 0.7792703  -0.2815346           59.92911
## 17      Bryo_Cover 0.1342158   1.5238813           47.16670
## 18   Bryo_Evenness 0.9201544  -0.1006623           59.95464
##                               conf_interval mean_difference
## 1   -0.0895645379837867 - 0.391497340233462      1.48030776
## 2   -0.0271331854869785 - 0.145459670367331      0.68921987
## 3       -29.362641775915 - 23.4710288726892     59.64870968
## 4   -0.0438152812476329 - 0.113988493754602      0.77958630
## 5      -0.517259469814372 - 2.0979046311047      6.14516129
## 6   -0.0790160585722377 - 0.369354079453865      1.31337022
## 7   -0.0247314224306002 - 0.150582793755416      0.64252338
## 8      -28.8087334264965 - 13.5077656845611     47.53258065
## 9    -0.019443956726723 - 0.143669958707601      0.77462616
## 10   -0.333208320373466 - 0.591272836502499      0.66129032
## 11   -0.104054504326389 - 0.126952529731509      0.11803579
## 12 -0.0600383132068852 - 0.0817932947380697      0.07466886
## 13     -4.03508139385956 - 6.31540397450472      5.66451613
## 14  -0.0844431632923039 - 0.165796778900337      0.14549179
## 15   -0.394975906373918 - 0.620782357986821      0.74193548
## 16   -0.106483674988612 - 0.080208129931647      0.08488107
## 17     -1.14071436794831 - 8.26974662601282      6.45161290
## 18   -0.160757873906328 - 0.145353473635043      0.14787764
indic_paired_results<- results_df_paired
write.csv(indic_paired_results,  "Output figs and tables/Indicator paired ttest results.csv", row.names = F)

Figures

reformat the data

colnames(plot_average_indic)
##  [1] "Plot_Identifier"      "All_Richness"         "All_Shannon"         
##  [4] "All_Simpson"          "All_Cover"            "All_Evenness"        
##  [7] "Lichen_Richness"      "Lichen_Shannon"       "Lichen_Simpson"      
## [10] "Lichen_Cover"         "Lichen_Evenness"      "Plant_Richness"      
## [13] "Plant_Shannon"        "Plant_Simpson"        "Plant_Cover"         
## [16] "Plant_Evenness"       "Bryo_Richness"        "Bryo_Shannon"        
## [19] "Bryo_Simpson"         "Bryo_Cover"           "Bryo_Evenness"       
## [22] "Transect_name"        "T_Pair_identifer"     "Quad_Identifier"     
## [25] "Site_name"            "Quad_pos"             "Cliff_Position"      
## [28] "Plot_Location"        "Nest_Position"        "Control_or_treatment"
## [31] "plot_num"             "quad_num"
# Define the columns for each indicator category
richness_columns <- c("All_Richness", "Lichen_Richness", "Plant_Richness", "Bryo_Richness")
shannon_columns <- c("All_Shannon", "Lichen_Shannon", "Plant_Shannon", "Bryo_Simpson")
cover_columns <- c("All_Cover", "Lichen_Cover", "Plant_Cover", "Bryo_Cover")
evenness_columns <- c("All_Evenness", "Lichen_Evenness", "Plant_Evenness", "Bryo_Evenness")
Simpson_columns <- c("All_Simpson", "Lichen_Simpson", "Plant_Simpson", "Bryo_Simpson")

# Specify the data frame and columns to pivot for each indicator category
long_richness <- plot_average_indic %>%
  pivot_longer(cols = all_of(richness_columns), 
               names_to = "Indicator", 
               values_to = "Richness")
long_shannon <- plot_average_indic %>%
  pivot_longer(cols = all_of(shannon_columns), 
               names_to = "Indicator", 
               values_to = "Shannon")
long_cover <- plot_average_indic %>%
  pivot_longer(cols = all_of(cover_columns), 
               names_to = "Indicator", 
               values_to = "Cover")
long_evenness <- plot_average_indic %>%
  pivot_longer(cols = all_of(evenness_columns), 
               names_to = "Indicator", 
               values_to = "Evenness")
long_simpson <- plot_average_indic %>%
  pivot_longer(cols = all_of(Simpson_columns), 
               names_to = "Indicator", 
               values_to = "Simpson")

# Extract the taxon from the Indicator column for each indicator category
long_richness <- long_richness %>%
  mutate(Taxon = sub(".*?([A-Za-z]+).*", "\\1", Indicator))
long_shannon <- long_shannon %>%
  mutate(Taxon = sub(".*?([A-Za-z]+).*", "\\1", Indicator))
long_cover <- long_cover %>%
  mutate(Taxon = sub(".*?([A-Za-z]+).*", "\\1", Indicator))
long_evenness <- long_evenness %>%
  mutate(Taxon = sub(".*?([A-Za-z]+).*", "\\1", Indicator))
long_simpson <- long_simpson %>%
  mutate(Taxon = sub(".*?([A-Za-z]+).*", "\\1", Indicator))

FIgure for cover

# Order the 'Taxon' factor variable
long_cover$Taxon <- factor(long_cover$Taxon, levels = c("All", "Lichen", "Bryo", "Plant"))

# Create the grouped box plot with ordered Taxon levels
gg_boxplot_cover <- ggplot(long_cover, aes(x = Control_or_treatment, y = Cover, fill = Taxon)) +
  geom_boxplot() +
  scale_fill_brewer(palette = "Dark2") +
  theme_classic() +
  labs(x = "Control or Treatment", y = "Cover (%)") +
  theme(axis.title.x = element_blank()) +
  theme(legend.position = "top")+
  scale_y_continuous(expand = expansion(add = 0, mult = 0))

gg_boxplot_cover

Figures for richness, diversity, and eveness

# Order the 'Taxon' factor variable
long_cover$Taxon <- factor(long_cover$Taxon, levels = c("All", "Lichen", "Bryo", "Plant"))
long_richness$Taxon <- factor(long_richness$Taxon, levels = c("All", "Lichen", "Bryo", "Plant"))
long_shannon$Taxon <- factor(long_shannon$Taxon, levels = c("All", "Lichen", "Bryo", "Plant"))
long_evenness$Taxon <- factor(long_evenness$Taxon, levels = c("All", "Lichen", "Bryo", "Plant"))
long_simpson$Taxon <- factor(long_simpson$Taxon, levels = c("All", "Lichen", "Bryo", "Plant"))

# Create the grouped box plot with ordered Taxon levels
gg_boxplot_cover <- ggplot(long_cover, aes(x = Control_or_treatment, y = Cover, fill = Taxon)) +
  geom_boxplot() +scale_fill_brewer(palette = "Dark2") +
  theme_classic() + labs(x = "Control or Treatment", y = "Cover (%)") +
  theme(axis.title.x = element_blank()) + theme(legend.position = "top")+
  scale_y_continuous(expand = expansion(add = 0, mult = 0))
gg_boxplot_cover

gg_boxplot_richness <- ggplot(long_richness, aes(x = Control_or_treatment, y = Richness, fill = Taxon)) +
  geom_boxplot() +scale_fill_brewer(palette = "Dark2") +
  theme_classic() + labs(x = "Control or Treatment", y = "Richness") +
  theme(axis.title.x = element_blank()) + theme(legend.position = "top")+
  scale_y_continuous(expand = expansion(add = 0, mult = 0))
gg_boxplot_richness

gg_boxplot_shannon <- ggplot(long_shannon, aes(x = Control_or_treatment, y = Shannon, fill = Taxon)) +
  geom_boxplot() +scale_fill_brewer(palette = "Dark2") +
  theme_classic() + labs(x = "Control or Treatment", y = "Shannon Diversity Index") +
  theme(axis.title.x = element_blank()) + theme(legend.position = "top")+
  scale_y_continuous(expand = expansion(add = 0, mult = 0))
gg_boxplot_shannon

gg_boxplot_simpson <- ggplot(long_simpson, aes(x = Control_or_treatment, y = Simpson, fill = Taxon)) +
  geom_boxplot() +scale_fill_brewer(palette = "Dark2") +
  theme_classic() + labs(x = "Control or Treatment", y = "Simpson Diversity Index") +
  theme(axis.title.x = element_blank()) + theme(legend.position = "top")+
  scale_y_continuous(expand = expansion(add = 0, mult = 0))
gg_boxplot_simpson

gg_boxplot_evenness <- ggplot(long_evenness, aes(x = Control_or_treatment, y = Evenness, fill = Taxon)) +
  geom_boxplot() +scale_fill_brewer(palette = "Dark2") +
  theme_classic() + labs(x = "Control or Treatment", y = "Evenness") +
  theme(axis.title.x = element_blank()) + theme(legend.position = "top")+
  scale_y_continuous(expand = expansion(add = 0, mult = 0))
gg_boxplot_evenness

# Display the grouped box plots
combined_indicies<- ggarrange(gg_boxplot_cover, gg_boxplot_richness, gg_boxplot_evenness, gg_boxplot_shannon, gg_boxplot_simpson,
          common.legend = TRUE, legend = "top")
combined_indicies

ggsave("Output figs and tables/combined_indicies_plot.png", plot = combined_indicies, width = 8, height = 6, dpi = 300)

Step 4 NMDS

– all species NMDS – just lichen nmds

– just below nest for both too

First we need to create environmental and community df with only sucessful traps

# Create the trap_success column
plot_enviro <- plot_enviro %>%
  mutate(trap_success = ifelse(NH4 == "ND" | NO32 == "ND", "failure", "success"))

# Filter plot_enviro to select only "success" rows
plot_enviro_success <- plot_enviro %>%
  filter(trap_success == "success")

# Extract Quad_Identifier values
matching_quad_ids <- plot_enviro_success$Quad_Identifier

# Filter rows in all.matrix based on matching Quad_Identifier values
all.matrix_filtered <- all.matrix[rownames(all.matrix) %in% matching_quad_ids, ]

# Filter rows in all.matrix based on matching Quad_Identifier values
lichen.matrix_filtered <- lichen.matrix[rownames(lichen.matrix) %in% matching_quad_ids, ]


# Filter rows in plot_enviro_success based on matching Quad_Identifier values
plot_enviro_success_filtered <- plot_enviro_success[plot_enviro_success$Quad_Identifier %in% rownames(all.matrix_filtered), ]
# bring in all community data
m_com  = as.matrix(all.matrix_filtered)


# Filter plot_enviro_success to select only relevant environmental columns
env <- plot_enviro_success_filtered[, c("NH4", "NO32")]
env$NO32 <- as.numeric(env$NO32)
env$NH4 <- as.numeric(env$NH4)
colnames(env)[2] <- "NO3"

# Ensure 'env' and 'en2nmds' have the same row dimensions
matching_quad_ids <- plot_enviro_success_filtered$Quad_Identifier
m_com_filtered <- m_com[rownames(m_com) %in% matching_quad_ids, ]

# Perform NMDS with the filtered community data
set.seed(123)
en2nmds = metaMDS(m_com_filtered, k = 2, distance = "bray")
## Square root transformation
## Wisconsin double standardization
## Run 0 stress 0.1321824 
## Run 1 stress 0.1357896 
## Run 2 stress 0.1334558 
## Run 3 stress 0.1332019 
## Run 4 stress 0.1416852 
## Run 5 stress 0.1321777 
## ... New best solution
## ... Procrustes: rmse 0.002254197  max resid 0.01540606 
## Run 6 stress 0.1330466 
## Run 7 stress 0.1336268 
## Run 8 stress 0.1335211 
## Run 9 stress 0.1335211 
## Run 10 stress 0.1329827 
## Run 11 stress 0.1324714 
## ... Procrustes: rmse 0.02607983  max resid 0.11947 
## Run 12 stress 0.1438811 
## Run 13 stress 0.1332012 
## Run 14 stress 0.1332013 
## Run 15 stress 0.1349237 
## Run 16 stress 0.1323636 
## ... Procrustes: rmse 0.02821341  max resid 0.1183386 
## Run 17 stress 0.1323634 
## ... Procrustes: rmse 0.028167  max resid 0.1183997 
## Run 18 stress 0.1449351 
## Run 19 stress 0.1321781 
## ... Procrustes: rmse 0.0002133179  max resid 0.0007503009 
## ... Similar to previous best
## Run 20 stress 0.1347276 
## *** Best solution repeated 1 times
# Perform environmental fit with matching dimensions
en2 = envfit(en2nmds, env, permutations = 999, na.rm = TRUE)
en2
## 
## ***VECTORS
## 
##         NMDS1     NMDS2     r2 Pr(>r)
## NH4 -0.001176  1.000000 0.0346  0.335
## NO3  0.210500 -0.977590 0.0445  0.205
## Permutation: free
## Number of permutations: 999

LICHENS

# bring in all community data
m_com  = as.matrix(lichen.matrix_filtered)


# Filter plot_enviro_success to select only relevant environmental columns
env <- plot_enviro_success_filtered[, c("NH4", "NO32")]
env$NO32 <- as.numeric(env$NO32)
env$NH4 <- as.numeric(env$NH4)
colnames(env)[2] <- "NO3"

# Ensure 'env' and 'en2nmds' have the same row dimensions
matching_quad_ids <- plot_enviro_success$Quad_Identifier
m_com_filtered <- m_com[rownames(m_com) %in% matching_quad_ids, ]

# Perform NMDS with the filtered community data
set.seed(123)
en2nmds = metaMDS(m_com_filtered, k = 2, distance = "bray")
## Square root transformation
## Wisconsin double standardization
## Run 0 stress 0.1257683 
## Run 1 stress 0.1264156 
## Run 2 stress 0.1269873 
## Run 3 stress 0.1270916 
## Run 4 stress 0.1270123 
## Run 5 stress 0.1257637 
## ... New best solution
## ... Procrustes: rmse 0.00436288  max resid 0.02154982 
## Run 6 stress 0.1257869 
## ... Procrustes: rmse 0.00454245  max resid 0.02591011 
## Run 7 stress 0.1316656 
## Run 8 stress 0.12677 
## Run 9 stress 0.1267703 
## Run 10 stress 0.126383 
## Run 11 stress 0.1265093 
## Run 12 stress 0.1400032 
## Run 13 stress 0.1267407 
## Run 14 stress 0.1271221 
## Run 15 stress 0.1267665 
## Run 16 stress 0.1264158 
## Run 17 stress 0.1259911 
## ... Procrustes: rmse 0.02445718  max resid 0.1068502 
## Run 18 stress 0.1264892 
## Run 19 stress 0.1257808 
## ... Procrustes: rmse 0.003026036  max resid 0.01994196 
## Run 20 stress 0.1267671 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##      1: no. of iterations >= maxit
##     19: stress ratio > sratmax
# Perform environmental fit with matching dimensions
en2 = envfit(en2nmds, env, permutations = 999, na.rm = TRUE)
en2
## 
## ***VECTORS
## 
##        NMDS1    NMDS2     r2 Pr(>r)
## NH4  0.32770  0.94478 0.0406  0.267
## NO3 -0.08341 -0.99652 0.0414  0.236
## Permutation: free
## Number of permutations: 999

only below nests

# Create the trap_success column
plot_enviro <- plot_enviro %>%
  mutate(trap_success = ifelse(NH4 == "ND" | NO32 == "ND", "failure", "success"))

# Filter plot_enviro to select only "success" rows
plot_enviro_success <- plot_enviro %>%
  filter(trap_success == "success")

# Add an additional filter layer for Nest_Position
plot_enviro_success_belowNest <- plot_enviro_success %>%
  filter(Nest_Position == "below_nest")

# Extract Quad_Identifier values
matching_quad_ids <- plot_enviro_success_belowNest$Quad_Identifier

# Filter rows in all.matrix based on matching Quad_Identifier values
all.matrix_filtered_belownest <- all.matrix[rownames(all.matrix) %in% matching_quad_ids, ]

# Filter rows in lichen.matrix based on matching Quad_Identifier values
lichen.matrix_filtered_belownest <- lichen.matrix[rownames(lichen.matrix) %in% matching_quad_ids, ]



# Filter rows in plot_enviro_success based on matching Quad_Identifier values
plot_enviro_success_belowNest_filtered <- plot_enviro_success_belowNest[plot_enviro_success_belowNest$Quad_Identifier %in% rownames(all.matrix_filtered), ]

all - below nest only

# bring in all community data
m_com  = as.matrix(all.matrix_filtered_belownest)


# Filter plot_enviro_success to select only relevant environmental columns
env <- plot_enviro_success_belowNest_filtered[, c("NH4", "NO32", "Site_name")]
env$NO32 <- as.numeric(env$NO32)
env$NH4 <- as.numeric(env$NH4)
colnames(env)[2] <- "NO3"

# Ensure 'env' and 'en2nmds' have the same row dimensions
matching_quad_ids <- plot_enviro_success_belowNest_filtered$Quad_Identifier
m_com_filtered <- m_com[rownames(m_com) %in% matching_quad_ids, ]

# Perform NMDS with the filtered community data
set.seed(123)
en2nmds = metaMDS(m_com_filtered, k = 2, distance = "bray")
## Square root transformation
## Wisconsin double standardization
## Run 0 stress 0.1347773 
## Run 1 stress 0.1388983 
## Run 2 stress 0.1348449 
## ... Procrustes: rmse 0.003359053  max resid 0.02018058 
## Run 3 stress 0.1368306 
## Run 4 stress 0.1383077 
## Run 5 stress 0.133843 
## ... New best solution
## ... Procrustes: rmse 0.06127205  max resid 0.1607576 
## Run 6 stress 0.1352497 
## Run 7 stress 0.1354224 
## Run 8 stress 0.1360223 
## Run 9 stress 0.1337889 
## ... New best solution
## ... Procrustes: rmse 0.007908966  max resid 0.03208174 
## Run 10 stress 0.1352666 
## Run 11 stress 0.1381898 
## Run 12 stress 0.1338766 
## ... Procrustes: rmse 0.006117337  max resid 0.03217183 
## Run 13 stress 0.1359186 
## Run 14 stress 0.1347772 
## Run 15 stress 0.1471259 
## Run 16 stress 0.1430228 
## Run 17 stress 0.1386782 
## Run 18 stress 0.1465816 
## Run 19 stress 0.1620193 
## Run 20 stress 0.1353333 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##      2: no. of iterations >= maxit
##     17: stress ratio > sratmax
##      1: scale factor of the gradient < sfgrmin
# Perform environmental fit with matching dimensions
en2 = envfit(en2nmds, env, permutations = 999, na.rm = TRUE)
en2
## 
## ***VECTORS
## 
##        NMDS1    NMDS2     r2 Pr(>r)   
## NH4 -0.02163  0.99977 0.1723  0.004 **
## NO3 -0.53136  0.84715 0.1103  0.028 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Permutation: free
## Number of permutations: 999
## 
## ***FACTORS:
## 
## Centroids:
##                NMDS1   NMDS2
## Site_nameBLC -0.5479 -0.1667
## Site_nameBUZ -0.0505  0.5615
## Site_nameCHM  0.3181 -0.2065
## Site_nameGOA -1.1617 -0.0764
## Site_nameNRS  0.9503 -0.8695
## Site_namePIL -0.3692  1.2769
## Site_nameSNA  1.0329  0.4863
## Site_nameTAB  0.3245 -0.6215
## 
## Goodness of fit:
##               r2 Pr(>r)    
## Site_name 0.8061  0.001 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Permutation: free
## Number of permutations: 999
# summary into to save for manuscipt
#Lichen only NMDS 
# vectors nh4 r2 0.17229  , p 0.004 **
# vectors no3 r2 0.11029  , p 0.028  **
# goodness of fit for factor (site)
# site r2 0.80606, p 0.001 ***



# Extract NMDS site scores
e2.data.scores <- as.data.frame(scores(en2nmds$points))
e2.data.scores$Region = plot_enviro_success_belowNest_filtered$Site_name
e2.data.scores$Trt = plot_enviro_success_belowNest_filtered$Control_or_treatment
e2.data.scores$NH4 = plot_enviro_success_belowNest_filtered$NH4
e2.data.scores$NO32 = plot_enviro_success_belowNest_filtered$NO32

en_coord_cont = as.data.frame(scores(en2, "vectors")) * ordiArrowMul(en2)
en_coord_cat = as.data.frame(scores(en2, "factors")) * ordiArrowMul(en2)





theme_set(
  theme(axis.title = element_text(size = 10, face = "bold", colour = "black"),
        panel.background = element_blank(), 
        panel.border = element_rect(fill = NA, colour = "black"), 
        axis.ticks = element_blank(), 
        axis.text = element_blank(), 
        legend.key = element_blank(), 
        legend.title = element_text(size = 10, face = "bold", colour = "black"),
        legend.text = element_text(size = 9, colour = "black")))


####### figures
# Define a color palette using RColorBrewer
dark2_palette <- brewer.pal(8, "Dark2")

# Scatter plot colored by Site using the Dark2 palette
alltaxa_siteNMDS <- ggplot(data = e2.data.scores, aes(x = MDS1, y = MDS2, 
                                                     color = Region)) + 
  geom_point(size = 2, alpha = 0.6) +
  scale_color_manual(values = dark2_palette) +  # Apply the Dark2 color palette
  labs(color = "Site")+
    labs(title = "All taxa - below nest")
alltaxa_siteNMDS

# Scatter plot colored by Site using the Dark2 palette
alltaxa_siteNMDS_withTrt <- 
  ggplot(data = e2.data.scores, aes(x = MDS1, y = MDS2,  color = Region, shape=Trt)) + 
  geom_point(size = 2, alpha = 0.6) +
  scale_color_manual(values = dark2_palette) +  # Apply the Dark2 color palette
  labs(color = "Site", shape = "Guano")+
    labs(title = "All taxa - below nest")
alltaxa_siteNMDS_withTrt

# # Scatter plot with ellipses and colored by Site using the Dark2 palette
# lichen_siteNMDS_ellipse <- ggplot(data = e2.data.scores, aes(x = MDS1, y = MDS2, color = Region)) + 
#   geom_point(size = 2, alpha = 0.6) +
#   stat_ellipse(size = 1.5) +
#   scale_color_manual(values = dark2_palette) +  # Apply the Dark2 color palette
#   labs(color = "Site")
# lichen_siteNMDS_ellipse

# Combine both scatter plots into one
alltaxa_siteNMDS_withN <- alltaxa_siteNMDS + 
  geom_segment(aes(x = 0, y = 0, xend = NMDS1, yend = NMDS2), 
               data = en_coord_cont, size = 1, alpha = 0.7, color = "black") +
  geom_text(data = en_coord_cont, aes(x = NMDS1, y = NMDS2), 
            check_overlap = TRUE, color = "black", 
            fontface = "bold", label = row.names(en_coord_cont), vjust = "outward") +
  labs(title = "All Taxa - below nest")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
alltaxa_siteNMDS_withN

# Combine the ggplot figures into a single plot
# remove fig title
alltaxa_nmds_combined_plot <- ggarrange(
  alltaxa_siteNMDS + labs(title = NULL),
  alltaxa_siteNMDS_withN + labs(title = NULL),
  ncol = 2,
  common.legend = TRUE,
  legend = "top"
)

# Display the combined plot
alltaxa_nmds_combined_plot

ggsave("Output figs and tables/alltaxa_nmds_combined_plot.png", plot = alltaxa_nmds_combined_plot, width = 8, height = 4, dpi = 300)

lichens - below nest only LICHENS

# bring in all community data
m_com  = as.matrix(lichen.matrix_filtered_belownest)

# Filter plot_enviro_success to select only relevant environmental columns
env <- plot_enviro_success_belowNest_filtered[, c("NH4", "NO32", "Site_name")]
env$NO32 <- as.numeric(env$NO32)
env$NH4 <- as.numeric(env$NH4)
colnames(env)[2] <- "NO3"

# Ensure 'env' and 'en2nmds' have the same row dimensions
matching_quad_ids <- plot_enviro_success_belowNest_filtered$Quad_Identifier
m_com_filtered <- m_com[rownames(m_com) %in% matching_quad_ids, ]

# Perform NMDS with the filtered community data
set.seed(123)
en2nmds = metaMDS(m_com_filtered, k = 2, distance = "bray")
## Square root transformation
## Wisconsin double standardization
## Run 0 stress 0.1283553 
## Run 1 stress 0.1403796 
## Run 2 stress 0.131287 
## Run 3 stress 0.1283551 
## ... New best solution
## ... Procrustes: rmse 0.0001477325  max resid 0.0008964656 
## ... Similar to previous best
## Run 4 stress 0.1326637 
## Run 5 stress 0.1422671 
## Run 6 stress 0.129115 
## Run 7 stress 0.1309624 
## Run 8 stress 0.1280076 
## ... New best solution
## ... Procrustes: rmse 0.05011856  max resid 0.1353979 
## Run 9 stress 0.1279845 
## ... New best solution
## ... Procrustes: rmse 0.00439645  max resid 0.02122266 
## Run 10 stress 0.129054 
## Run 11 stress 0.1314952 
## Run 12 stress 0.128019 
## ... Procrustes: rmse 0.004738878  max resid 0.0212096 
## Run 13 stress 0.1333126 
## Run 14 stress 0.1283552 
## ... Procrustes: rmse 0.04773489  max resid 0.1365068 
## Run 15 stress 0.1280065 
## ... Procrustes: rmse 0.003994851  max resid 0.02038142 
## Run 16 stress 0.1396275 
## Run 17 stress 0.1332443 
## Run 18 stress 0.1365667 
## Run 19 stress 0.1577363 
## Run 20 stress 0.1286015 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##      3: no. of iterations >= maxit
##     17: stress ratio > sratmax
# Perform environmental fit with matching dimensions
en2 = envfit(en2nmds, env, permutations = 999, na.rm = TRUE)
en2
## 
## ***VECTORS
## 
##        NMDS1    NMDS2     r2 Pr(>r)   
## NH4  0.03738  0.99930 0.2048  0.002 **
## NO3 -0.60392  0.79705 0.1082  0.032 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Permutation: free
## Number of permutations: 999
## 
## ***FACTORS:
## 
## Centroids:
##                NMDS1   NMDS2
## Site_nameBLC -0.6255  0.0169
## Site_nameBUZ -0.0324  0.6821
## Site_nameCHM  0.3203 -0.3264
## Site_nameGOA -1.3516 -0.2257
## Site_nameNRS  0.9306 -1.0707
## Site_namePIL -0.3776  1.5288
## Site_nameSNA  1.2587  0.4768
## Site_nameTAB  0.3690 -0.6609
## 
## Goodness of fit:
##               r2 Pr(>r)    
## Site_name 0.8194  0.001 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Permutation: free
## Number of permutations: 999
# summary into to save for manuscipt
#Lichen only NMDS 
# vectors nh4 r2 0.17229, p 0.004 **
# vectors no3 r2 0.11029  , p 0.028  **
# goodness of fit for factor (site)
# site r2 0.80606, p 0.001 ***


# Extract NMDS site scores
e2.data.scores <- as.data.frame(scores(en2nmds$points))
e2.data.scores$Region = plot_enviro_success_belowNest_filtered$Site_name
e2.data.scores$Trt = plot_enviro_success_belowNest_filtered$Control_or_treatment
e2.data.scores$NH4 = plot_enviro_success_belowNest_filtered$NH4
e2.data.scores$NO32 = plot_enviro_success_belowNest_filtered$NO32

en_coord_cont = as.data.frame(scores(en2, "vectors")) * ordiArrowMul(en2)
en_coord_cat = as.data.frame(scores(en2, "factors")) * ordiArrowMul(en2)



####### figures
# Define a color palette using RColorBrewer
dark2_palette <- brewer.pal(8, "Dark2")

# Scatter plot colored by Site using the Dark2 palette
lichen_siteNMDS <- ggplot(data = e2.data.scores, aes(x = MDS1, y = MDS2, 
                                                     color = Region)) + 
  geom_point(size = 2, alpha = 0.6) +
  scale_color_manual(values = dark2_palette) +  # Apply the Dark2 color palette
  labs(color = "Site")+
    labs(title = "Only lichens - below nest")
lichen_siteNMDS

# Scatter plot colored by Site using the Dark2 palette
lichen_siteNMDS_withTrt <- 
  ggplot(data = e2.data.scores, aes(x = MDS1, y = MDS2,  color = Region, shape=Trt)) + 
  geom_point(size = 2, alpha = 0.6) +
  scale_color_manual(values = dark2_palette) +  # Apply the Dark2 color palette
  labs(color = "Site", shape = "Guano")+
    labs(title = "Only lichens - below nest")
lichen_siteNMDS_withTrt

# # Scatter plot with ellipses and colored by Site using the Dark2 palette
# lichen_siteNMDS_ellipse <- ggplot(data = e2.data.scores, aes(x = MDS1, y = MDS2, color = Region)) + 
#   geom_point(size = 2, alpha = 0.6) +
#   stat_ellipse(size = 1.5) +
#   scale_color_manual(values = dark2_palette) +  # Apply the Dark2 color palette
#   labs(color = "Site")
# lichen_siteNMDS_ellipse

# Combine both scatter plots into one
lichen_siteNMDS_withN <- lichen_siteNMDS + 
  geom_segment(aes(x = 0, y = 0, xend = NMDS1, yend = NMDS2), 
               data = en_coord_cont, size = 1, alpha = 0.7, color = "black") +
  geom_text(data = en_coord_cont, aes(x = NMDS1, y = NMDS2), 
            check_overlap = TRUE, color = "black", 
            fontface = "bold", label = row.names(en_coord_cont), vjust = "outward") +
  labs(title = "Only lichens - below nest")
lichen_siteNMDS_withN

# Combine the ggplot figures into a single plot
# remove fig title
lichen_nmds_combined_plot <- ggarrange(
  lichen_siteNMDS + labs(title = NULL),
  lichen_siteNMDS_withN + labs(title = NULL),
  ncol = 2,
  common.legend = TRUE,
  legend = "top"
)

# Display the combined plot
lichen_nmds_combined_plot

ggsave("Output figs and tables/lichen_nmds_combined_plot.png", plot = lichen_nmds_combined_plot, width = 8, height = 4, dpi = 300)

# 
# 
# lichen_nmds_combined_plot <- ggarrange(
#   lichen_siteNMDS + labs(title = NULL),
#   lichen_siteNMDS_withTrt + labs(title = NULL),
#   lichen_siteNMDS_withN + labs(title = NULL),
#   ncol = 3,
#   common.legend = TRUE,
#   legend = "bottom"
# )

Step 5 indicator species and rank abundance species lists

#recall the community matricies 
head(all.matrix)
##                      Ssp122 Ssp150 Ssp117 Ssp119 Ssp138 Ssp53 Ssp81 Ssp96
## PIL_Nest_22_44_R        0.1    0.1    0.0      0    0.0   0.0     0     0
## PIL_Nest_23_46_R        0.2    0.1    1.0      0    0.0   0.0     0     0
## PIL_Nest_22_43_L        1.0    2.0    0.0      1    0.5   0.0     0     0
## NRS_Nest_63_126_R       0.0    0.0    0.0      0    0.0   0.5     2     3
## PIL_Nest_23_45_L        2.4    1.0    1.7      0    0.0   0.0     0     0
## CHM_Control_56_112_L    0.0    0.0    0.0      2    0.0   0.0     0     0
##                      Ssp131 Ssp20 Ssp139 Ssp12 Ssp30 Ssp162 Ssp31 Ssp49 Ssp85
## PIL_Nest_22_44_R        0.0   0.0      0     0     0      0     0     0     0
## PIL_Nest_23_46_R        0.0   0.0      0     0     0      0     0     0     0
## PIL_Nest_22_43_L        0.0   0.0      0     0     0      0     0     0     0
## NRS_Nest_63_126_R       0.5   0.0      0     0     0      0     0     0     0
## PIL_Nest_23_45_L        0.0   0.1      3     0     0      0     0     0     0
## CHM_Control_56_112_L    0.0   0.0      0     4     1      2     0     0     0
##                      Ssp114 Ssp116 Ssp104 Ssp5 Ssp100 Ssp2 Ssp29 Ssp4 Ssp159
## PIL_Nest_22_44_R          0      0      0    0      0    0     0    0      0
## PIL_Nest_23_46_R          0      0      0    0      0    0     0    0      0
## PIL_Nest_22_43_L          0      0      0    0      0    0     0    0      0
## NRS_Nest_63_126_R         0      0      0    0      0    0     0    0      0
## PIL_Nest_23_45_L          0      0      0    0      0    0     0    0      0
## CHM_Control_56_112_L      0      0      0    0      0    0     0    0      0
##                      Ssp10 Ssp61 Ssp62 Ssp65 Ssp69 Ssp82 Ssp118 Ssp146 Ssp171
## PIL_Nest_22_44_R         0     0     0     0     0     0      0      0      0
## PIL_Nest_23_46_R         0     0     0     0     0     0      0      0      0
## PIL_Nest_22_43_L         0     0     0     0     0     0      0      0      0
## NRS_Nest_63_126_R        0     0     0     0     0     0      0      0      0
## PIL_Nest_23_45_L         0     0     0     0     0     0      0      0      0
## CHM_Control_56_112_L     0     0     0     0     0     0      0      0      0
##                      Ssp156 Ssp126 Ssp9 Ssp13 Ssp16 Ssp26 Ssp103 Ssp136 Ssp143
## PIL_Nest_22_44_R          0      0    0     0     0     0      0      0      0
## PIL_Nest_23_46_R          0      0    0     0     0     0      0      0      0
## PIL_Nest_22_43_L          0      0    0     0     0     0      0      0      0
## NRS_Nest_63_126_R         0      0    0     0     0     0      0      0      0
## PIL_Nest_23_45_L          0      0    0     0     0     0      0      0      0
## CHM_Control_56_112_L      0      0    0     0     0     0      0      0      0
##                      Ssp169 Ssp177 Ssp19 Ssp44 Ssp1 Ssp124 Ssp163 Ssp15 Ssp40
## PIL_Nest_22_44_R          0      0     0     0    0      0      0     0     0
## PIL_Nest_23_46_R          0      0     0     0    0      0      0     0     0
## PIL_Nest_22_43_L          0      0     0     0    0      0      0     0     0
## NRS_Nest_63_126_R         0      0     0     0    0      0      0     0     0
## PIL_Nest_23_45_L          0      0     0     0    0      0      0     0     0
## CHM_Control_56_112_L      0      0     0     0    0      0      0     0     0
##                      Ssp80 Ssp127 Ssp157 Ssp14 Ssp21 Ssp56 Ssp95 Ssp106 Ssp128
## PIL_Nest_22_44_R         0      0      0     0     0     0     0      0      0
## PIL_Nest_23_46_R         0      0      0     0     0     0     0      0      0
## PIL_Nest_22_43_L         0      0      0     0     0     0     0      0      0
## NRS_Nest_63_126_R        0      0      0     0     0     0     0      0      0
## PIL_Nest_23_45_L         0      0      0     0     0     0     0      0      0
## CHM_Control_56_112_L     0      0      0     0     0     0     0      0      0
##                      Ssp155 Ssp107 Ssp111 Ssp125 Ssp141 Ssp187 Ssp160 Ssp25
## PIL_Nest_22_44_R          0      0      0      0      0      0      0     0
## PIL_Nest_23_46_R          0      0      0      0      0      0      0     0
## PIL_Nest_22_43_L          0      0      0      0      0      0      0     0
## NRS_Nest_63_126_R         0      0      0      0      0      0      0     0
## PIL_Nest_23_45_L          0      0      0      0      0      0      0     0
## CHM_Control_56_112_L      0      0      0      0      0      0      0     0
##                      Ssp168 Ssp181 Ssp129 Ssp149 Ssp3 Ssp18 Ssp32 Ssp66 Ssp158
## PIL_Nest_22_44_R          0      0      0      0    0     0     0     0      0
## PIL_Nest_23_46_R          0      0      0      0    0     0     0     0      0
## PIL_Nest_22_43_L          0      0      0      0    0     0     0     0      0
## NRS_Nest_63_126_R         0      0      0      0    0     0     0     0      0
## PIL_Nest_23_45_L          0      0      0      0    0     0     0     0      0
## CHM_Control_56_112_L      0      0      0      0    0     0     0     0      0
##                      Ssp8 Ssp186 Ssp27 Ssp130 Ssp55 Ssp35 Ssp38 Ssp94 Ssp151
## PIL_Nest_22_44_R        0      0     0      0     0     0     0     0      0
## PIL_Nest_23_46_R        0      0     0      0     0     0     0     0      0
## PIL_Nest_22_43_L        0      0     0      0     0     0     0     0      0
## NRS_Nest_63_126_R       0      0     0      0     0     0     0     0      0
## PIL_Nest_23_45_L        0      0     0      0     0     0     0     0      0
## CHM_Control_56_112_L    0      0     0      0     0     0     0     0      0
##                      Ssp54 Ssp88 Ssp98 Ssp112 Ssp72 Ssp73 Ssp77 Ssp57 Ssp120
## PIL_Nest_22_44_R         0     0     0      0     0     0     0     0      0
## PIL_Nest_23_46_R         0     0     0      0     0     0     0     0      0
## PIL_Nest_22_43_L         0     0     0      0     0     0     0     0      0
## NRS_Nest_63_126_R        0     0     0      0     0     0     0     0      0
## PIL_Nest_23_45_L         0     0     0      0     0     0     0     0      0
## CHM_Control_56_112_L     0     0     0      0     0     0     0     0      0
##                      Ssp39 Ssp93 Ssp132 Ssp154 Ssp113 Ssp78 Ssp179 Ssp17 Ssp71
## PIL_Nest_22_44_R         0     0      0      0      0     0      0     0     0
## PIL_Nest_23_46_R         0     0      0      0      0     0      0     0     0
## PIL_Nest_22_43_L         0     0      0      0      0     0      0     0     0
## NRS_Nest_63_126_R        0     0      0      0      0     0      0     0     0
## PIL_Nest_23_45_L         0     0      0      0      0     0      0     0     0
## CHM_Control_56_112_L     0     0      0      0      0     0      0     0     0
##                      Ssp90 Ssp108 Ssp133 Ssp144 Ssp172 Ssp191 Ssp43 Ssp45
## PIL_Nest_22_44_R         0      0      0      0      0      0     0     0
## PIL_Nest_23_46_R         0      0      0      0      0      0     0     0
## PIL_Nest_22_43_L         0      0      0      0      0      0     0     0
## NRS_Nest_63_126_R        0      0      0      0      0      0     0     0
## PIL_Nest_23_45_L         0      0      0      0      0      0     0     0
## CHM_Control_56_112_L     0      0      0      0      0      0     0     0
##                      Ssp115 Ssp67 Ssp101 Ssp121 Ssp76 Ssp109 Ssp167 Ssp28 Ssp48
## PIL_Nest_22_44_R          0     0      0      0     0      0      0     0     0
## PIL_Nest_23_46_R          0     0      0      0     0      0      0     0     0
## PIL_Nest_22_43_L          0     0      0      0     0      0      0     0     0
## NRS_Nest_63_126_R         0     0      0      0     0      0      0     0     0
## PIL_Nest_23_45_L          0     0      0      0     0      0      0     0     0
## CHM_Control_56_112_L      0     0      0      0     0      0      0     0     0
##                      Ssp68 Ssp142 Ssp123 Ssp148 Ssp182 Ssp178 Ssp24 Ssp23 Ssp83
## PIL_Nest_22_44_R         0      0      0      0      0      0     0     0     0
## PIL_Nest_23_46_R         0      0      0      0      0      0     0     0     0
## PIL_Nest_22_43_L         0      0      0      0      0      0     0     0     0
## NRS_Nest_63_126_R        0      0      0      0      0      0     0     0     0
## PIL_Nest_23_45_L         0      0      0      0      0      0     0     0     0
## CHM_Control_56_112_L     0      0      0      0      0      0     0     0     0
##                      Ssp110 Ssp189 Ssp64 Ssp6 Ssp7 Ssp137 Ssp42 Ssp50 Ssp46
## PIL_Nest_22_44_R          0      0     0    0    0      0     0     0     0
## PIL_Nest_23_46_R          0      0     0    0    0      0     0     0     0
## PIL_Nest_22_43_L          0      0     0    0    0      0     0     0     0
## NRS_Nest_63_126_R         0      0     0    0    0      0     0     0     0
## PIL_Nest_23_45_L          0      0     0    0    0      0     0     0     0
## CHM_Control_56_112_L      0      0     0    0    0      0     0     0     0
##                      Ssp188 Ssp164 Ssp192 Ssp47 Ssp99
## PIL_Nest_22_44_R          0      0      0     0     0
## PIL_Nest_23_46_R          0      0      0     0     0
## PIL_Nest_22_43_L          0      0      0     0     0
## NRS_Nest_63_126_R         0      0      0     0     0
## PIL_Nest_23_45_L          0      0      0     0     0
## CHM_Control_56_112_L      0      0      0     0     0
head(cover)
##   Transect_name T_Pair_identifer Plot_Identifier   Quad_Identifier
## 1      PIL_Nest         PIL_Face     PIL_Nest_22  PIL_Nest_22_44_R
## 2      PIL_Nest         PIL_Face     PIL_Nest_23  PIL_Nest_23_46_R
## 3      GOA_Nest         GOA_Face     GOA_Nest_76 GOA_Nest_76_151_L
## 4      PIL_Nest         PIL_Face     PIL_Nest_22  PIL_Nest_22_43_L
## 5      NRS_Nest         NRS_Face     NRS_Nest_63 NRS_Nest_63_126_R
## 6      PIL_Nest         PIL_Face     PIL_Nest_23  PIL_Nest_23_45_L
##   Transect.Code Site plot clfpos L3R4 NP1P2 BryoC LichC VascC Heter SL  AS
## 1            44    3   22      1    4     2     0   0.2     0     3 90 298
## 2            46    3   23      2    4     2     0   1.3     0     2 89 250
## 3           151    8   76      1    3     2     0   0.0     0     4 95 356
## 4            43    3   22      1    3     2     0   4.5     0     3 98 298
## 5           126    7   63      2    4     2     2  10.0     3     3 95 253
## 6            45    3   23      2    3     2     0   8.2     0     2 91 310
##        wpn      wpw transdist         NH4        NO31        NO32 ALL.SUM Ssp1
## 1 36.34156 80.48754        10          ND          ND          ND     0.2    0
## 2 36.34156 80.48754        10          ND          ND          ND     1.3    0
## 3 36.29108 81.78748         5 1.38278E-06 3.26746E-07 4.54435E-07     4.0    0
## 4 36.34156 80.48754        10          ND          ND          ND     4.5    0
## 5 36.45817 81.33304         5          ND  1.6378E-05 7.28661E-06     6.0    0
## 6 36.34156 80.48754        10          ND          ND          ND     8.2    0
##   Ssp2 Ssp3 Ssp4 Ssp5 Ssp6 Ssp7 Ssp8 Ssp9 Ssp10 Ssp11 Ssp12 Ssp13 Ssp14 Ssp15
## 1    0    0    0    0    0    0    0    0     0     0     0     0     0     0
## 2    0    0    0    0    0    0    0    0     0     0     0     0     0     0
## 3    0    0    0    0    0    0    0    0     0     0     0     0     0     0
## 4    0    0    0    0    0    0    0    0     0     0     0     0     0     0
## 5    0    0    0    0    0    0    0    0     0     0     0     0     0     0
## 6    0    0    0    0    0    0    0    0     0     0     0     0     0     0
##   Ssp16 Ssp17 Ssp18 Ssp19 Ssp20 Ssp21 Ssp22 Ssp23 Ssp24 Ssp25 Ssp26 Ssp27 Ssp28
## 1     0     0     0     0   0.0     0     0     0     0     0     0     0     0
## 2     0     0     0     0   0.0     0     0     0     0     0     0     0     0
## 3     0     0     0     0   0.0     0     0     0     0     0     0     0     0
## 4     0     0     0     0   0.0     0     0     0     0     0     0     0     0
## 5     0     0     0     0   0.0     0     0     0     0     0     0     0     0
## 6     0     0     0     0   0.1     0     0     0     0     0     0     0     0
##   Ssp29 Ssp30 Ssp31 Ssp32 Ssp33 Ssp34 Ssp35 Ssp36 Ssp37 Ssp38 Ssp39 Ssp40 Ssp41
## 1     0     0     0     0     0     0     0     0     0     0     0     0     0
## 2     0     0     0     0     0     0     0     0     0     0     0     0     0
## 3     0     0     0     0     0     0     0     0     0     0     0     0     0
## 4     0     0     0     0     0     0     0     0     0     0     0     0     0
## 5     0     0     0     0     0     0     0     0     0     0     0     0     0
## 6     0     0     0     0     0     0     0     0     0     0     0     0     0
##   Ssp42 Ssp43 Ssp44 Ssp45 Ssp46 Ssp47 Ssp48 Ssp49 Ssp50 Ssp51 Ssp52 Ssp53 Ssp54
## 1     0     0     0     0     0     0     0     0     0     0     0   0.0     0
## 2     0     0     0     0     0     0     0     0     0     0     0   0.0     0
## 3     0     0     0     0     0     0     0     0     0     0     0   0.0     0
## 4     0     0     0     0     0     0     0     0     0     0     0   0.0     0
## 5     0     0     0     0     0     0     0     0     0     0     0   0.5     0
## 6     0     0     0     0     0     0     0     0     0     0     0   0.0     0
##   Ssp55 Ssp56 Ssp57 Ssp58 Ssp59 Ssp60 Ssp61 Ssp62 Ssp63 Ssp64 Ssp65 Ssp66 Ssp67
## 1     0     0     0     0     0     0     0     0     0     0     0     0     0
## 2     0     0     0     0     0     0     0     0     0     0     0     0     0
## 3     0     0     0     0     0     0     0     0     0     0     0     0     0
## 4     0     0     0     0     0     0     0     0     0     0     0     0     0
## 5     0     0     0     0     0     0     0     0     0     0     0     0     0
## 6     0     0     0     0     0     0     0     0     0     0     0     0     0
##   Ssp68 Ssp69 Ssp70 Ssp71 Ssp72 Ssp73 Ssp74 Ssp75 Ssp76 Ssp77 Ssp78 Ssp79 Ssp80
## 1     0     0     0     0     0     0     0     0     0     0     0     0     0
## 2     0     0     0     0     0     0     0     0     0     0     0     0     0
## 3     0     0     0     0     0     0     0     0     0     0     0     0     0
## 4     0     0     0     0     0     0     0     0     0     0     0     0     0
## 5     0     0     0     0     0     0     0     0     0     0     0     0     0
## 6     0     0     0     0     0     0     0     0     0     0     0     0     0
##   Ssp81 Ssp82 Ssp83 Ssp84 Ssp85 Ssp86 Ssp87 Ssp88 Ssp89 Ssp90 Ssp91 Ssp92 Ssp93
## 1     0     0     0     0     0     0     0     0     0     0     0     0     0
## 2     0     0     0     0     0     0     0     0     0     0     0     0     0
## 3     0     0     0     0     0     0     0     0     0     0     0     4     0
## 4     0     0     0     0     0     0     0     0     0     0     0     0     0
## 5     2     0     0     0     0     0     0     0     0     0     0     0     0
## 6     0     0     0     0     0     0     0     0     0     0     0     0     0
##   Ssp94 Ssp95 Ssp96 Ssp97 Ssp98 Ssp99 Ssp100 Ssp101 Ssp102 Ssp103 Ssp104 Ssp105
## 1     0     0     0     0     0     0      0      0      0      0      0      0
## 2     0     0     0     0     0     0      0      0      0      0      0      0
## 3     0     0     0     0     0     0      0      0      0      0      0      0
## 4     0     0     0     0     0     0      0      0      0      0      0      0
## 5     0     0     3     0     0     0      0      0      0      0      0      0
## 6     0     0     0     0     0     0      0      0      0      0      0      0
##   Ssp106 Ssp107 Ssp108 Ssp109 Ssp110 Ssp111 Ssp112 Ssp113 Ssp114 Ssp115 Ssp116
## 1      0      0      0      0      0      0      0      0      0      0      0
## 2      0      0      0      0      0      0      0      0      0      0      0
## 3      0      0      0      0      0      0      0      0      0      0      0
## 4      0      0      0      0      0      0      0      0      0      0      0
## 5      0      0      0      0      0      0      0      0      0      0      0
## 6      0      0      0      0      0      0      0      0      0      0      0
##   Ssp117 Ssp118 Ssp119 Ssp120 Ssp121 Ssp122 Ssp123 Ssp124 Ssp125 Ssp126 Ssp127
## 1    0.0      0      0      0      0    0.1      0      0      0      0      0
## 2    1.0      0      0      0      0    0.2      0      0      0      0      0
## 3    0.0      0      0      0      0    0.0      0      0      0      0      0
## 4    0.0      0      1      0      0    1.0      0      0      0      0      0
## 5    0.0      0      0      0      0    0.0      0      0      0      0      0
## 6    1.7      0      0      0      0    2.4      0      0      0      0      0
##   Ssp128 Ssp129 Ssp130 Ssp131 Ssp132 Ssp133 Ssp134 Ssp135 Ssp136 Ssp137 Ssp138
## 1      0      0      0    0.0      0      0      0      0      0      0    0.0
## 2      0      0      0    0.0      0      0      0      0      0      0    0.0
## 3      0      0      0    0.0      0      0      0      0      0      0    0.0
## 4      0      0      0    0.0      0      0      0      0      0      0    0.5
## 5      0      0      0    0.5      0      0      0      0      0      0    0.0
## 6      0      0      0    0.0      0      0      0      0      0      0    0.0
##   Ssp139 Ssp140 Ssp141 Ssp142 Ssp143 Ssp144 Ssp145 Ssp146 Ssp147 Ssp148 Ssp149
## 1      0      0      0      0      0      0      0      0      0      0      0
## 2      0      0      0      0      0      0      0      0      0      0      0
## 3      0      0      0      0      0      0      0      0      0      0      0
## 4      0      0      0      0      0      0      0      0      0      0      0
## 5      0      0      0      0      0      0      0      0      0      0      0
## 6      3      0      0      0      0      0      0      0      0      0      0
##   Ssp150 Ssp151 Ssp152 Ssp153 Ssp154 Ssp155 Ssp156 Ssp157 Ssp158 Ssp159 Ssp160
## 1    0.1      0      0      0      0      0      0      0      0      0      0
## 2    0.1      0      0      0      0      0      0      0      0      0      0
## 3    0.0      0      0      0      0      0      0      0      0      0      0
## 4    2.0      0      0      0      0      0      0      0      0      0      0
## 5    0.0      0      0      0      0      0      0      0      0      0      0
## 6    1.0      0      0      0      0      0      0      0      0      0      0
##   Ssp161 Ssp162 Ssp163 Ssp164 Ssp165 Ssp166 Ssp167 Ssp168 Ssp169 Ssp170 Ssp171
## 1      0      0      0      0      0      0      0      0      0      0      0
## 2      0      0      0      0      0      0      0      0      0      0      0
## 3      0      0      0      0      0      0      0      0      0      0      0
## 4      0      0      0      0      0      0      0      0      0      0      0
## 5      0      0      0      0      0      0      0      0      0      0      0
## 6      0      0      0      0      0      0      0      0      0      0      0
##   Ssp172 Ssp173 Ssp174 Ssp175 Ssp176 Ssp177 Ssp178 Ssp179 Ssp180 Ssp181 Ssp182
## 1      0      0      0      0      0      0      0      0      0      0      0
## 2      0      0      0      0      0      0      0      0      0      0      0
## 3      0      0      0      0      0      0      0      0      0      0      0
## 4      0      0      0      0      0      0      0      0      0      0      0
## 5      0      0      0      0      0      0      0      0      0      0      0
## 6      0      0      0      0      0      0      0      0      0      0      0
##   Ssp183 Ssp184 Ssp185 Ssp186 Ssp187 Ssp188 Ssp189 Ssp190 Ssp191 Ssp192
## 1      0      0      0      0      0      0      0      0      0      0
## 2      0      0      0      0      0      0      0      0      0      0
## 3      0      0      0      0      0      0      0      0      0      0
## 4      0      0      0      0      0      0      0      0      0      0
## 5      0      0      0      0      0      0      0      0      0      0
## 6      0      0      0      0      0      0      0      0      0      0
##   quad_num Site_num plot_num clfpos_num L3R4_num Site_name Quad_pos
## 1       44        3       22          1        4       PIL        R
## 2       46        3       23          2        4       PIL        R
## 3      151        8       76          1        3       GOA        L
## 4       43        3       22          1        3       PIL        L
## 5      126        7       63          2        4       NRS        R
## 6       45        3       23          2        3       PIL        L
##   Cliff_Position Plot_Location Control_or_treatment Nest_Position
## 1           Face            P1                 Nest    above_nest
## 2           Face            P2                 Nest    below_nest
## 3           Face            P1                 Nest    below_nest
## 4           Face            P1                 Nest    above_nest
## 5           Face            P2                 Nest    below_nest
## 6           Face            P2                 Nest    below_nest
env <- cover[,c("Quad_Identifier","Control_or_treatment", "Site_name", "Nest_Position")]


# Define the list of matching Quad Identifiers
matching_quad_ids <- env$Quad_Identifier

# Filter env to keep rows with matching Quad Identifiers
env_filtered <- env %>%
  filter(Quad_Identifier %in% matching_quad_ids)

# Filter rows in env based on matching Quad_Identifier values
env_filtered <- env[env$Quad_Identifier %in% rownames(all.matrix), ]


# Use the filtered Quad Identifiers from env to filter all.matrix
all.matrix_filtered <- all.matrix %>%
  filter(row.names(all.matrix) %in% env_filtered$Quad_Identifier)


env_filtered$Control_or_treatment <- as.factor(env_filtered$Control_or_treatment)
env_filtered$Site_name <- as.factor(env_filtered$Site_name)
env_filtered$Nest_Position <- as.factor(env_filtered$Nest_Position)







#overall rank abundance 
RankAbun1 <- rankabundance(all.matrix_filtered, y=env_filtered)
RankAbun1
##        rank abundance proportion plower pupper accumfreq logabun rankfreq
## Ssp90     1     427.0        5.7    2.2    9.2       5.7     2.6      0.7
## Ssp103    2     417.7        5.6    3.0    8.1      11.3     2.6      1.4
## Ssp17     3     395.5        5.3    1.8    8.8      16.6     2.6      2.1
## Ssp150    4     352.2        4.7    2.4    7.1      21.3     2.5      2.7
## Ssp187    5     281.4        3.8    1.2    6.3      25.1     2.4      3.4
## Ssp2      6     213.0        2.8    1.1    4.6      27.9     2.3      4.1
## Ssp32     7     213.0        2.8    0.0    5.7      30.7     2.3      4.8
## Ssp146    8     207.0        2.8    0.5    5.0      33.5     2.3      5.5
## Ssp137    9     203.0        2.7    0.7    4.8      36.2     2.3      6.2
## Ssp80    10     194.2        2.6    0.7    4.5      38.8     2.3      6.8
## Ssp177   11     193.0        2.6    1.4    3.7      41.4     2.3      7.5
## Ssp118   12     186.0        2.5   -0.1    5.1      43.9     2.3      8.2
## Ssp69    13     174.5        2.3   -0.2    4.9      46.2     2.2      8.9
## Ssp31    14     172.0        2.3    0.6    4.0      48.5     2.2      9.6
## Ssp119   15     145.3        1.9    1.0    2.9      50.5     2.2     10.3
## Ssp104   16     138.0        1.8    0.2    3.5      52.3     2.1     11.0
## Ssp95    17     135.0        1.8    0.8    2.8      54.1     2.1     11.6
## Ssp18    18     135.0        1.8    0.3    3.3      55.9     2.1     12.3
## Ssp162   19     118.0        1.6    0.5    2.7      57.5     2.1     13.0
## Ssp29    20     116.5        1.6    0.3    2.8      59.1     2.1     13.7
## Ssp14    21     116.0        1.6    0.5    2.6      60.6     2.1     14.4
## Ssp117   22     114.4        1.5    0.7    2.3      62.1     2.1     15.1
## Ssp154   23     110.2        1.5    0.3    2.6      63.6     2.0     15.8
## Ssp159   24     106.2        1.4    0.3    2.6      65.0     2.0     16.4
## Ssp156   25     101.0        1.4    0.1    2.6      66.4     2.0     17.1
## Ssp13    26      98.0        1.3    0.4    2.2      67.7     2.0     17.8
## Ssp49    27      98.0        1.3   -0.1    2.7      69.0     2.0     18.5
## Ssp149   28      89.0        1.2    0.3    2.1      70.2     1.9     19.2
## Ssp71    29      88.2        1.2    0.5    1.9      71.4     1.9     19.9
## Ssp26    30      84.0        1.1    0.1    2.2      72.5     1.9     20.5
## Ssp101   31      83.0        1.1    0.1    2.2      73.6     1.9     21.2
## Ssp143   32      70.0        0.9    0.1    1.8      74.5     1.8     21.9
## Ssp54    33      69.0        0.9    0.1    1.8      75.5     1.8     22.6
## Ssp191   34      68.0        0.9    0.0    1.8      76.4     1.8     23.3
## Ssp43    35      61.5        0.8    0.1    1.5      77.2     1.8     24.0
## Ssp73    36      56.0        0.7   -0.4    1.9      77.9     1.7     24.7
## Ssp108   37      56.0        0.7    0.0    1.5      78.7     1.7     25.3
## Ssp158   38      52.5        0.7   -0.1    1.5      79.4     1.7     26.0
## Ssp115   39      52.0        0.7    0.2    1.2      80.1     1.7     26.7
## Ssp114   40      51.0        0.7    0.1    1.2      80.8     1.7     27.4
## Ssp130   41      50.0        0.7   -0.4    1.8      81.4     1.7     28.1
## Ssp78    42      47.5        0.6    0.1    1.2      82.1     1.7     28.8
## Ssp189   43      42.0        0.6   -0.5    1.7      82.6     1.6     29.5
## Ssp112   44      41.0        0.5    0.0    1.1      83.2     1.6     30.1
## Ssp142   45      41.0        0.5   -0.3    1.4      83.7     1.6     30.8
## Ssp7     46      39.3        0.5   -0.2    1.2      84.3     1.6     31.5
## Ssp21    47      39.0        0.5   -0.2    1.2      84.8     1.6     32.2
## Ssp94    48      39.0        0.5    0.0    1.0      85.3     1.6     32.9
## Ssp66    49      38.4        0.5   -0.3    1.3      85.8     1.6     33.6
## Ssp192   50      38.0        0.5   -0.5    1.5      86.3     1.6     34.2
## Ssp6     51      37.2        0.5    0.0    1.0      86.8     1.6     34.9
## Ssp129   52      37.0        0.5    0.1    0.9      87.3     1.6     35.6
## Ssp55    53      33.5        0.4    0.0    0.9      87.8     1.5     36.3
## Ssp25    54      31.5        0.4    0.0    0.8      88.2     1.5     37.0
## Ssp1     55      30.0        0.4   -0.2    1.0      88.6     1.5     37.7
## Ssp121   56      29.2        0.4    0.0    0.8      89.0     1.5     38.4
## Ssp111   57      29.0        0.4    0.0    0.7      89.4     1.5     39.0
## Ssp3     58      29.0        0.4    0.0    0.8      89.8     1.5     39.7
## Ssp132   59      27.0        0.4    0.0    0.8      90.1     1.4     40.4
## Ssp122   60      26.5        0.4    0.0    0.7      90.5     1.4     41.1
## Ssp85    61      26.0        0.3    0.0    0.7      90.8     1.4     41.8
## Ssp8     62      26.0        0.3    0.0    0.7      91.2     1.4     42.5
## Ssp16    63      25.0        0.3    0.0    0.7      91.5     1.4     43.2
## Ssp113   64      25.0        0.3    0.0    0.7      91.8     1.4     43.8
## Ssp20    65      24.2        0.3   -0.2    0.8      92.2     1.4     44.5
## Ssp141   66      22.5        0.3    0.0    0.6      92.5     1.4     45.2
## Ssp10    67      22.0        0.3   -0.2    0.8      92.8     1.3     45.9
## Ssp139   68      22.0        0.3    0.0    0.6      93.0     1.3     46.6
## Ssp82    69      21.0        0.3    0.0    0.6      93.3     1.3     47.3
## Ssp57    70      21.0        0.3   -0.1    0.7      93.6     1.3     47.9
## Ssp171   71      19.0        0.3    0.0    0.5      93.9     1.3     48.6
## Ssp182   72      18.0        0.2   -0.2    0.7      94.1     1.3     49.3
## Ssp100   73      18.0        0.2    0.0    0.5      94.3     1.3     50.0
## Ssp160   74      17.5        0.2    0.0    0.5      94.6     1.2     50.7
## Ssp186   75      16.0        0.2    0.0    0.4      94.8     1.2     51.4
## Ssp109   76      15.0        0.2    0.0    0.4      95.0     1.2     52.1
## Ssp188   77      15.0        0.2   -0.2    0.6      95.2     1.2     52.7
## Ssp163   78      14.0        0.2   -0.1    0.5      95.4     1.1     53.4
## Ssp181   79      13.5        0.2   -0.1    0.4      95.6     1.1     54.1
## Ssp151   80      13.0        0.2   -0.2    0.5      95.7     1.1     54.8
## Ssp4     81      13.0        0.2    0.0    0.3      95.9     1.1     55.5
## Ssp133   82      12.0        0.2    0.0    0.4      96.1     1.1     56.2
## Ssp12    83      12.0        0.2   -0.1    0.4      96.2     1.1     56.8
## Ssp19    84      12.0        0.2   -0.1    0.4      96.4     1.1     57.5
## Ssp167   85      10.5        0.1    0.0    0.3      96.5     1.0     58.2
## Ssp45    86      10.0        0.1   -0.1    0.4      96.7     1.0     58.9
## Ssp164   87      10.0        0.1   -0.1    0.4      96.8     1.0     59.6
## Ssp39    88      10.0        0.1    0.0    0.2      96.9     1.0     60.3
## Ssp168   89      10.0        0.1   -0.1    0.3      97.1     1.0     61.0
## Ssp65    90       9.0        0.1   -0.1    0.3      97.2     1.0     61.6
## Ssp96    91       9.0        0.1   -0.1    0.3      97.3     1.0     62.3
## Ssp123   92       9.0        0.1   -0.1    0.3      97.4     1.0     63.0
## Ssp50    93       9.0        0.1   -0.1    0.4      97.5     1.0     63.7
## Ssp179   94       9.0        0.1   -0.1    0.3      97.7     1.0     64.4
## Ssp128   95       9.0        0.1    0.0    0.2      97.8     1.0     65.1
## Ssp72    96       8.0        0.1    0.0    0.2      97.9     0.9     65.8
## Ssp106   97       8.0        0.1   -0.1    0.3      98.0     0.9     66.4
## Ssp5     98       7.5        0.1    0.0    0.2      98.1     0.9     67.1
## Ssp27    99       7.0        0.1    0.0    0.2      98.2     0.8     67.8
## Ssp47   100       7.0        0.1   -0.1    0.3      98.3     0.8     68.5
## Ssp77   101       6.0        0.1    0.0    0.2      98.4     0.8     69.2
## Ssp15   102       6.0        0.1    0.0    0.2      98.4     0.8     69.9
## Ssp23   103       6.0        0.1   -0.1    0.2      98.5     0.8     70.5
## Ssp169  104       6.0        0.1   -0.1    0.2      98.6     0.8     71.2
## Ssp38   105       6.0        0.1   -0.1    0.2      98.7     0.8     71.9
## Ssp64   106       5.0        0.1   -0.1    0.2      98.8     0.7     72.6
## Ssp42   107       5.0        0.1   -0.1    0.2      98.8     0.7     73.3
## Ssp155  108       5.0        0.1    0.0    0.2      98.9     0.7     74.0
## Ssp35   109       5.0        0.1   -0.1    0.2      99.0     0.7     74.7
## Ssp67   110       5.0        0.1   -0.1    0.2      99.0     0.7     75.3
## Ssp81   111       4.0        0.1    0.0    0.1      99.1     0.6     76.0
## Ssp107  112       4.0        0.1    0.0    0.1      99.1     0.6     76.7
## Ssp120  113       4.0        0.1    0.0    0.1      99.2     0.6     77.4
## Ssp148  114       4.0        0.1    0.0    0.1      99.2     0.6     78.1
## Ssp46   115       4.0        0.1   -0.1    0.2      99.3     0.6     78.8
## Ssp125  116       4.0        0.1    0.0    0.1      99.3     0.6     79.5
## Ssp88   117       4.0        0.1    0.0    0.1      99.4     0.6     80.1
## Ssp127  118       3.5        0.0    0.0    0.1      99.4     0.5     80.8
## Ssp172  119       3.0        0.0    0.0    0.1      99.5     0.5     81.5
## Ssp61   120       3.0        0.0    0.0    0.1      99.5     0.5     82.2
## Ssp157  121       3.0        0.0    0.0    0.1      99.6     0.5     82.9
## Ssp136  122       3.0        0.0    0.0    0.1      99.6     0.5     83.6
## Ssp76   123       3.0        0.0    0.0    0.1      99.6     0.5     84.2
## Ssp9    124       2.5        0.0    0.0    0.1      99.7     0.4     84.9
## Ssp116  125       2.0        0.0    0.0    0.1      99.7     0.3     85.6
## Ssp44   126       2.0        0.0    0.0    0.1      99.7     0.3     86.3
## Ssp30   127       2.0        0.0    0.0    0.1      99.8     0.3     87.0
## Ssp24   128       2.0        0.0    0.0    0.1      99.8     0.3     87.7
## Ssp144  129       2.0        0.0    0.0    0.1      99.8     0.3     88.4
## Ssp56   130       1.5        0.0    0.0    0.0      99.8     0.2     89.0
## Ssp124  131       1.0        0.0    0.0    0.0      99.8     0.0     89.7
## Ssp53   132       1.0        0.0    0.0    0.0      99.9     0.0     90.4
## Ssp98   133       1.0        0.0    0.0    0.0      99.9     0.0     91.1
## Ssp68   134       1.0        0.0    0.0    0.0      99.9     0.0     91.8
## Ssp126  135       1.0        0.0    0.0    0.0      99.9     0.0     92.5
## Ssp131  136       1.0        0.0    0.0    0.0      99.9     0.0     93.2
## Ssp28   137       1.0        0.0    0.0    0.0      99.9     0.0     93.8
## Ssp178  138       1.0        0.0    0.0    0.0      99.9     0.0     94.5
## Ssp48   139       1.0        0.0    0.0    0.0     100.0     0.0     95.2
## Ssp93   140       0.5        0.0    0.0    0.0     100.0    -0.3     95.9
## Ssp138  141       0.5        0.0    0.0    0.0     100.0    -0.3     96.6
## Ssp110  142       0.5        0.0    0.0    0.0     100.0    -0.3     97.3
## Ssp62   143       0.5        0.0    0.0    0.0     100.0    -0.3     97.9
## Ssp40   144       0.5        0.0    0.0    0.0     100.0    -0.3     98.6
## Ssp83   145       0.5        0.0    0.0    0.0     100.0    -0.3     99.3
## Ssp99   146       0.5        0.0    0.0    0.0     100.0    -0.3    100.0
# Convert the matrix to a data frame
RankAbun1 <- as.data.frame(RankAbun1)

RankAbun1 <- RankAbun1 %>%
  rownames_to_column(var = "Species")
head(RankAbun1)
##   Species rank abundance proportion plower pupper accumfreq logabun rankfreq
## 1   Ssp90    1     427.0        5.7    2.2    9.2       5.7     2.6      0.7
## 2  Ssp103    2     417.7        5.6    3.0    8.1      11.3     2.6      1.4
## 3   Ssp17    3     395.5        5.3    1.8    8.8      16.6     2.6      2.1
## 4  Ssp150    4     352.2        4.7    2.4    7.1      21.3     2.5      2.7
## 5  Ssp187    5     281.4        3.8    1.2    6.3      25.1     2.4      3.4
## 6    Ssp2    6     213.0        2.8    1.1    4.6      27.9     2.3      4.1
RA_withSPID <- merge(RankAbun1, sp_codes, by.x = "Species", by.y = "Number", all.x = TRUE)
write.csv(RA_withSPID, "Output figs and tables/RankAbundance_overall.csv", row.names = F)


# rank abundance by treatment
RA_poop <- rankabuncomp(all.matrix_filtered, y=env_filtered, factor="Control_or_treatment",
                        scale='proportion', legend=FALSE)

RA_poop
##     Grouping species labelit rank abundance proportion plower pupper accumfreq
## 1    Control   Ssp17    TRUE    1     238.0        6.4    0.9   12.0       6.4
## 2    Control   Ssp90    TRUE    2     211.0        5.7    1.2   10.2      12.1
## 3    Control  Ssp103    TRUE    3     190.7        5.2    1.9    8.5      17.3
## 4    Control  Ssp118   FALSE    4     169.0        4.6   -0.5    9.6      21.9
## 5    Control  Ssp146   FALSE    5     157.0        4.2    0.1    8.4      26.1
## 6    Control   Ssp80   FALSE    6     129.7        3.5    0.1    6.9      29.6
## 7    Control  Ssp177   FALSE    7     123.0        3.3    1.2    5.4      32.9
## 8    Control  Ssp162   FALSE    8     118.0        3.2    1.0    5.4      36.1
## 9    Control    Ssp2   FALSE    9     111.0        3.0   -0.2    6.2      39.1
## 10   Control  Ssp150   FALSE   10     108.0        2.9    1.1    4.7      42.1
## 11   Control   Ssp49   FALSE   11      98.0        2.6   -0.3    5.6      44.7
## 12   Control   Ssp31   FALSE   12      97.0        2.6    0.1    5.2      47.3
## 13   Control   Ssp95   FALSE   13      94.0        2.5    0.7    4.4      49.9
## 14   Control   Ssp14   FALSE   14      91.0        2.5    0.5    4.4      52.3
## 15   Control  Ssp156   FALSE   15      81.0        2.2   -0.1    4.5      54.5
## 16   Control   Ssp26   FALSE   16      81.0        2.2    0.0    4.3      56.7
## 17   Control  Ssp119   FALSE   17      78.8        2.1    0.8    3.4      58.8
## 18   Control   Ssp54   FALSE   18      69.0        1.9    0.2    3.5      60.7
## 19   Control  Ssp191   FALSE   19      68.0        1.8    0.1    3.6      62.5
## 20   Control   Ssp13   FALSE   20      66.0        1.8    0.2    3.4      64.3
## 21   Control   Ssp29   FALSE   21      60.0        1.6   -0.6    3.8      66.0
## 22   Control  Ssp108   FALSE   22      56.0        1.5    0.1    2.9      67.5
## 23   Control  Ssp187   FALSE   23      52.4        1.4    0.4    2.5      68.9
## 24   Control  Ssp114   FALSE   24      51.0        1.4    0.4    2.4      70.3
## 25   Control  Ssp115   FALSE   25      45.0        1.2    0.2    2.3      71.5
## 26   Control  Ssp189   FALSE   26      42.0        1.1   -1.1    3.4      72.6
## 27   Control  Ssp142   FALSE   27      41.0        1.1   -0.6    2.8      73.7
## 28   Control    Ssp7   FALSE   28      39.3        1.1   -0.3    2.4      74.8
## 29   Control   Ssp21   FALSE   29      36.0        1.0   -0.4    2.3      75.8
## 30   Control    Ssp6   FALSE   30      36.0        1.0    0.1    1.9      76.7
## 31   Control   Ssp71   FALSE   31      34.7        0.9    0.2    1.7      77.7
## 32   Control   Ssp18   FALSE   32      32.0        0.9   -0.2    1.9      78.5
## 33   Control  Ssp129   FALSE   33      31.0        0.8    0.0    1.7      79.4
## 34   Control  Ssp143   FALSE   34      30.0        0.8    0.1    1.5      80.2
## 35   Control    Ssp1   FALSE   35      30.0        0.8   -0.4    2.0      81.0
## 36   Control   Ssp78   FALSE   36      29.5        0.8    0.1    1.5      81.8
## 37   Control  Ssp132   FALSE   37      27.0        0.7   -0.1    1.5      82.5
## 38   Control   Ssp85   FALSE   38      26.0        0.7   -0.1    1.5      83.2
## 39   Control   Ssp43   FALSE   39      24.5        0.7   -0.1    1.5      83.9
## 40   Control  Ssp113   FALSE   40      24.0        0.6    0.0    1.3      84.5
## 41   Control   Ssp25   FALSE   41      24.0        0.6   -0.1    1.4      85.2
## 42   Control   Ssp69   FALSE   42      22.5        0.6   -0.1    1.3      85.8
## 43   Control  Ssp141   FALSE   43      22.5        0.6   -0.1    1.3      86.4
## 44   Control  Ssp104   FALSE   44      22.0        0.6   -0.1    1.3      87.0
## 45   Control   Ssp94   FALSE   45      20.0        0.5   -0.1    1.2      87.5
## 46   Control  Ssp117   FALSE   46      19.2        0.5    0.1    0.9      88.1
## 47   Control  Ssp101   FALSE   47      19.0        0.5   -0.5    1.5      88.6
## 48   Control  Ssp121   FALSE   48      18.2        0.5   -0.2    1.2      89.1
## 49   Control  Ssp100   FALSE   49      18.0        0.5    0.0    1.0      89.6
## 50   Control   Ssp57   FALSE   50      18.0        0.5   -0.3    1.3      90.0
## 51   Control  Ssp182   FALSE   51      18.0        0.5   -0.5    1.5      90.5
## 52   Control  Ssp158   FALSE   52      17.5        0.5   -0.1    1.1      91.0
## 53   Control  Ssp154   FALSE   53      17.2        0.5   -0.4    1.3      91.5
## 54   Control  Ssp112   FALSE   54      17.0        0.5   -0.3    1.2      91.9
## 55   Control  Ssp188   FALSE   55      15.0        0.4   -0.4    1.2      92.3
## 56   Control  Ssp149   FALSE   56      15.0        0.4   -0.2    1.0      92.7
## 57   Control  Ssp111   FALSE   57      13.0        0.4    0.0    0.7      93.1
## 58   Control  Ssp171   FALSE   58      12.0        0.3   -0.1    0.7      93.4
## 59   Control   Ssp12   FALSE   59      12.0        0.3   -0.2    0.8      93.7
## 60   Control   Ssp19   FALSE   60      11.0        0.3   -0.2    0.8      94.0
## 61   Control  Ssp163   FALSE   61      11.0        0.3   -0.3    0.8      94.3
## 62   Control  Ssp159   FALSE   62      10.2        0.3    0.0    0.5      94.6
## 63   Control   Ssp45   FALSE   63      10.0        0.3   -0.3    0.8      94.9
## 64   Control   Ssp82   FALSE   64      10.0        0.3   -0.2    0.8      95.2
## 65   Control   Ssp16   FALSE   65      10.0        0.3   -0.2    0.8      95.4
## 66   Control  Ssp122   FALSE   66       9.3        0.3   -0.1    0.6      95.7
## 67   Control   Ssp50   FALSE   67       9.0        0.2   -0.2    0.7      95.9
## 68   Control  Ssp130   FALSE   68       9.0        0.2   -0.2    0.7      96.2
## 69   Control   Ssp32   FALSE   69       8.0        0.2   -0.2    0.7      96.4
## 70   Control  Ssp167   FALSE   70       8.0        0.2   -0.1    0.6      96.6
## 71   Control  Ssp137   FALSE   71       8.0        0.2   -0.1    0.5      96.8
## 72   Control  Ssp160   FALSE   72       8.0        0.2   -0.1    0.5      97.0
## 73   Control    Ssp5   FALSE   73       7.5        0.2   -0.1    0.5      97.2
## 74   Control  Ssp128   FALSE   74       7.0        0.2   -0.1    0.4      97.4
## 75   Control   Ssp65   FALSE   75       7.0        0.2   -0.2    0.6      97.6
## 76   Control   Ssp39   FALSE   76       7.0        0.2    0.0    0.4      97.8
## 77   Control   Ssp96   FALSE   77       6.0        0.2   -0.2    0.5      98.0
## 78   Control  Ssp169   FALSE   78       6.0        0.2   -0.2    0.5      98.1
## 79   Control   Ssp67   FALSE   79       5.0        0.1   -0.1    0.4      98.3
## 80   Control   Ssp42   FALSE   80       5.0        0.1   -0.1    0.4      98.4
## 81   Control  Ssp107   FALSE   81       4.0        0.1   -0.1    0.3      98.5
## 82   Control  Ssp181   FALSE   82       4.0        0.1   -0.1    0.3      98.6
## 83   Control   Ssp46   FALSE   83       4.0        0.1   -0.1    0.3      98.7
## 84   Control  Ssp179   FALSE   84       3.0        0.1   -0.1    0.2      98.8
## 85   Control   Ssp20   FALSE   85       3.0        0.1   -0.1    0.2      98.9
## 86   Control  Ssp109   FALSE   86       3.0        0.1   -0.1    0.2      99.0
## 87   Control  Ssp168   FALSE   87       3.0        0.1   -0.1    0.2      99.0
## 88   Control  Ssp125   FALSE   88       3.0        0.1    0.0    0.2      99.1
## 89   Control  Ssp136   FALSE   89       2.5        0.1    0.0    0.1      99.2
## 90   Control   Ssp66   FALSE   90       2.5        0.1    0.0    0.2      99.3
## 91   Control  Ssp133   FALSE   91       2.0        0.1   -0.1    0.2      99.3
## 92   Control   Ssp88   FALSE   92       2.0        0.1   -0.1    0.2      99.4
## 93   Control  Ssp144   FALSE   93       2.0        0.1    0.0    0.1      99.4
## 94   Control   Ssp24   FALSE   94       2.0        0.1    0.0    0.1      99.5
## 95   Control   Ssp30   FALSE   95       2.0        0.1    0.0    0.1      99.5
## 96   Control   Ssp10   FALSE   96       1.0        0.0    0.0    0.1      99.6
## 97   Control  Ssp124   FALSE   97       1.0        0.0    0.0    0.1      99.6
## 98   Control   Ssp15   FALSE   98       1.0        0.0    0.0    0.1      99.6
## 99   Control  Ssp139   FALSE   99       1.0        0.0    0.0    0.1      99.6
## 100  Control  Ssp155   FALSE  100       1.0        0.0    0.0    0.1      99.7
## 101  Control  Ssp126   FALSE  101       1.0        0.0    0.0    0.1      99.7
## 102  Control    Ssp4   FALSE  102       1.0        0.0    0.0    0.1      99.7
## 103  Control  Ssp127   FALSE  103       1.0        0.0    0.0    0.1      99.7
## 104  Control  Ssp178   FALSE  104       1.0        0.0    0.0    0.1      99.8
## 105  Control  Ssp116   FALSE  105       1.0        0.0    0.0    0.1      99.8
## 106  Control   Ssp48   FALSE  106       1.0        0.0    0.0    0.1      99.8
## 107  Control   Ssp68   FALSE  107       1.0        0.0    0.0    0.1      99.9
## 108  Control  Ssp120   FALSE  108       1.0        0.0    0.0    0.1      99.9
## 109  Control   Ssp76   FALSE  109       1.0        0.0    0.0    0.1      99.9
## 110  Control  Ssp172   FALSE  110       1.0        0.0    0.0    0.1      99.9
## 111  Control   Ssp27   FALSE  111       1.0        0.0    0.0    0.1     100.0
## 112  Control  Ssp110   FALSE  112       0.5        0.0    0.0    0.0     100.0
## 113  Control   Ssp56   FALSE  113       0.5        0.0    0.0    0.0     100.0
## 114  Control   Ssp83   FALSE  114       0.5        0.0    0.0    0.0     100.0
## 115     Nest  Ssp150    TRUE    1     244.2        6.5    2.1   10.9       6.5
## 116     Nest  Ssp187    TRUE    2     229.0        6.1    1.5   10.7      12.5
## 117     Nest  Ssp103    TRUE    3     227.0        6.0    2.1    9.9      18.5
## 118     Nest   Ssp90   FALSE    4     216.0        5.7    0.2   11.2      24.2
## 119     Nest   Ssp32   FALSE    5     205.0        5.4   -0.3   11.2      29.7
## 120     Nest  Ssp137   FALSE    6     195.0        5.2    1.6    8.7      34.8
## 121     Nest   Ssp17   FALSE    7     157.5        4.2   -0.2    8.5      39.0
## 122     Nest   Ssp69   FALSE    8     152.0        4.0   -1.1    9.1      43.0
## 123     Nest  Ssp104   FALSE    9     116.0        3.1   -0.2    6.3      46.1
## 124     Nest   Ssp18   FALSE   10     103.0        2.7   -0.1    5.5      48.8
## 125     Nest    Ssp2   FALSE   11     102.0        2.7    1.0    4.4      51.5
## 126     Nest  Ssp159   FALSE   12      96.0        2.5    0.2    4.8      54.0
## 127     Nest  Ssp117   FALSE   13      95.2        2.5    0.9    4.1      56.5
## 128     Nest  Ssp154   FALSE   14      93.0        2.5    0.5    4.5      59.0
## 129     Nest   Ssp31   FALSE   15      75.0        2.0   -0.2    4.2      61.0
## 130     Nest  Ssp149   FALSE   16      74.0        2.0    0.2    3.7      62.9
## 131     Nest  Ssp177   FALSE   17      70.0        1.9    0.7    3.0      64.8
## 132     Nest  Ssp119   FALSE   18      66.5        1.8    0.4    3.2      66.6
## 133     Nest   Ssp80   FALSE   19      64.5        1.7    0.1    3.3      68.3
## 134     Nest  Ssp101   FALSE   20      64.0        1.7   -0.1    3.5      70.0
## 135     Nest   Ssp29   FALSE   21      56.5        1.5    0.3    2.7      71.4
## 136     Nest   Ssp73   FALSE   22      56.0        1.5   -0.8    3.8      72.9
## 137     Nest   Ssp71   FALSE   23      53.5        1.4    0.3    2.6      74.3
## 138     Nest  Ssp146   FALSE   24      50.0        1.3   -0.5    3.2      75.7
## 139     Nest   Ssp95   FALSE   25      41.0        1.1    0.3    1.9      76.8
## 140     Nest  Ssp130   FALSE   26      41.0        1.1   -1.0    3.2      77.8
## 141     Nest  Ssp143   FALSE   27      40.0        1.1   -0.6    2.7      78.9
## 142     Nest  Ssp192   FALSE   28      38.0        1.0   -0.9    2.9      79.9
## 143     Nest   Ssp43   FALSE   29      37.0        1.0   -0.2    2.2      80.9
## 144     Nest   Ssp66   FALSE   30      35.9        0.9   -0.7    2.6      81.8
## 145     Nest  Ssp158   FALSE   31      35.0        0.9   -0.7    2.5      82.8
## 146     Nest   Ssp55   FALSE   32      33.5        0.9   -0.1    1.8      83.6
## 147     Nest   Ssp13   FALSE   33      32.0        0.8    0.0    1.7      84.5
## 148     Nest    Ssp3   FALSE   34      29.0        0.8   -0.1    1.6      85.3
## 149     Nest    Ssp8   FALSE   35      26.0        0.7    0.0    1.4      85.9
## 150     Nest   Ssp14   FALSE   36      25.0        0.7   -0.2    1.5      86.6
## 151     Nest  Ssp112   FALSE   37      24.0        0.6   -0.1    1.3      87.2
## 152     Nest   Ssp20   FALSE   38      21.2        0.6   -0.5    1.6      87.8
## 153     Nest  Ssp139   FALSE   39      21.0        0.6   -0.1    1.2      88.4
## 154     Nest   Ssp10   FALSE   40      21.0        0.6   -0.5    1.6      88.9
## 155     Nest  Ssp156   FALSE   41      20.0        0.5   -0.2    1.3      89.4
## 156     Nest   Ssp94   FALSE   42      19.0        0.5   -0.3    1.3      89.9
## 157     Nest   Ssp78   FALSE   43      18.0        0.5   -0.4    1.4      90.4
## 158     Nest  Ssp122   FALSE   44      17.2        0.5   -0.2    1.1      90.9
## 159     Nest  Ssp118   FALSE   45      17.0        0.4   -0.1    1.0      91.3
## 160     Nest  Ssp111   FALSE   46      16.0        0.4   -0.2    1.0      91.7
## 161     Nest  Ssp186   FALSE   47      16.0        0.4    0.0    0.8      92.2
## 162     Nest   Ssp16   FALSE   48      15.0        0.4   -0.1    0.9      92.6
## 163     Nest  Ssp151   FALSE   49      13.0        0.3   -0.4    1.0      92.9
## 164     Nest    Ssp4   FALSE   50      12.0        0.3    0.0    0.6      93.2
## 165     Nest  Ssp109   FALSE   51      12.0        0.3   -0.1    0.8      93.5
## 166     Nest   Ssp82   FALSE   52      11.0        0.3   -0.1    0.7      93.8
## 167     Nest  Ssp121   FALSE   53      11.0        0.3   -0.3    0.9      94.1
## 168     Nest  Ssp133   FALSE   54      10.0        0.3   -0.1    0.6      94.4
## 169     Nest  Ssp164   FALSE   55      10.0        0.3   -0.2    0.8      94.7
## 170     Nest  Ssp181   FALSE   56       9.5        0.3   -0.2    0.8      94.9
## 171     Nest  Ssp160   FALSE   57       9.5        0.3   -0.2    0.7      95.2
## 172     Nest  Ssp123   FALSE   58       9.0        0.2   -0.1    0.6      95.4
## 173     Nest   Ssp72   FALSE   59       8.0        0.2    0.0    0.5      95.6
## 174     Nest  Ssp106   FALSE   60       8.0        0.2   -0.2    0.6      95.8
## 175     Nest   Ssp25   FALSE   61       7.5        0.2   -0.2    0.6      96.0
## 176     Nest  Ssp171   FALSE   62       7.0        0.2   -0.1    0.5      96.2
## 177     Nest  Ssp168   FALSE   63       7.0        0.2   -0.2    0.6      96.4
## 178     Nest   Ssp47   FALSE   64       7.0        0.2   -0.2    0.5      96.6
## 179     Nest  Ssp115   FALSE   65       7.0        0.2   -0.1    0.4      96.8
## 180     Nest   Ssp38   FALSE   66       6.0        0.2   -0.2    0.5      96.9
## 181     Nest  Ssp179   FALSE   67       6.0        0.2   -0.2    0.5      97.1
## 182     Nest   Ssp23   FALSE   68       6.0        0.2   -0.2    0.5      97.2
## 183     Nest   Ssp77   FALSE   69       6.0        0.2   -0.1    0.4      97.4
## 184     Nest  Ssp129   FALSE   70       6.0        0.2   -0.1    0.4      97.5
## 185     Nest   Ssp27   FALSE   71       6.0        0.2   -0.1    0.4      97.7
## 186     Nest   Ssp15   FALSE   72       5.0        0.1   -0.1    0.4      97.8
## 187     Nest   Ssp35   FALSE   73       5.0        0.1   -0.1    0.4      98.0
## 188     Nest   Ssp64   FALSE   74       5.0        0.1   -0.1    0.4      98.1
## 189     Nest   Ssp81   FALSE   75       4.0        0.1    0.0    0.3      98.2
## 190     Nest  Ssp148   FALSE   76       4.0        0.1   -0.1    0.3      98.3
## 191     Nest  Ssp155   FALSE   77       4.0        0.1   -0.1    0.3      98.4
## 192     Nest  Ssp120   FALSE   78       3.0        0.1   -0.1    0.2      98.5
## 193     Nest  Ssp157   FALSE   79       3.0        0.1   -0.1    0.2      98.6
## 194     Nest   Ssp26   FALSE   80       3.0        0.1   -0.1    0.2      98.7
## 195     Nest   Ssp61   FALSE   81       3.0        0.1   -0.1    0.2      98.7
## 196     Nest   Ssp21   FALSE   82       3.0        0.1    0.0    0.2      98.8
## 197     Nest   Ssp39   FALSE   83       3.0        0.1    0.0    0.2      98.9
## 198     Nest   Ssp96   FALSE   84       3.0        0.1   -0.1    0.2      99.0
## 199     Nest  Ssp163   FALSE   85       3.0        0.1   -0.1    0.2      99.1
## 200     Nest   Ssp57   FALSE   86       3.0        0.1   -0.1    0.2      99.1
## 201     Nest    Ssp9   FALSE   87       2.5        0.1   -0.1    0.2      99.2
## 202     Nest  Ssp167   FALSE   88       2.5        0.1   -0.1    0.2      99.3
## 203     Nest  Ssp127   FALSE   89       2.5        0.1    0.0    0.2      99.3
## 204     Nest  Ssp128   FALSE   90       2.0        0.1    0.0    0.1      99.4
## 205     Nest   Ssp65   FALSE   91       2.0        0.1   -0.1    0.2      99.4
## 206     Nest   Ssp88   FALSE   92       2.0        0.1   -0.1    0.2      99.5
## 207     Nest   Ssp44   FALSE   93       2.0        0.1   -0.1    0.2      99.5
## 208     Nest   Ssp76   FALSE   94       2.0        0.1   -0.1    0.2      99.6
## 209     Nest  Ssp172   FALSE   95       2.0        0.1    0.0    0.2      99.6
## 210     Nest    Ssp6   FALSE   96       1.2        0.0    0.0    0.1      99.7
## 211     Nest   Ssp19   FALSE   97       1.0        0.0    0.0    0.1      99.7
## 212     Nest   Ssp28   FALSE   98       1.0        0.0    0.0    0.1      99.7
## 213     Nest  Ssp116   FALSE   99       1.0        0.0    0.0    0.1      99.8
## 214     Nest   Ssp56   FALSE  100       1.0        0.0    0.0    0.1      99.8
## 215     Nest   Ssp98   FALSE  101       1.0        0.0    0.0    0.1      99.8
## 216     Nest  Ssp131   FALSE  102       1.0        0.0    0.0    0.1      99.8
## 217     Nest  Ssp125   FALSE  103       1.0        0.0    0.0    0.1      99.9
## 218     Nest   Ssp53   FALSE  104       1.0        0.0    0.0    0.1      99.9
## 219     Nest  Ssp113   FALSE  105       1.0        0.0    0.0    0.1      99.9
## 220     Nest   Ssp40   FALSE  106       0.5        0.0    0.0    0.0      99.9
## 221     Nest   Ssp93   FALSE  107       0.5        0.0    0.0    0.0      99.9
## 222     Nest  Ssp138   FALSE  108       0.5        0.0    0.0    0.0     100.0
## 223     Nest   Ssp62   FALSE  109       0.5        0.0    0.0    0.0     100.0
## 224     Nest  Ssp136   FALSE  110       0.5        0.0    0.0    0.0     100.0
## 225     Nest   Ssp99   FALSE  111       0.5        0.0    0.0    0.0     100.0
##     logabun rankfreq
## 1       2.4      0.9
## 2       2.3      1.8
## 3       2.3      2.6
## 4       2.2      3.5
## 5       2.2      4.4
## 6       2.1      5.3
## 7       2.1      6.1
## 8       2.1      7.0
## 9       2.0      7.9
## 10      2.0      8.8
## 11      2.0      9.6
## 12      2.0     10.5
## 13      2.0     11.4
## 14      2.0     12.3
## 15      1.9     13.2
## 16      1.9     14.0
## 17      1.9     14.9
## 18      1.8     15.8
## 19      1.8     16.7
## 20      1.8     17.5
## 21      1.8     18.4
## 22      1.7     19.3
## 23      1.7     20.2
## 24      1.7     21.1
## 25      1.7     21.9
## 26      1.6     22.8
## 27      1.6     23.7
## 28      1.6     24.6
## 29      1.6     25.4
## 30      1.6     26.3
## 31      1.5     27.2
## 32      1.5     28.1
## 33      1.5     28.9
## 34      1.5     29.8
## 35      1.5     30.7
## 36      1.5     31.6
## 37      1.4     32.5
## 38      1.4     33.3
## 39      1.4     34.2
## 40      1.4     35.1
## 41      1.4     36.0
## 42      1.4     36.8
## 43      1.4     37.7
## 44      1.3     38.6
## 45      1.3     39.5
## 46      1.3     40.4
## 47      1.3     41.2
## 48      1.3     42.1
## 49      1.3     43.0
## 50      1.3     43.9
## 51      1.3     44.7
## 52      1.2     45.6
## 53      1.2     46.5
## 54      1.2     47.4
## 55      1.2     48.2
## 56      1.2     49.1
## 57      1.1     50.0
## 58      1.1     50.9
## 59      1.1     51.8
## 60      1.0     52.6
## 61      1.0     53.5
## 62      1.0     54.4
## 63      1.0     55.3
## 64      1.0     56.1
## 65      1.0     57.0
## 66      1.0     57.9
## 67      1.0     58.8
## 68      1.0     59.6
## 69      0.9     60.5
## 70      0.9     61.4
## 71      0.9     62.3
## 72      0.9     63.2
## 73      0.9     64.0
## 74      0.8     64.9
## 75      0.8     65.8
## 76      0.8     66.7
## 77      0.8     67.5
## 78      0.8     68.4
## 79      0.7     69.3
## 80      0.7     70.2
## 81      0.6     71.1
## 82      0.6     71.9
## 83      0.6     72.8
## 84      0.5     73.7
## 85      0.5     74.6
## 86      0.5     75.4
## 87      0.5     76.3
## 88      0.5     77.2
## 89      0.4     78.1
## 90      0.4     78.9
## 91      0.3     79.8
## 92      0.3     80.7
## 93      0.3     81.6
## 94      0.3     82.5
## 95      0.3     83.3
## 96      0.0     84.2
## 97      0.0     85.1
## 98      0.0     86.0
## 99      0.0     86.8
## 100     0.0     87.7
## 101     0.0     88.6
## 102     0.0     89.5
## 103     0.0     90.4
## 104     0.0     91.2
## 105     0.0     92.1
## 106     0.0     93.0
## 107     0.0     93.9
## 108     0.0     94.7
## 109     0.0     95.6
## 110     0.0     96.5
## 111     0.0     97.4
## 112    -0.3     98.2
## 113    -0.3     99.1
## 114    -0.3    100.0
## 115     2.4      0.9
## 116     2.4      1.8
## 117     2.4      2.7
## 118     2.3      3.6
## 119     2.3      4.5
## 120     2.3      5.4
## 121     2.2      6.3
## 122     2.2      7.2
## 123     2.1      8.1
## 124     2.0      9.0
## 125     2.0      9.9
## 126     2.0     10.8
## 127     2.0     11.7
## 128     2.0     12.6
## 129     1.9     13.5
## 130     1.9     14.4
## 131     1.8     15.3
## 132     1.8     16.2
## 133     1.8     17.1
## 134     1.8     18.0
## 135     1.8     18.9
## 136     1.7     19.8
## 137     1.7     20.7
## 138     1.7     21.6
## 139     1.6     22.5
## 140     1.6     23.4
## 141     1.6     24.3
## 142     1.6     25.2
## 143     1.6     26.1
## 144     1.6     27.0
## 145     1.5     27.9
## 146     1.5     28.8
## 147     1.5     29.7
## 148     1.5     30.6
## 149     1.4     31.5
## 150     1.4     32.4
## 151     1.4     33.3
## 152     1.3     34.2
## 153     1.3     35.1
## 154     1.3     36.0
## 155     1.3     36.9
## 156     1.3     37.8
## 157     1.3     38.7
## 158     1.2     39.6
## 159     1.2     40.5
## 160     1.2     41.4
## 161     1.2     42.3
## 162     1.2     43.2
## 163     1.1     44.1
## 164     1.1     45.0
## 165     1.1     45.9
## 166     1.0     46.8
## 167     1.0     47.7
## 168     1.0     48.6
## 169     1.0     49.5
## 170     1.0     50.5
## 171     1.0     51.4
## 172     1.0     52.3
## 173     0.9     53.2
## 174     0.9     54.1
## 175     0.9     55.0
## 176     0.8     55.9
## 177     0.8     56.8
## 178     0.8     57.7
## 179     0.8     58.6
## 180     0.8     59.5
## 181     0.8     60.4
## 182     0.8     61.3
## 183     0.8     62.2
## 184     0.8     63.1
## 185     0.8     64.0
## 186     0.7     64.9
## 187     0.7     65.8
## 188     0.7     66.7
## 189     0.6     67.6
## 190     0.6     68.5
## 191     0.6     69.4
## 192     0.5     70.3
## 193     0.5     71.2
## 194     0.5     72.1
## 195     0.5     73.0
## 196     0.5     73.9
## 197     0.5     74.8
## 198     0.5     75.7
## 199     0.5     76.6
## 200     0.5     77.5
## 201     0.4     78.4
## 202     0.4     79.3
## 203     0.4     80.2
## 204     0.3     81.1
## 205     0.3     82.0
## 206     0.3     82.9
## 207     0.3     83.8
## 208     0.3     84.7
## 209     0.3     85.6
## 210     0.1     86.5
## 211     0.0     87.4
## 212     0.0     88.3
## 213     0.0     89.2
## 214     0.0     90.1
## 215     0.0     91.0
## 216     0.0     91.9
## 217     0.0     92.8
## 218     0.0     93.7
## 219     0.0     94.6
## 220    -0.3     95.5
## 221    -0.3     96.4
## 222    -0.3     97.3
## 223    -0.3     98.2
## 224    -0.3     99.1
## 225    -0.3    100.0
# now assign functional group and species ID  
RA_poop_withSPID <- merge(RA_poop, sp_codes, by.x = "species", by.y = "Number", all.x = TRUE)
write.csv(RA_poop_withSPID, "Output figs and tables/RankAbundance_byTreatment.csv", row.names = F)


# rank abundance by nest location
RA_nest <- rankabuncomp(all.matrix_filtered, y=env_filtered, factor="Nest_Position",
                        scale='proportion', legend=FALSE)

RA_nest
##       Grouping species labelit rank abundance proportion plower pupper
## 1   above_nest  Ssp103    TRUE    1      49.0       13.3   -3.3   30.0
## 2   above_nest  Ssp130    TRUE    2      40.0       10.9  -10.4   32.2
## 3   above_nest  Ssp118    TRUE    3      34.0        9.2   -5.9   24.4
## 4   above_nest  Ssp146   FALSE    4      27.0        7.3   -4.4   19.0
## 5   above_nest   Ssp54   FALSE    5      25.0        6.8   -2.8   16.4
## 6   above_nest   Ssp85   FALSE    6      19.0        5.2   -4.2   14.5
## 7   above_nest   Ssp94   FALSE    7      18.0        4.9   -0.5   10.3
## 8   above_nest  Ssp122   FALSE    8      13.2        3.6   -3.4   10.5
## 9   above_nest  Ssp151   FALSE    9      13.0        3.5   -4.6   11.7
## 10  above_nest  Ssp109   FALSE   10      12.0        3.3   -0.5    7.0
## 11  above_nest  Ssp177   FALSE   11      10.0        2.7   -1.1    6.5
## 12  above_nest   Ssp65   FALSE   12       9.0        2.4   -1.8    6.7
## 13  above_nest  Ssp117   FALSE   13       9.0        2.4   -0.6    5.5
## 14  above_nest   Ssp82   FALSE   14       8.0        2.2   -2.0    6.4
## 15  above_nest  Ssp111   FALSE   15       8.0        2.2   -2.5    6.8
## 16  above_nest   Ssp13   FALSE   16       8.0        2.2   -2.1    6.4
## 17  above_nest   Ssp96   FALSE   17       6.0        1.6   -2.0    5.3
## 18  above_nest   Ssp38   FALSE   18       6.0        1.6   -2.1    5.4
## 19  above_nest  Ssp150   FALSE   19       5.1        1.4   -1.1    3.9
## 20  above_nest  Ssp171   FALSE   20       5.0        1.4   -1.8    4.5
## 21  above_nest   Ssp35   FALSE   21       5.0        1.4   -1.8    4.5
## 22  above_nest  Ssp119   FALSE   22       3.1        0.8   -0.7    2.4
## 23  above_nest  Ssp114   FALSE   23       3.0        0.8   -1.2    2.8
## 24  above_nest   Ssp69   FALSE   24       3.0        0.8   -0.6    2.2
## 25  above_nest   Ssp49   FALSE   25       3.0        0.8   -0.7    2.4
## 26  above_nest   Ssp61   FALSE   26       3.0        0.8   -1.2    2.8
## 27  above_nest   Ssp57   FALSE   27       3.0        0.8   -0.8    2.4
## 28  above_nest  Ssp167   FALSE   28       2.5        0.7   -0.8    2.1
## 29  above_nest   Ssp27   FALSE   29       2.0        0.5   -0.6    1.7
## 30  above_nest  Ssp112   FALSE   30       2.0        0.5   -0.1    1.2
## 31  above_nest   Ssp76   FALSE   31       2.0        0.5   -0.6    1.7
## 32  above_nest   Ssp10   FALSE   32       2.0        0.5   -0.8    1.9
## 33  above_nest  Ssp128   FALSE   33       1.5        0.4   -0.1    0.9
## 34  above_nest  Ssp148   FALSE   34       1.0        0.3   -0.3    0.8
## 35  above_nest   Ssp21   FALSE   35       1.0        0.3   -0.3    0.8
## 36  above_nest  Ssp155   FALSE   36       1.0        0.3   -0.3    0.9
## 37  above_nest  Ssp159   FALSE   37       1.0        0.3   -0.3    0.9
## 38  above_nest  Ssp125   FALSE   38       1.0        0.3   -0.3    0.8
## 39  above_nest  Ssp187   FALSE   39       1.0        0.3   -0.3    0.8
## 40  above_nest  Ssp139   FALSE   40       0.5        0.1   -0.2    0.4
## 41  above_nest   Ssp62   FALSE   41       0.5        0.1   -0.2    0.5
## 42  above_nest   Ssp56   FALSE   42       0.5        0.1   -0.2    0.4
## 43  above_nest  Ssp138   FALSE   43       0.5        0.1   -0.2    0.5
## 44  above_nest  Ssp127   FALSE   44       0.5        0.1   -0.1    0.4
## 45  below_nest   Ssp90    TRUE    1     427.0        6.0    2.3    9.7
## 46  below_nest   Ssp17    TRUE    2     395.5        5.6    1.9    9.2
## 47  below_nest  Ssp103    TRUE    3     368.7        5.2    2.6    7.7
## 48  below_nest  Ssp150   FALSE    4     347.1        4.9    2.4    7.4
## 49  below_nest  Ssp187   FALSE    5     280.4        3.9    1.3    6.6
## 50  below_nest   Ssp32   FALSE    6     213.0        3.0    0.0    6.0
## 51  below_nest    Ssp2   FALSE    7     213.0        3.0    1.1    4.9
## 52  below_nest  Ssp137   FALSE    8     203.0        2.9    0.7    5.0
## 53  below_nest   Ssp80   FALSE    9     194.2        2.7    0.8    4.7
## 54  below_nest  Ssp177   FALSE   10     183.0        2.6    1.4    3.8
## 55  below_nest  Ssp146   FALSE   11     180.0        2.5    0.2    4.9
## 56  below_nest   Ssp31   FALSE   12     172.0        2.4    0.6    4.2
## 57  below_nest   Ssp69   FALSE   13     171.5        2.4   -0.3    5.1
## 58  below_nest  Ssp118   FALSE   14     152.0        2.1   -0.5    4.8
## 59  below_nest  Ssp119   FALSE   15     142.2        2.0    1.0    3.0
## 60  below_nest  Ssp104   FALSE   16     138.0        1.9    0.2    3.7
## 61  below_nest   Ssp95   FALSE   17     135.0        1.9    0.8    3.0
## 62  below_nest   Ssp18   FALSE   18     135.0        1.9    0.3    3.5
## 63  below_nest  Ssp162   FALSE   19     118.0        1.7    0.5    2.8
## 64  below_nest   Ssp29   FALSE   20     116.5        1.6    0.3    2.9
## 65  below_nest   Ssp14   FALSE   21     116.0        1.6    0.5    2.8
## 66  below_nest  Ssp154   FALSE   22     110.2        1.5    0.3    2.8
## 67  below_nest  Ssp117   FALSE   23     105.4        1.5    0.6    2.3
## 68  below_nest  Ssp159   FALSE   24     105.2        1.5    0.3    2.7
## 69  below_nest  Ssp156   FALSE   25     101.0        1.4    0.1    2.7
## 70  below_nest   Ssp49   FALSE   26      95.0        1.3   -0.2    2.8
## 71  below_nest   Ssp13   FALSE   27      90.0        1.3    0.3    2.2
## 72  below_nest  Ssp149   FALSE   28      89.0        1.3    0.3    2.2
## 73  below_nest   Ssp71   FALSE   29      88.2        1.2    0.5    2.0
## 74  below_nest   Ssp26   FALSE   30      84.0        1.2    0.1    2.3
## 75  below_nest  Ssp101   FALSE   31      83.0        1.2    0.1    2.3
## 76  below_nest  Ssp143   FALSE   32      70.0        1.0    0.1    1.9
## 77  below_nest  Ssp191   FALSE   33      68.0        1.0    0.0    1.9
## 78  below_nest   Ssp43   FALSE   34      61.5        0.9    0.1    1.6
## 79  below_nest   Ssp73   FALSE   35      56.0        0.8   -0.4    2.0
## 80  below_nest  Ssp108   FALSE   36      56.0        0.8    0.0    1.5
## 81  below_nest  Ssp158   FALSE   37      52.5        0.7   -0.1    1.6
## 82  below_nest  Ssp115   FALSE   38      52.0        0.7    0.2    1.3
## 83  below_nest  Ssp114   FALSE   39      48.0        0.7    0.1    1.2
## 84  below_nest   Ssp78   FALSE   40      47.5        0.7    0.1    1.3
## 85  below_nest   Ssp54   FALSE   41      44.0        0.6   -0.1    1.4
## 86  below_nest  Ssp189   FALSE   42      42.0        0.6   -0.6    1.8
## 87  below_nest  Ssp142   FALSE   43      41.0        0.6   -0.3    1.4
## 88  below_nest    Ssp7   FALSE   44      39.3        0.6   -0.2    1.3
## 89  below_nest  Ssp112   FALSE   45      39.0        0.5    0.0    1.1
## 90  below_nest   Ssp66   FALSE   46      38.4        0.5   -0.3    1.4
## 91  below_nest  Ssp192   FALSE   47      38.0        0.5   -0.5    1.6
## 92  below_nest   Ssp21   FALSE   48      38.0        0.5   -0.2    1.2
## 93  below_nest    Ssp6   FALSE   49      37.2        0.5    0.0    1.0
## 94  below_nest  Ssp129   FALSE   50      37.0        0.5    0.1    1.0
## 95  below_nest   Ssp55   FALSE   51      33.5        0.5    0.0    1.0
## 96  below_nest   Ssp25   FALSE   52      31.5        0.4    0.0    0.9
## 97  below_nest    Ssp1   FALSE   53      30.0        0.4   -0.2    1.0
## 98  below_nest  Ssp121   FALSE   54      29.2        0.4    0.0    0.9
## 99  below_nest    Ssp3   FALSE   55      29.0        0.4    0.0    0.9
## 100 below_nest  Ssp132   FALSE   56      27.0        0.4    0.0    0.8
## 101 below_nest    Ssp8   FALSE   57      26.0        0.4    0.0    0.7
## 102 below_nest  Ssp113   FALSE   58      25.0        0.4    0.0    0.7
## 103 below_nest   Ssp16   FALSE   59      25.0        0.4    0.0    0.7
## 104 below_nest   Ssp20   FALSE   60      24.2        0.3   -0.2    0.9
## 105 below_nest  Ssp141   FALSE   61      22.5        0.3    0.0    0.7
## 106 below_nest  Ssp139   FALSE   62      21.5        0.3    0.0    0.7
## 107 below_nest   Ssp94   FALSE   63      21.0        0.3   -0.1    0.7
## 108 below_nest  Ssp111   FALSE   64      21.0        0.3    0.0    0.6
## 109 below_nest   Ssp10   FALSE   65      20.0        0.3   -0.3    0.8
## 110 below_nest  Ssp100   FALSE   66      18.0        0.3    0.0    0.5
## 111 below_nest   Ssp57   FALSE   67      18.0        0.3   -0.2    0.7
## 112 below_nest  Ssp182   FALSE   68      18.0        0.3   -0.2    0.8
## 113 below_nest  Ssp160   FALSE   69      17.5        0.2    0.0    0.5
## 114 below_nest  Ssp186   FALSE   70      16.0        0.2    0.0    0.4
## 115 below_nest  Ssp188   FALSE   71      15.0        0.2   -0.2    0.6
## 116 below_nest  Ssp171   FALSE   72      14.0        0.2    0.0    0.4
## 117 below_nest  Ssp163   FALSE   73      14.0        0.2   -0.1    0.5
## 118 below_nest  Ssp181   FALSE   74      13.5        0.2   -0.1    0.5
## 119 below_nest  Ssp122   FALSE   75      13.3        0.2    0.0    0.4
## 120 below_nest    Ssp4   FALSE   76      13.0        0.2    0.0    0.4
## 121 below_nest   Ssp82   FALSE   77      13.0        0.2   -0.1    0.5
## 122 below_nest  Ssp133   FALSE   78      12.0        0.2    0.0    0.4
## 123 below_nest   Ssp19   FALSE   79      12.0        0.2   -0.1    0.4
## 124 below_nest   Ssp12   FALSE   80      12.0        0.2   -0.1    0.4
## 125 below_nest   Ssp45   FALSE   81      10.0        0.1   -0.1    0.4
## 126 below_nest   Ssp39   FALSE   82      10.0        0.1    0.0    0.3
## 127 below_nest  Ssp168   FALSE   83      10.0        0.1   -0.1    0.4
## 128 below_nest  Ssp164   FALSE   84      10.0        0.1   -0.1    0.4
## 129 below_nest  Ssp130   FALSE   85      10.0        0.1   -0.1    0.4
## 130 below_nest  Ssp123   FALSE   86       9.0        0.1   -0.1    0.3
## 131 below_nest   Ssp50   FALSE   87       9.0        0.1   -0.1    0.4
## 132 below_nest  Ssp179   FALSE   88       9.0        0.1   -0.1    0.3
## 133 below_nest   Ssp72   FALSE   89       8.0        0.1    0.0    0.2
## 134 below_nest  Ssp106   FALSE   90       8.0        0.1   -0.1    0.3
## 135 below_nest  Ssp167   FALSE   91       8.0        0.1   -0.1    0.3
## 136 below_nest    Ssp5   FALSE   92       7.5        0.1    0.0    0.3
## 137 below_nest  Ssp128   FALSE   93       7.5        0.1    0.0    0.2
## 138 below_nest   Ssp47   FALSE   94       7.0        0.1   -0.1    0.3
## 139 below_nest   Ssp85   FALSE   95       7.0        0.1    0.0    0.2
## 140 below_nest   Ssp15   FALSE   96       6.0        0.1    0.0    0.2
## 141 below_nest  Ssp169   FALSE   97       6.0        0.1   -0.1    0.3
## 142 below_nest   Ssp23   FALSE   98       6.0        0.1   -0.1    0.3
## 143 below_nest   Ssp77   FALSE   99       6.0        0.1    0.0    0.2
## 144 below_nest   Ssp64   FALSE  100       5.0        0.1   -0.1    0.2
## 145 below_nest   Ssp42   FALSE  101       5.0        0.1   -0.1    0.2
## 146 below_nest   Ssp67   FALSE  102       5.0        0.1   -0.1    0.2
## 147 below_nest   Ssp27   FALSE  103       5.0        0.1    0.0    0.2
## 148 below_nest   Ssp81   FALSE  104       4.0        0.1    0.0    0.1
## 149 below_nest  Ssp155   FALSE  105       4.0        0.1    0.0    0.1
## 150 below_nest  Ssp120   FALSE  106       4.0        0.1    0.0    0.1
## 151 below_nest   Ssp46   FALSE  107       4.0        0.1   -0.1    0.2
## 152 below_nest   Ssp88   FALSE  108       4.0        0.1    0.0    0.1
## 153 below_nest  Ssp107   FALSE  109       4.0        0.1    0.0    0.1
## 154 below_nest  Ssp136   FALSE  110       3.0        0.0    0.0    0.1
## 155 below_nest  Ssp172   FALSE  111       3.0        0.0    0.0    0.1
## 156 below_nest  Ssp157   FALSE  112       3.0        0.0    0.0    0.1
## 157 below_nest   Ssp96   FALSE  113       3.0        0.0    0.0    0.1
## 158 below_nest  Ssp127   FALSE  114       3.0        0.0    0.0    0.1
## 159 below_nest  Ssp125   FALSE  115       3.0        0.0    0.0    0.1
## 160 below_nest  Ssp148   FALSE  116       3.0        0.0    0.0    0.1
## 161 below_nest  Ssp109   FALSE  117       3.0        0.0    0.0    0.1
## 162 below_nest    Ssp9   FALSE  118       2.5        0.0    0.0    0.1
## 163 below_nest  Ssp116   FALSE  119       2.0        0.0    0.0    0.1
## 164 below_nest   Ssp30   FALSE  120       2.0        0.0    0.0    0.1
## 165 below_nest   Ssp24   FALSE  121       2.0        0.0    0.0    0.1
## 166 below_nest   Ssp44   FALSE  122       2.0        0.0    0.0    0.1
## 167 below_nest  Ssp144   FALSE  123       2.0        0.0    0.0    0.1
## 168 below_nest   Ssp56   FALSE  124       1.0        0.0    0.0    0.0
## 169 below_nest   Ssp68   FALSE  125       1.0        0.0    0.0    0.0
## 170 below_nest   Ssp53   FALSE  126       1.0        0.0    0.0    0.0
## 171 below_nest  Ssp131   FALSE  127       1.0        0.0    0.0    0.0
## 172 below_nest  Ssp126   FALSE  128       1.0        0.0    0.0    0.0
## 173 below_nest   Ssp28   FALSE  129       1.0        0.0    0.0    0.0
## 174 below_nest  Ssp178   FALSE  130       1.0        0.0    0.0    0.0
## 175 below_nest   Ssp48   FALSE  131       1.0        0.0    0.0    0.0
## 176 below_nest  Ssp124   FALSE  132       1.0        0.0    0.0    0.0
## 177 below_nest   Ssp98   FALSE  133       1.0        0.0    0.0    0.0
## 178 below_nest   Ssp76   FALSE  134       1.0        0.0    0.0    0.0
## 179 below_nest   Ssp40   FALSE  135       0.5        0.0    0.0    0.0
## 180 below_nest   Ssp93   FALSE  136       0.5        0.0    0.0    0.0
## 181 below_nest  Ssp110   FALSE  137       0.5        0.0    0.0    0.0
## 182 below_nest   Ssp83   FALSE  138       0.5        0.0    0.0    0.0
## 183 below_nest   Ssp99   FALSE  139       0.5        0.0    0.0    0.0
##     accumfreq logabun rankfreq
## 1        13.3     1.7      2.3
## 2        24.2     1.6      4.5
## 3        33.4     1.5      6.8
## 4        40.8     1.4      9.1
## 5        47.6     1.4     11.4
## 6        52.7     1.3     13.6
## 7        57.6     1.3     15.9
## 8        61.2     1.1     18.2
## 9        64.7     1.1     20.5
## 10       68.0     1.1     22.7
## 11       70.7     1.0     25.0
## 12       73.2     1.0     27.3
## 13       75.6     1.0     29.5
## 14       77.8     0.9     31.8
## 15       80.0     0.9     34.1
## 16       82.1     0.9     36.4
## 17       83.8     0.8     38.6
## 18       85.4     0.8     40.9
## 19       86.8     0.7     43.2
## 20       88.1     0.7     45.5
## 21       89.5     0.7     47.7
## 22       90.4     0.5     50.0
## 23       91.2     0.5     52.3
## 24       92.0     0.5     54.5
## 25       92.8     0.5     56.8
## 26       93.6     0.5     59.1
## 27       94.4     0.5     61.4
## 28       95.1     0.4     63.6
## 29       95.7     0.3     65.9
## 30       96.2     0.3     68.2
## 31       96.7     0.3     70.5
## 32       97.3     0.3     72.7
## 33       97.7     0.2     75.0
## 34       98.0     0.0     77.3
## 35       98.2     0.0     79.5
## 36       98.5     0.0     81.8
## 37       98.8     0.0     84.1
## 38       99.0     0.0     86.4
## 39       99.3     0.0     88.6
## 40       99.5    -0.3     90.9
## 41       99.6    -0.3     93.2
## 42       99.7    -0.3     95.5
## 43       99.9    -0.3     97.7
## 44      100.0    -0.3    100.0
## 45        6.0     2.6      0.7
## 46       11.6     2.6      1.4
## 47       16.8     2.6      2.2
## 48       21.6     2.5      2.9
## 49       25.6     2.4      3.6
## 50       28.6     2.3      4.3
## 51       31.6     2.3      5.0
## 52       34.4     2.3      5.8
## 53       37.2     2.3      6.5
## 54       39.7     2.3      7.2
## 55       42.3     2.3      7.9
## 56       44.7     2.2      8.6
## 57       47.1     2.2      9.4
## 58       49.2     2.2     10.1
## 59       51.2     2.2     10.8
## 60       53.2     2.1     11.5
## 61       55.1     2.1     12.2
## 62       57.0     2.1     12.9
## 63       58.6     2.1     13.7
## 64       60.3     2.1     14.4
## 65       61.9     2.1     15.1
## 66       63.4     2.0     15.8
## 67       64.9     2.0     16.5
## 68       66.4     2.0     17.3
## 69       67.8     2.0     18.0
## 70       69.2     2.0     18.7
## 71       70.4     2.0     19.4
## 72       71.7     1.9     20.1
## 73       72.9     1.9     20.9
## 74       74.1     1.9     21.6
## 75       75.3     1.9     22.3
## 76       76.2     1.8     23.0
## 77       77.2     1.8     23.7
## 78       78.1     1.8     24.5
## 79       78.9     1.7     25.2
## 80       79.6     1.7     25.9
## 81       80.4     1.7     26.6
## 82       81.1     1.7     27.3
## 83       81.8     1.7     28.1
## 84       82.5     1.7     28.8
## 85       83.1     1.6     29.5
## 86       83.7     1.6     30.2
## 87       84.2     1.6     30.9
## 88       84.8     1.6     31.7
## 89       85.3     1.6     32.4
## 90       85.9     1.6     33.1
## 91       86.4     1.6     33.8
## 92       87.0     1.6     34.5
## 93       87.5     1.6     35.3
## 94       88.0     1.6     36.0
## 95       88.5     1.5     36.7
## 96       88.9     1.5     37.4
## 97       89.3     1.5     38.1
## 98       89.7     1.5     38.8
## 99       90.1     1.5     39.6
## 100      90.5     1.4     40.3
## 101      90.9     1.4     41.0
## 102      91.2     1.4     41.7
## 103      91.6     1.4     42.4
## 104      91.9     1.4     43.2
## 105      92.3     1.4     43.9
## 106      92.6     1.3     44.6
## 107      92.9     1.3     45.3
## 108      93.1     1.3     46.0
## 109      93.4     1.3     46.8
## 110      93.7     1.3     47.5
## 111      93.9     1.3     48.2
## 112      94.2     1.3     48.9
## 113      94.4     1.2     49.6
## 114      94.7     1.2     50.4
## 115      94.9     1.2     51.1
## 116      95.1     1.1     51.8
## 117      95.3     1.1     52.5
## 118      95.5     1.1     53.2
## 119      95.6     1.1     54.0
## 120      95.8     1.1     54.7
## 121      96.0     1.1     55.4
## 122      96.2     1.1     56.1
## 123      96.3     1.1     56.8
## 124      96.5     1.1     57.6
## 125      96.7     1.0     58.3
## 126      96.8     1.0     59.0
## 127      96.9     1.0     59.7
## 128      97.1     1.0     60.4
## 129      97.2     1.0     61.2
## 130      97.3     1.0     61.9
## 131      97.5     1.0     62.6
## 132      97.6     1.0     63.3
## 133      97.7     0.9     64.0
## 134      97.8     0.9     64.7
## 135      97.9     0.9     65.5
## 136      98.0     0.9     66.2
## 137      98.1     0.9     66.9
## 138      98.2     0.8     67.6
## 139      98.3     0.8     68.3
## 140      98.4     0.8     69.1
## 141      98.5     0.8     69.8
## 142      98.6     0.8     70.5
## 143      98.7     0.8     71.2
## 144      98.7     0.7     71.9
## 145      98.8     0.7     72.7
## 146      98.9     0.7     73.4
## 147      99.0     0.7     74.1
## 148      99.0     0.6     74.8
## 149      99.1     0.6     75.5
## 150      99.1     0.6     76.3
## 151      99.2     0.6     77.0
## 152      99.2     0.6     77.7
## 153      99.3     0.6     78.4
## 154      99.3     0.5     79.1
## 155      99.4     0.5     79.9
## 156      99.4     0.5     80.6
## 157      99.5     0.5     81.3
## 158      99.5     0.5     82.0
## 159      99.6     0.5     82.7
## 160      99.6     0.5     83.5
## 161      99.6     0.5     84.2
## 162      99.7     0.4     84.9
## 163      99.7     0.3     85.6
## 164      99.7     0.3     86.3
## 165      99.8     0.3     87.1
## 166      99.8     0.3     87.8
## 167      99.8     0.3     88.5
## 168      99.8     0.0     89.2
## 169      99.8     0.0     89.9
## 170      99.9     0.0     90.6
## 171      99.9     0.0     91.4
## 172      99.9     0.0     92.1
## 173      99.9     0.0     92.8
## 174      99.9     0.0     93.5
## 175      99.9     0.0     94.2
## 176      99.9     0.0     95.0
## 177     100.0     0.0     95.7
## 178     100.0     0.0     96.4
## 179     100.0    -0.3     97.1
## 180     100.0    -0.3     97.8
## 181     100.0    -0.3     98.6
## 182     100.0    -0.3     99.3
## 183     100.0    -0.3    100.0
# now assign functional group and species ID  
RA_nest_withSPID <- merge(RA_nest, sp_codes, by.x = "species", by.y = "Number", all.x = TRUE)
write.csv(RA_poop_withSPID, "Output figs and tables/RankAbundance_bynestLocation.csv", row.names = F)

indicator species anlaysis note that this is will all the data - maybe should be face only

head(env)
##     Quad_Identifier Control_or_treatment Site_name Nest_Position
## 1  PIL_Nest_22_44_R                 Nest       PIL    above_nest
## 2  PIL_Nest_23_46_R                 Nest       PIL    below_nest
## 3 GOA_Nest_76_151_L                 Nest       GOA    below_nest
## 4  PIL_Nest_22_43_L                 Nest       PIL    above_nest
## 5 NRS_Nest_63_126_R                 Nest       NRS    below_nest
## 6  PIL_Nest_23_45_L                 Nest       PIL    below_nest
env_vector <- env_filtered$Control_or_treatment


indval_trt <- multipatt(all.matrix_filtered, env_vector, 
                    control = how(nperm=999)) 
summary(indval_trt, alpha = .1)
## 
##  Multilevel pattern analysis
##  ---------------------------
## 
##  Association function: IndVal.g
##  Significance level (alpha): 0.1
## 
##  Total number of species: 146
##  Selected number of species: 22 
##  Number of species associated to 1 group: 22 
## 
##  List of species associated to each combination: 
## 
##  Group Control  #sps.  11 
##         stat p.value    
## Ssp162 0.458   0.001 ***
## Ssp49  0.359   0.005 ** 
## Ssp26  0.352   0.020 *  
## Ssp114 0.336   0.010 ** 
## Ssp132 0.311   0.038 *  
## Ssp85  0.284   0.060 .  
## Ssp141 0.284   0.070 .  
## Ssp54  0.284   0.058 .  
## Ssp108 0.284   0.062 .  
## Ssp191 0.284   0.065 .  
## Ssp7   0.284   0.055 .  
## 
##  Group Nest  #sps.  11 
##         stat p.value   
## Ssp117 0.520   0.006 **
## Ssp159 0.411   0.050 * 
## Ssp3   0.319   0.011 * 
## Ssp186 0.319   0.011 * 
## Ssp32  0.313   0.030 * 
## Ssp137 0.313   0.077 . 
## Ssp149 0.292   0.068 . 
## Ssp55  0.291   0.026 * 
## Ssp139 0.285   0.085 . 
## Ssp4   0.280   0.059 . 
## Ssp8   0.260   0.046 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 
# 
# cover
# 
# cover_onlyTrt <- cover %>%
#   filter(Control_or_treatment == "Nest")
# 
# env_vector_nestpos <- cover_onlyTrt$Nest_Position
# 
# species_onlytrt <- cover_onlyTrt %>%
#   select(Ssp1:Ssp192)
# 
# 
# # 
# # 
# 
# # summary(env_vector)
# indval_nest <- multipatt(species_onlytrt, env_vector_nestpos, 
#                     control = how(nperm=999)) 
# summary(indval_nest, alpha = 1)

# indval$cluster
# indval$comb
# indval$str
# indval$A
# indval$B
# indval$sign

I saved this outside of R to clean it up. Lets bring it back in and add species ID and taxa

indic_sp<- read.csv("Intermediate data files/indic_sp_alpha0.1.csv")
indic_sp
##    Grouping Species  Stat P.values Sig
## 1   Control  Ssp162 0.458    0.001 ***
## 2   Control   Ssp49 0.359    0.005  **
## 3   Control   Ssp26 0.352    0.020   *
## 4   Control  Ssp114 0.336    0.010  **
## 5   Control  Ssp132 0.311    0.038   *
## 6   Control   Ssp85 0.284    0.060   .
## 7   Control  Ssp141 0.284    0.070   .
## 8   Control   Ssp54 0.284    0.058   .
## 9   Control  Ssp108 0.284    0.062   .
## 10  Control  Ssp191 0.284    0.065   .
## 11     Nest  Ssp117 0.520    0.006  **
## 12     Nest  Ssp159 0.411    0.050   *
## 13     Nest    Ssp3 0.319    0.011   *
## 14     Nest  Ssp186 0.319    0.011   *
## 15     Nest   Ssp32 0.313    0.030   *
## 16     Nest  Ssp137 0.313    0.077   .
## 17     Nest  Ssp149 0.292    0.068   .
## 18     Nest   Ssp55 0.291    0.026   *
## 19     Nest  Ssp139 0.285    0.085   .
## 20     Nest    Ssp4 0.280    0.059   .
indic_sp_with_spID <- merge(indic_sp, sp_codes, by.x = "Species", by.y = "Number", all.x = TRUE)
indic_sp_with_spID
##    Species Grouping  Stat P.values Sig                       Species.y taxa
## 1   Ssp108  Control 0.284    0.062   .                     Lecidea sp.    L
## 2   Ssp114  Control 0.336    0.010  **       Lepraria sp. "Light Grey"    L
## 3   Ssp117     Nest 0.520    0.006  ** Lepraria sp. ("Green Lepraria")    L
## 4   Ssp132  Control 0.311    0.038   *            Myelochroa aurulenta    L
## 5   Ssp137     Nest 0.313    0.077   .            Parmelia omphalodes     L
## 6   Ssp139     Nest 0.285    0.085   .           Parmelinopsis minarum    L
## 7   Ssp141  Control 0.284    0.070   .             Parmotrema crinitum    L
## 8   Ssp149     Nest 0.292    0.068   .                  Physcia caesia    L
## 9   Ssp159     Nest 0.411    0.050   *             Ramalina intermedia    L
## 10  Ssp162  Control 0.458    0.001 ***                 Rhizocarpon sp.    L
## 11  Ssp186     Nest 0.319    0.011   *                       Usnea sp.    L
## 12  Ssp191  Control 0.284    0.065   .                     Woodsia sp.    V
## 13   Ssp26  Control 0.352    0.020   *                     "Tan Crust"    L
## 14    Ssp3     Nest 0.319    0.011   *                   "Black Crust"    L
## 15   Ssp32     Nest 0.313    0.030   *              "White-Grey Crust"    L
## 16    Ssp4     Nest 0.280    0.059   .                 "Black Crust 2"    L
## 17   Ssp49  Control 0.359    0.005  ** Buellia sp. "Black Hypothallus"    L
## 18   Ssp54  Control 0.284    0.058   .          Bryum cf. caespiticium    B
## 19   Ssp55     Nest 0.291    0.026   *               Caloplaca citrina    L
## 20   Ssp85  Control 0.284    0.060   .                   Dimelaena sp.    L
##    Notes
## 1       
## 2       
## 3       
## 4       
## 5       
## 6       
## 7       
## 8      x
## 9       
## 10      
## 11      
## 12      
## 13      
## 14      
## 15      
## 16      
## 17      
## 18      
## 19     x
## 20
write.csv(indic_sp_with_spID, "Output figs and tables/Indicator species by treatment.csv", row.names = F)