Data and Libraries

These are the libraries that I used throughout the project. I was going to use BiodiveristyR, but it consistently crashed everytime I tried to load it. I added knitr and kableExtra to what I was going to use in order to make tables and edit them to look better. I chose the Cary site which is in New York and the beech/maple forest in Heusto which is in Ohio. I cleaned the original data by removing a bunch of columns, removing null values, and pivoting the table.

library(readxl)
library(tidyverse)
library(knitr)
library(kableExtra)

cary<-read_xls("Transect_Data_Set/temp.namerica/CARY.xls")
hbm<-read_xls("Transect_Data_Set/temp.namerica/HEUSTOBM.xls")

ccary<- cary %>%
  select(-voucher...5,-voucher...6,-voucher...7,-voucher...8,-voucher...9,    
         -voucher...10,-voucher...11,-voucher...12,-Liana, -"N(Ind.)") %>%      
  pivot_longer(cols = starts_with("Stemdbh"), 
               values_to = "Stemdbh") %>%
  filter(!is.na(Stemdbh)) %>%
  select(-name)


chbm<- hbm %>%
  select(-voucher...5,-voucher...6,-voucher...7,-voucher...8,-voucher...9,
         -voucher...10,-voucher...11,-voucher...12,-Liana, -"N(Ind.)") %>%
  pivot_longer(cols = starts_with("Stemdbh"), 
               values_to = "Stemdbh") %>%
  filter(!is.na(Stemdbh)) %>%
  select(-name)

Functions Created

I created 3 functions overall where the first one separates the data by transect line, the second is used to graph the DBH measurements for the transect lines, and the last one is used to calculate the D value for a species within a transect line.

seplines<-function(x, num){      #function to separate df by lines in order to graph them by lines
  filter(x, Line== num)
}


dbhgraph<-function(x,name){                            #function to graph the dbh of individuals and by species
  ggplot(x, aes(Species, Stemdbh, fill =  Species)) +
    geom_bar(stat = "identity", position = "dodge2") +
    labs(x = "Species", y = "Stem DBH, cm", title= name) +
     theme(axis.text.x = element_text(angle = 45, hjust = 1),
           panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_blank())
  
}
calcD<- function(n,N){     #calculates the D value
  (n/N)^2
}

Biodiversity calculations and tables

These calculations are the first ones that should be done in order to get the number of individuals in each species and Line and to get the total number of individuals, regardless of species, per line. These values are used in the calculations of the indices ahead.

n_count <- ccary %>%       #individuals per species         CARY 
  group_by(Line, Species) %>%
  summarize(n = n()) %>%
  ungroup()

N_count <- ccary %>%    #total individuals per line
  group_by(Line) %>%
  summarize(N = n()) %>%
  ungroup()

nandN<-left_join(n_count,N_count, by=c("Line"="Line"))

n_count2 <- chbm %>%       #indivduals per species          HEUSTO
group_by(Line, Species) %>%
  summarize(n = n()) %>%
  ungroup()

N_count2 <- chbm %>%    #total individuals per line
  group_by(Line) %>%
  summarize(N = n()) %>%
  ungroup()

nandN2<-left_join(n_count2,N_count2, by=c("Line"="Line"))

The indices for each transect within a site are calculated and arranged by the line number. Tables using the kable function are then created from the dataframes created for each site.

withD <- nandN %>%
  mutate(D = calcD(n,N),                                                
         ndivN= n/N,             
         lnndivN= log(n/N)) %>%            
  mutate(befH= ndivN * lnndivN)            


sumD<- withD %>%                      #CARY site indices per transect
  group_by(Line) %>%
  summarize(sumD= round(sum(D),3),
            sumbefH= round(sum(befH),3),
            totspecies= n_distinct(Species)) %>%
  mutate(Index_of_Div= round(1-sumD,3),
         Recip_Index= round(1 / sumD, 3)) %>%
  mutate(ESimp= round(Recip_Index/totspecies,3),
         H= round(-1 * sumbefH,3)) %>%
  mutate(EShan= round(H/log(totspecies),3)) %>%
  arrange(as.numeric(Line)) %>%
  mutate(Site="Cary") %>%
  select(-sumbefH)


withD2 <- nandN2 %>%
  mutate(D = calcD(n,N),  
         ndivN= n/N,
         lnndivN= log(n/N)) %>%
  mutate(befH= ndivN * lnndivN)    


sumD2<- withD2 %>%                    #HEUSTO site indices per transect
  group_by(Line) %>%
  summarize(sumD= round(sum(D),3),
            sumbefH= round(sum(befH),3),
            totspecies= n_distinct(Species)) %>%
  mutate(Index_of_Div= round(1-sumD,3),
         Recip_Index= round(1/sumD,3)) %>%
  mutate(ESimp= round(Recip_Index/totspecies,3),
         H= round(-1 * sumbefH,3)) %>%
  mutate(EShan= round(H/log(totspecies),3)) %>%
  arrange(as.numeric(Line)) %>%
  mutate(Site="Heusto B/M") %>%
  select(-sumbefH)

names<-c("Line","Simpson's Index","Total Species","Index of Diversity","Reciprocal Index", "Simpson's Eveness", "Shannon Index","Shannon Eveness","Site")

carytable<-kable(sumD, format="pipe", col.name=names, align=c("lcccccccr"))

kable(sumD, format="pipe", col.name=names, align=c("lcccccccr"))
Line Simpson’s Index Total Species Index of Diversity Reciprocal Index Simpson’s Eveness Shannon Index Shannon Eveness Site
1 0.240 6 0.760 4.167 0.695 1.594 0.890 Cary
2 0.183 10 0.817 5.464 0.546 2.007 0.872 Cary
3 0.194 7 0.806 5.155 0.736 1.792 0.921 Cary
4 0.156 9 0.844 6.410 0.712 2.014 0.917 Cary
5 0.278 4 0.722 3.597 0.899 1.330 0.959 Cary
6 0.388 4 0.612 2.577 0.644 1.154 0.832 Cary
7 0.247 6 0.753 4.049 0.675 1.565 0.873 Cary
8 0.190 9 0.810 5.263 0.585 1.891 0.861 Cary
9 0.188 6 0.812 5.319 0.886 1.733 0.967 Cary
10 0.180 8 0.820 5.556 0.695 1.890 0.909 Cary
heustobmtable<- kable(sumD2, format="pipe", col.name=names, align=c("lcccccccr"))

kable(sumD2, format="pipe", col.name=names, align=c("lcccccccr"))
Line Simpson’s Index Total Species Index of Diversity Reciprocal Index Simpson’s Eveness Shannon Index Shannon Eveness Site
1 0.265 5 0.735 3.774 0.755 1.433 0.890 Heusto B/M
2 0.194 6 0.806 5.155 0.859 1.697 0.947 Heusto B/M
3 0.240 5 0.760 4.167 0.833 1.505 0.935 Heusto B/M
4 0.344 4 0.656 2.907 0.727 1.213 0.875 Heusto B/M
5 0.184 6 0.816 5.435 0.906 1.748 0.976 Heusto B/M
6 0.200 6 0.800 5.000 0.833 1.696 0.947 Heusto B/M
7 0.329 6 0.671 3.040 0.507 1.425 0.795 Heusto B/M
8 0.253 8 0.747 3.953 0.494 1.706 0.820 Heusto B/M
9 0.347 3 0.653 2.882 0.961 1.079 0.982 Heusto B/M
10 0.243 8 0.757 4.115 0.514 1.681 0.808 Heusto B/M

The average of the indices for the two sites are calculated and joined in a dataframe which is then made into a table.

avgsite<- sumD %>%              #table with indices for the two sites
 rbind.data.frame(sumD2) %>%
  group_by(Site) %>%
  summarize(D=round(mean(sumD),3),
  "1-D"=round(mean(Index_of_Div),3),
  "D'"=round(mean(Recip_Index),3),
  ESimpson=round(mean(ESimp),3),
  H=round(mean(H),3),
  EShannon=round(mean(EShan),3))
  

avgsitetable<- kable(avgsite, format="pipe", col.name=c("Site","Simpson's Index","Index of Diversity","Reciprocal Index","Simpson's Eveness","Shannon Index","Shannon Eveness"),align=c("lcccccr"))

kable(avgsite, format="pipe", col.name=c("Site","Simpson's Index","Index of Diversity","Reciprocal Index","Simpson's Eveness","Shannon Index","Shannon Eveness"),align=c("lcccccr"))
Site Simpson’s Index Index of Diversity Reciprocal Index Simpson’s Eveness Shannon Index Shannon Eveness
Cary 0.224 0.776 4.756 0.707 1.697 0.900
Heusto B/M 0.260 0.740 4.043 0.739 1.518 0.897

Graphs of Most and Least Diverse Transects

Using the index of diversity results from the transect calcualtions above, the DBH graphs of the transect with the highest and lowest index are created. Transect Line 4 and 10 from the Cary and Heusto sites,respectively, are the most diverse according to the index of diversity. Cary transect line 6 and Heusto transect line 9 are the least diverse according to the same metrics.

cary4<-seplines(ccary,4)
dbhgraph(cary4, "Cary: Stem DBH per Species, Line 4")

cary6<-seplines(ccary,6)
dbhgraph(cary6, "Cary: Stem DBH per Species, Line 6")

hbm5<-seplines(chbm,5)
dbhgraph(hbm5, "HBM: Stem DBH per Species, Line 5")  

hbm9<-seplines(chbm,9)
dbhgraph(hbm9, "HBM: Stem DBH per Species, Line 9")