require(readxl);
require(janitor); 
require(tidyverse); 
require(ggplot2); 
require(ggpubr);
require(vegan); 
require(gridExtra);
require(knitr)
require(sf)
getwd()
## [1] "C:/Users/knoll/Documents/RGTECH/Biodiversity Project"
#import datasets 
tidroute<-read_xls("C:/Users/knoll/Documents/RGTECH/Biodiversity Project/Data/TIDROUTE.xls")%>%clean_names()
## New names:
## • `voucher` -> `voucher...5`
## • `voucher` -> `voucher...6`
## • `voucher` -> `voucher...7`
## • `voucher` -> `voucher...8`
## • `voucher` -> `voucher...9`
## • `voucher` -> `voucher...10`
## • `voucher` -> `voucher...11`
## • `voucher` -> `voucher...12`
## • `Stemdbh` -> `Stemdbh...15`
## • `Stemdbh` -> `Stemdbh...16`
## • `Stemdbh` -> `Stemdbh...17`
## • `Stemdbh` -> `Stemdbh...18`
## • `Stemdbh` -> `Stemdbh...19`
## • `Stemdbh` -> `Stemdbh...20`
## • `Stemdbh` -> `Stemdbh...21`
## • `Stemdbh` -> `Stemdbh...22`
## • `Stemdbh` -> `Stemdbh...23`
## • `Stemdbh` -> `Stemdbh...24`
## • `Stemdbh` -> `Stemdbh...25`
wildbasin<-read_xls("C:/Users/knoll/Documents/RGTECH/Biodiversity Project/Data/WILDBASI.xls")%>%clean_names()
## New names:
## • `voucher` -> `voucher...5`
## • `voucher` -> `voucher...6`
## • `voucher` -> `voucher...7`
## • `voucher` -> `voucher...8`
## • `voucher` -> `voucher...9`
## • `voucher` -> `voucher...10`
## • `voucher` -> `voucher...11`
## • `voucher` -> `voucher...12`
## • `Stemdbh` -> `Stemdbh...15`
## • `Stemdbh` -> `Stemdbh...16`
## • `Stemdbh` -> `Stemdbh...17`
## • `Stemdbh` -> `Stemdbh...18`
## • `Stemdbh` -> `Stemdbh...19`
## • `Stemdbh` -> `Stemdbh...20`
## • `Stemdbh` -> `Stemdbh...21`
## • `Stemdbh` -> `Stemdbh...22`
## • `Stemdbh` -> `Stemdbh...23`
## • `Stemdbh` -> `Stemdbh...24`
## • `Stemdbh` -> `Stemdbh...25`
## • `Stemdbh` -> `Stemdbh...26`
## • `Stemdbh` -> `Stemdbh...27`
## • `Stemdbh` -> `Stemdbh...28`
## • `Stemdbh` -> `Stemdbh...29`
## • `Stemdbh` -> `Stemdbh...30`
## • `Stemdbh` -> `Stemdbh...31`
## • `Stemdbh` -> `Stemdbh...32`
## • `Stemdbh` -> `Stemdbh...33`
## • `Stemdbh` -> `Stemdbh...34`
#clean data 
tidroute_new<-tidroute[, -c(5:12)]
wildbasin_new<-wildbasin[, -c(5:12)] 

class(tidroute_new)  
## [1] "tbl_df"     "tbl"        "data.frame"
tidroute_df<-as.data.frame(tidroute_new)%>%unite("Scientific_Name",genus:species,sep=" ",remove=FALSE)%>%replace(is.na(.), 0)

tidroute_df$line<-as.numeric(as.character(tidroute_df$line))
sapply(tidroute_df,class)
##            line          family Scientific_Name           genus         species 
##       "numeric"     "character"     "character"     "character"     "character" 
##           liana           n_ind      stemdbh_15      stemdbh_16      stemdbh_17 
##     "character"       "numeric"       "numeric"       "numeric"       "numeric" 
##      stemdbh_18      stemdbh_19      stemdbh_20      stemdbh_21      stemdbh_22 
##       "numeric"       "numeric"       "numeric"       "numeric"       "numeric" 
##      stemdbh_23      stemdbh_24      stemdbh_25 
##       "numeric"       "numeric"       "numeric"
class(wildbasin_new)
## [1] "tbl_df"     "tbl"        "data.frame"
wildbasin_df<-as.data.frame(wildbasin_new)%>%unite("Scientific_Name",genus:species,sep=" ",remove=FALSE)%>%replace(is.na(.), 0)

wildbasin_df$line<-as.numeric(as.character(wildbasin_df$line))
sapply(wildbasin_df,class)
##            line          family Scientific_Name           genus         species 
##       "numeric"     "character"     "character"     "character"     "character" 
##           liana           n_ind      stemdbh_15      stemdbh_16      stemdbh_17 
##     "character"       "numeric"       "numeric"       "numeric"       "numeric" 
##      stemdbh_18      stemdbh_19      stemdbh_20      stemdbh_21      stemdbh_22 
##       "numeric"       "numeric"       "numeric"       "numeric"       "numeric" 
##      stemdbh_23      stemdbh_24      stemdbh_25      stemdbh_26      stemdbh_27 
##       "numeric"       "numeric"       "numeric"       "numeric"       "numeric" 
##      stemdbh_28      stemdbh_29      stemdbh_30      stemdbh_31      stemdbh_32 
##       "numeric"       "numeric"       "numeric"       "numeric"       "numeric" 
##      stemdbh_33      stemdbh_34 
##       "numeric"       "numeric"
############Tidroute Stem DBH Plots########################################

#Transect 1
tid_T1_pivot<-tidroute_df%>%filter(line=='1')%>%pivot_longer(cols=stemdbh_15:stemdbh_17,names_to='stem_id',values_to ='stem_dbh' )

Tidroute_T1_plot<-ggplot(tid_T1_pivot, aes(x=Scientific_Name,y=stem_dbh,fill=stem_id))+ 
  geom_col(position='dodge')+
  ggtitle('Transect 1')+
  xlab("Scientific Name")+
  ylab('Stem DBH Measurement(cm)')+ 
  theme(legend.title.position = "top",plot.title = element_text(hjust=0.5))+
  labs(fill="Stem Number ID")

#Transect 2 

tid_T2_pivot<-tidroute_df%>%filter(line=='2')%>%pivot_longer(cols=stemdbh_15:stemdbh_19,names_to='stem_id',values_to ='stem_dbh')

Tidroute_T2_plot<-ggplot(tid_T2_pivot, aes(x=Scientific_Name,y=stem_dbh,fill=stem_id))+ 
  geom_col(position='dodge')+
  ggtitle('Transect 2')+
  xlab("Scientific Name")+
  ylab('Stem DBH Measurement(cm)')+ 
  theme(legend.title.position = "top",plot.title = element_text(hjust=0.5))+
  labs(fill="Stem Number ID") 

#Transect 3
tid_T3_pivot<-tidroute_df%>%filter(line=='3')%>%pivot_longer(cols=stemdbh_15:stemdbh_18,names_to='stem_id',values_to ='stem_dbh')

Tidroute_T3_plot<-ggplot(tid_T3_pivot, aes(x=Scientific_Name,y=stem_dbh,fill=stem_id))+ 
  geom_col(position='dodge')+
  ggtitle('Transect 3')+
  xlab("Scientific Name")+
  ylab('Stem DBH Measurement(cm)')+ 
  theme(legend.title.position = "top",plot.title = element_text(hjust=0.5))+
  labs(fill="Stem Number ID") 

#Transect 4
tid_T4_pivot<-tidroute_df%>%filter(line=='4')%>%pivot_longer(cols=stemdbh_15:stemdbh_17,names_to='stem_id',values_to ='stem_dbh')

Tidroute_T4_plot<-ggplot(tid_T4_pivot, aes(x=Scientific_Name,y=stem_dbh,fill=stem_id))+ 
  geom_col(position='dodge')+
  ggtitle('Transect 4')+
  xlab("Scientific Name")+
  ylab('Stem DBH Measurement(cm)')+ 
  theme(legend.title.position = "top",plot.title = element_text(hjust=0.5))+
  labs(fill="Stem Number ID") 

#Transect 5  
tid_T5_pivot<-tidroute_df%>%filter(line=='5')%>%pivot_longer(cols=stemdbh_15:stemdbh_16,names_to='stem_id',values_to ='stem_dbh')

Tidroute_T5_plot<-ggplot(tid_T5_pivot, aes(x=Scientific_Name,y=stem_dbh,fill=stem_id))+ 
  geom_col(position='dodge')+
  ggtitle('Transect 5')+
  xlab("Scientific Name")+
  ylab('Stem DBH Measurement(cm)')+ 
  theme(legend.title.position = "top",plot.title = element_text(hjust=0.5))+
  labs(fill="Stem Number ID") 

#Transect 6  
tid_T6_pivot<-tidroute_df%>%filter(line=='6')%>%pivot_longer(cols=stemdbh_15:stemdbh_25,names_to='stem_id',values_to ='stem_dbh')

Tidroute_T6_plot<-ggplot(tid_T6_pivot, aes(x=Scientific_Name,y=stem_dbh,fill=stem_id))+ 
  geom_col(position='dodge')+
  ggtitle('Transect 6')+
  xlab("Scientific Name")+
  ylab('Stem DBH Measurement(cm)')+ 
  theme(legend.title.position = "top",plot.title = element_text(hjust=0.5))+
  labs(fill="Stem Number ID") 

#Transect 7 
tid_T7_pivot<-tidroute_df%>%filter(line=='7')%>%pivot_longer(cols=stemdbh_15:stemdbh_21,names_to='stem_id',values_to ='stem_dbh')

Tidroute_T7_plot<-ggplot(tid_T7_pivot, aes(x=Scientific_Name,y=stem_dbh,fill=stem_id))+ 
  geom_col(position='dodge')+
  ggtitle('Transect 7')+
  xlab("Scientific Name")+
  ylab('Stem DBH Measurement(cm)')+ 
  theme(legend.title.position = "top",plot.title = element_text(hjust=0.5))+
  labs(fill="Stem Number ID") 

#Transect 8 
tid_T8_pivot<-tidroute_df%>%filter(line=='8')%>%pivot_longer(cols=stemdbh_15:stemdbh_17,names_to='stem_id',values_to ='stem_dbh')

Tidroute_T8_plot<-ggplot(tid_T8_pivot, aes(x=Scientific_Name,y=stem_dbh,fill=stem_id))+ 
  geom_col(position='dodge')+
  ggtitle('Transect 8')+
  xlab("Scientific Name")+
  ylab('Stem DBH Measurement(cm)')+ 
  theme(legend.title.position = "top",plot.title = element_text(hjust=0.5))+
  labs(fill="Stem Number ID")

#Transect 9
tid_T9_pivot<-tidroute_df%>%filter(line=='9')%>%pivot_longer(cols=stemdbh_15:stemdbh_18,names_to='stem_id',values_to ='stem_dbh')

Tidroute_T9_plot<-ggplot(tid_T9_pivot, aes(x=Scientific_Name,y=stem_dbh,fill=stem_id))+ 
  geom_col(position='dodge')+
  ggtitle('Transect 9')+
  xlab("Scientific Name")+
  ylab('Stem DBH Measurement(cm)')+ 
  theme(legend.title.position = "top",plot.title = element_text(hjust=0.5))+
  labs(fill="Stem Number ID")

#Transect 10
tid_T10_pivot<-tidroute_df%>%filter(line=='10')%>%pivot_longer(cols=stemdbh_15:stemdbh_18,names_to='stem_id',values_to ='stem_dbh')

Tidroute_T10_plot<-ggplot(tid_T10_pivot, aes(x=Scientific_Name,y=stem_dbh,fill=stem_id))+ 
  geom_col(position='dodge')+
  ggtitle('Transect 10')+
  xlab("Scientific Name")+
  ylab('Stem DBH Measurement(cm)')+ 
  theme(legend.title.position = "top",plot.title = element_text(hjust=0.5))+
  labs(fill="Stem Number ID")


grobs<-list(Tidroute_T1_plot,Tidroute_T2_plot,Tidroute_T3_plot,
Tidroute_T4_plot,Tidroute_T5_plot,Tidroute_T6_plot,
Tidroute_T7_plot,Tidroute_T8_plot,Tidroute_T9_plot,
Tidroute_T10_plot)

Tidroute_graphs<-marrangeGrob(grobs = grobs,ncol=1,nrow=1)


ggexport(Tidroute_graphs,"Tidroute_graphs.pdf",width=18,height=10)
## file saved to ohmpjxs.pdf
grobs
## [[1]]
Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

## 
## [[2]]
Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

## 
## [[3]]
Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

## 
## [[4]]
Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

## 
## [[5]]
Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

## 
## [[6]]
Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

## 
## [[7]]
Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

## 
## [[8]]
Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

## 
## [[9]]
Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

## 
## [[10]]
Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

Bar plot illustrating the stem dbh measurements of each individual of each species found in the transect.

#Tidroute Indices

#Transect 1###################################################################
#S=species richness 
#S=specnumber() 

tid_T1_D<-tidroute_df%>%filter(line=='1')

tid_T1_count<-specnumber(tid_T1_D$Scientific_Name)
print(tid_T1_count) 
## [1] 5
##Simpsons Diversity 1-D 

tid_T1_SimD<-diversity(tid_T1_D$n_ind,'simpson')
print(tid_T1_SimD) 
## [1] 0.75
#Simpsons Index

tid_T1_sim.index<-(tid_T1_SimD-1)/-1
print(tid_T1_sim.index)
## [1] 0.25
##Simpsons Reciprocal 1/D (D')
tid_T1_SimD_recip<-diversity(tid_T1_D$n_ind,"inv")
print(tid_T1_SimD_recip) 
## [1] 4
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

Tid_T1_SimpsonEM<-(tid_T1_SimD_recip/tid_T1_count)
print(Tid_T1_SimpsonEM)
## [1] 0.8
##Shannon Diversity Index 
tid_T1_shannon<-diversity(tid_T1_D$n_ind,"shannon") 
print(tid_T1_shannon)
## [1] 1.494175
##Shannon's evenness H/(ln(S)) 

tid_T1_shannon.evenness<-tid_T1_shannon/(log(tid_T1_count))
print(tid_T1_shannon.evenness)
## [1] 0.9283832
#Transect 2###################################################################

#S=species richness 
#S=specnumber() 

tid_T2_D<-tidroute_df%>%filter(line=='2')

tid_T2_count<-specnumber(tid_T2_D$Scientific_Name)
print(tid_T2_count)
## [1] 4
##Simpsons Diversity 1-D 

tid_T2_SimD<-diversity(tid_T2_D$n_ind,'simpson')
print(tid_T2_SimD) 
## [1] 0.6122449
#Simpsons Index

tid_T2_sim.index<-(tid_T2_SimD-1)/-1
print(tid_T2_sim.index)
## [1] 0.3877551
##Simpsons Reciprocal 1/D (D')
tid_T2_SimD_recip<-diversity(tid_T2_D$n_ind,"inv")
print(tid_T2_SimD_recip) 
## [1] 2.578947
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

Tid_T2_SimpsonEM<-(tid_T2_SimD_recip/tid_T2_count)
print(Tid_T2_SimpsonEM)
## [1] 0.6447368
##Shannon Diversity Index 
tid_T2_shannon<-diversity(tid_T2_D$n_ind,"shannon") 
print(tid_T2_shannon)
## [1] 1.153742
##Shannon's evenness H/(ln(S)) 

tid_T2_shannon.evenness<-tid_T2_shannon/(log(tid_T2_count))
print(tid_T2_shannon.evenness)
## [1] 0.8322489
#Transect 3################################################################### 

#S=species richness 
#S=specnumber() 

tid_T3_D<-tidroute_df%>%filter(line=='3')

tid_T3_count<-specnumber(tid_T3_D$Scientific_Name)
print(tid_T3_count)
## [1] 5
##Simpsons Diversity 1-D 

tid_T3_SimD<-diversity(tid_T3_D$n_ind,'simpson')
print(tid_T3_SimD) 
## [1] 0.6875
#Simpsons Index

tid_T3_sim.index<-(tid_T3_SimD-1)/-1
print(tid_T3_sim.index)
## [1] 0.3125
##Simpsons Reciprocal 1/D (D')
tid_T3_SimD_recip<-diversity(tid_T3_D$n_ind,"inv")
print(tid_T3_SimD_recip) 
## [1] 3.2
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

Tid_T3_SimpsonEM<-(tid_T3_SimD_recip/tid_T3_count)
print(Tid_T3_SimpsonEM)
## [1] 0.64
##Shannon Diversity Index 
tid_T3_shannon<-diversity(tid_T3_D$n_ind,"shannon") 
print(tid_T3_shannon)
## [1] 1.386294
##Shannon's evenness H/(ln(S)) 

tid_T3_shannon.evenness<-tid_T3_shannon/(log(tid_T3_count))
print(tid_T3_shannon.evenness) 
## [1] 0.8613531
#Transect 4###################################################################  

#S=species richness 
#S=specnumber() 

tid_T4_D<-tidroute_df%>%filter(line=='4')

tid_T4_count<-specnumber(tid_T4_D$Scientific_Name)
print(tid_T4_count)
## [1] 3
##Simpsons Diversity 1-D 

tid_T4_SimD<-diversity(tid_T4_D$n_ind,'simpson')
print(tid_T4_SimD) 
## [1] 0.6122449
#Simpsons Index

tid_T4_sim.index<-(tid_T4_SimD-1)/-1
print(tid_T4_sim.index)
## [1] 0.3877551
##Simpsons Reciprocal 1/D (D')
tid_T4_SimD_recip<-diversity(tid_T4_D$n_ind,"inv")
print(tid_T4_SimD_recip) 
## [1] 2.578947
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

Tid_T4_SimpsonEM<-(tid_T4_SimD_recip/tid_T4_count)
print(Tid_T4_SimpsonEM)
## [1] 0.8596491
##Shannon Diversity Index 
tid_T4_shannon<-diversity(tid_T4_D$n_ind,"shannon") 
print(tid_T4_shannon)
## [1] 1.004242
##Shannon's evenness H/(ln(S)) 

tid_T4_shannon.evenness<-tid_T4_shannon/(log(tid_T4_count))
print(tid_T4_shannon.evenness) 
## [1] 0.9141009
#Transect 5###################################################################  

#S=species richness 
#S=specnumber() 

tid_T5_D<-tidroute_df%>%filter(line=='5')

tid_T5_count<-specnumber(tid_T5_D$Scientific_Name)
print(tid_T5_count)
## [1] 9
##Simpsons Diversity 1-D 

tid_T5_SimD<-diversity(tid_T5_D$n_ind,'simpson')
print(tid_T5_SimD) 
## [1] 0.88
#Simpsons Index

tid_T5_sim.index<-(tid_T5_SimD-1)/-1
print(tid_T5_sim.index)
## [1] 0.12
##Simpsons Reciprocal 1/D (D')
tid_T5_SimD_recip<-diversity(tid_T5_D$n_ind,"inv")
print(tid_T5_SimD_recip) 
## [1] 8.333333
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

Tid_T5_SimpsonEM<-(tid_T5_SimD_recip/tid_T5_count)
print(Tid_T5_SimpsonEM)
## [1] 0.9259259
##Shannon Diversity Index 
tid_T5_shannon<-diversity(tid_T5_D$n_ind,"shannon") 
print(tid_T5_shannon)
## [1] 2.163956
##Shannon's evenness H/(ln(S)) 

tid_T5_shannon.evenness<-tid_T5_shannon/(log(tid_T5_count))
print(tid_T5_shannon.evenness) 
## [1] 0.9848587
#Transect 6###################################################################  

#S=species richness 
#S=specnumber() 

tid_T6_D<-tidroute_df%>%filter(line=='6')

tid_T6_count<-specnumber(tid_T6_D$Scientific_Name)
print(tid_T6_count)
## [1] 4
##Simpsons Diversity 1-D 

tid_T6_SimD<-diversity(tid_T6_D$n_ind,'simpson')
print(tid_T6_SimD)
## [1] 0.4591837
#Simpsons Index

tid_T6_sim.index<-(tid_T6_SimD-1)/-1
print(tid_T6_sim.index)
## [1] 0.5408163
##Simpsons Reciprocal 1/D (D')
tid_T6_SimD_recip<-diversity(tid_T6_D$n_ind,"inv")
print(tid_T6_SimD_recip) 
## [1] 1.849057
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

Tid_T6_SimpsonEM<-(tid_T6_SimD_recip/tid_T6_count)
print(Tid_T6_SimpsonEM)
## [1] 0.4622642
##Shannon Diversity Index 
tid_T6_shannon<-diversity(tid_T6_D$n_ind,"shannon") 
print(tid_T6_shannon)
## [1] 0.8953327
##Shannon's evenness H/(ln(S)) 

tid_T6_shannon.evenness<-tid_T6_shannon/(log(tid_T6_count))
print(tid_T6_shannon.evenness)  
## [1] 0.645846
#Transect 7###################################################################  

#S=species richness 
#S=specnumber() 

tid_T7_D<-tidroute_df%>%filter(line=='7')

tid_T7_count<-specnumber(tid_T7_D$Scientific_Name)
print(tid_T7_count)
## [1] 7
##Simpsons Diversity 1-D 

tid_T7_SimD<-diversity(tid_T7_D$n_ind,'simpson')
print(tid_T7_SimD) 
## [1] 0.6745562
#Simpsons Index

tid_T7_sim.index<-(tid_T7_SimD-1)/-1
print(tid_T7_sim.index)
## [1] 0.3254438
##Simpsons Reciprocal 1/D (D')
tid_T7_SimD_recip<-diversity(tid_T7_D$n_ind,"inv")
print(tid_T7_SimD_recip) 
## [1] 3.072727
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

Tid_T7_SimpsonEM<-(tid_T7_SimD_recip/tid_T7_count)
print(Tid_T7_SimpsonEM)
## [1] 0.438961
##Shannon Diversity Index 
tid_T7_shannon<-diversity(tid_T7_D$n_ind,"shannon") 
print(tid_T7_shannon)
## [1] 1.517152
##Shannon's evenness H/(ln(S)) 

tid_T7_shannon.evenness<-tid_T7_shannon/(log(tid_T7_count))
print(tid_T7_shannon.evenness) 
## [1] 0.7796617
#Transect 8###################################################################  

#S=species richness 
#S=specnumber() 

tid_T8_D<-tidroute_df%>%filter(line=='8')

tid_T8_count<-specnumber(tid_T8_D$Scientific_Name)
print(tid_T8_count)
## [1] 6
##Simpsons Diversity 1-D 

tid_T8_SimD<-diversity(tid_T8_D$n_ind,'simpson')
print(tid_T8_SimD) 
## [1] 0.78125
#Simpsons Index

tid_T8_sim.index<-(tid_T8_SimD-1)/-1
print(tid_T8_sim.index)
## [1] 0.21875
##Simpsons Reciprocal 1/D (D')
tid_T8_SimD_recip<-diversity(tid_T8_D$n_ind,"inv")
print(tid_T8_SimD_recip) 
## [1] 4.571429
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

Tid_T8_SimpsonEM<-(tid_T8_SimD_recip/tid_T8_count)
print(Tid_T8_SimpsonEM)
## [1] 0.7619048
##Shannon Diversity Index 
tid_T8_shannon<-diversity(tid_T8_D$n_ind,"shannon") 
print(tid_T8_shannon)
## [1] 1.667462
##Shannon's evenness H/(ln(S)) 

tid_T8_shannon.evenness<-tid_T8_shannon/(log(tid_T8_count))
print(tid_T8_shannon.evenness) 
## [1] 0.9306282
#Transect 9###################################################################  

#S=species richness 
#S=specnumber() 

tid_T9_D<-tidroute_df%>%filter(line=='9')

tid_T9_count<-specnumber(tid_T9_D$Scientific_Name)
print(tid_T9_count)
## [1] 4
##Simpsons Diversity 1-D 

tid_T9_SimD<-diversity(tid_T9_D$n_ind,'simpson')
print(tid_T9_SimD) 
## [1] 0.6122449
#Simpsons Index

tid_T9_sim.index<-(tid_T9_SimD-1)/-1
print(tid_T9_sim.index)
## [1] 0.3877551
##Simpsons Reciprocal 1/D (D')
tid_T9_SimD_recip<-diversity(tid_T9_D$n_ind,"inv")
print(tid_T9_SimD_recip) 
## [1] 2.578947
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

Tid_T9_SimpsonEM<-(tid_T9_SimD_recip/tid_T9_count)
print(Tid_T9_SimpsonEM)
## [1] 0.6447368
##Shannon Diversity Index 
tid_T9_shannon<-diversity(tid_T9_D$n_ind,"shannon") 
print(tid_T9_shannon)
## [1] 1.153742
##Shannon's evenness H/(ln(S)) 

tid_T9_shannon.evenness<-tid_T9_shannon/(log(tid_T9_count))
print(tid_T9_shannon.evenness) 
## [1] 0.8322489
#Transect 10###################################################################  

#S=species richness 
#S=specnumber() 

tid_T10_D<-tidroute_df%>%filter(line=='10')

tid_T10_count<-specnumber(tid_T10_D$Scientific_Name)
print(tid_T10_count)
## [1] 6
##Simpsons Diversity 1-D 

tid_T10_SimD<-diversity(tid_T10_D$n_ind,'simpson')
print(tid_T10_SimD) 
## [1] 0.75
#Simpsons Index

tid_T10_sim.index<-(tid_T10_SimD-1)/-1
print(tid_T10_sim.index)
## [1] 0.25
##Simpsons Reciprocal 1/D (D')
tid_T10_SimD_recip<-diversity(tid_T10_D$n_ind,"inv")
print(tid_T10_SimD_recip) 
## [1] 4
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

Tid_T10_SimpsonEM<-(tid_T10_SimD_recip/tid_T10_count)
print(Tid_T10_SimpsonEM)
## [1] 0.6666667
##Shannon Diversity Index 
tid_T10_shannon<-diversity(tid_T10_D$n_ind,"shannon") 
print(tid_T10_shannon)
## [1] 1.56071
##Shannon's evenness H/(ln(S)) 

tid_T10_shannon.evenness<-tid_T10_shannon/(log(tid_T10_count))
print(tid_T10_shannon.evenness) 
## [1] 0.8710491
#####Mean of indices 

#Simpsons Diversity 1-D
Tid_Mean_SimD<-mean(tid_T1_SimD,tid_T2_SimD,tid_T3_SimD,tid_T4_SimD,tid_T5_SimD,
                    tid_T6_SimD,tid_T7_SimD,tid_T8_SimD,tid_T9_SimD,tid_T10_SimD) 

print(Tid_Mean_SimD)
## [1] 0.75
#Simpsons Index
Tid_Mean_sim.index<-mean(tid_T1_sim.index,tid_T2_sim.index,
                 tid_T3_sim.index,tid_T4_sim.index,tid_T5_sim.index,
                 tid_T6_sim.index,tid_T7_sim.index,
                 tid_T8_sim.index,tid_T9_sim.index,
                 tid_T10_sim.index)

print(Tid_Mean_sim.index) 
## [1] 0.25
#Simpsons Reciprocal Index 1/D

Tid_Mean_SimD_recip<-mean(tid_T1_SimD_recip,tid_T2_SimD_recip,tid_T3_SimD_recip,
                          tid_T4_SimD_recip,tid_T5_SimD_recip,tid_T6_SimD_recip,tid_T7_SimD_recip,
                          tid_T8_SimD_recip, tid_T9_SimD_recip, tid_T10_SimD_recip,)

print(Tid_Mean_SimD_recip)
## [1] 4
#Simpsons Evenness

Tid_Mean_Simpson.evenness<-mean(Tid_T1_SimpsonEM,Tid_T2_SimpsonEM,Tid_T3_SimpsonEM,
                                Tid_T4_SimpsonEM,Tid_T5_SimpsonEM,Tid_T6_SimpsonEM,Tid_T7_SimpsonEM,
                                Tid_T8_SimpsonEM,Tid_T9_SimpsonEM,Tid_T10_SimpsonEM)


print(Tid_Mean_Simpson.evenness)
## [1] 0.8
##Shannon Diversity Index 

Tid_Mean_Shannon<-mean(tid_T1_shannon,tid_T2_shannon,tid_T3_shannon,tid_T4_shannon,
                       tid_T5_shannon,tid_T6_shannon,tid_T7_shannon,tid_T8_shannon,
                       tid_T9_shannon,tid_T10_shannon)

print(Tid_Mean_Shannon)
## [1] 1.494175
##Shannon's evenness

Tid_Mean_Shannon.evenness<-mean(tid_T1_shannon.evenness,tid_T2_shannon.evenness,tid_T3_shannon.evenness,
                                tid_T4_shannon.evenness,tid_T5_shannon.evenness,tid_T6_shannon.evenness,
                                tid_T7_shannon.evenness,tid_T8_shannon.evenness,tid_T9_shannon.evenness,
                                tid_T10_shannon.evenness)

print(Tid_Mean_Shannon.evenness)
## [1] 0.9283832
##############Wild Basin Indices########################################################################

#Transect 1###################################################################
#S=species richness 
#S=specnumber() 

wildbasin_T1_D<-wildbasin_df%>%filter(line=='1')

wildbasin_T1_count<-specnumber(wildbasin_T1_D$Scientific_Name)
print(wildbasin_T1_count)
## [1] 8
##Simpsons Diversity 1-D 

wildbasin_T1_SimD<-diversity(wildbasin_T1_D$n_ind,'simpson')
print(wildbasin_T1_SimD) 
## [1] 0.795858
#Simpsons Index

wildbasin_T1_sim.index<-(wildbasin_T1_SimD-1)/-1
print(wildbasin_T1_sim.index)
## [1] 0.204142
##Simpsons Reciprocal 1/D (D')
wildbasin_T1_SimD_recip<-diversity(wildbasin_T1_D$n_ind,"inv")
print(wildbasin_T1_SimD_recip) 
## [1] 4.898551
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

wildbasin_T1_SimpsonEM<-(wildbasin_T1_SimD_recip/wildbasin_T1_count)
print(wildbasin_T1_SimpsonEM)
## [1] 0.6123188
##Shannon Diversity Index 
wildbasin_T1_shannon<-diversity(wildbasin_T1_D$n_ind,"shannon") 
print(wildbasin_T1_shannon)
## [1] 1.794653
##Shannon's evenness H/(ln(S)) 

wildbasin_T1_shannon.evenness<-wildbasin_T1_shannon/(log(wildbasin_T1_count))
print(wildbasin_T1_shannon.evenness) 
## [1] 0.8630459
#Transect 2###################################################################
#S=species richness 
#S=specnumber() 

wildbasin_T2_D<-wildbasin_df%>%filter(line=='2')

wildbasin_T2_count<-specnumber(wildbasin_T2_D$Scientific_Name)
print(wildbasin_T2_count)
## [1] 9
##Simpsons Diversity 1-D 

wildbasin_T2_SimD<-diversity(wildbasin_T2_D$n_ind,'simpson')
print(wildbasin_T2_SimD) 
## [1] 0.8387097
#Simpsons Index

wildbasin_T2_sim.index<-(wildbasin_T2_SimD-1)/-1
print(wildbasin_T2_sim.index)
## [1] 0.1612903
##Simpsons Reciprocal 1/D (D')
wildbasin_T2_SimD_recip<-diversity(wildbasin_T2_D$n_ind,"inv")
print(wildbasin_T2_SimD_recip)  
## [1] 6.2
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

wildbasin_T2_SimpsonEM<-(wildbasin_T2_SimD_recip/wildbasin_T2_count)
print(wildbasin_T2_SimpsonEM)
## [1] 0.6888889
##Shannon Diversity Index 
wildbasin_T2_shannon<-diversity(wildbasin_T2_D$n_ind,"shannon") 
print(wildbasin_T2_shannon)
## [1] 1.993887
##Shannon's evenness H/(ln(S)) 

wildbasin_T2_shannon.evenness<-wildbasin_T2_shannon/(log(wildbasin_T2_count))
print(wildbasin_T2_shannon.evenness)
## [1] 0.9074572
#Transect 3###################################################################
#S=species richness 
#S=specnumber() 

wildbasin_T3_D<-wildbasin_df%>%filter(line=='3')

wildbasin_T3_count<-specnumber(wildbasin_T3_D$Scientific_Name)
print(wildbasin_T3_count)
## [1] 7
##Simpsons Diversity 1-D 

wildbasin_T3_SimD<-diversity(wildbasin_T3_D$n_ind,'simpson')
print(wildbasin_T3_SimD) 
## [1] 0.6710291
#Simpsons Index

wildbasin_T3_sim.index<-(wildbasin_T3_SimD-1)/-1
print(wildbasin_T3_sim.index)
## [1] 0.3289709
##Simpsons Reciprocal 1/D (D')
wildbasin_T3_SimD_recip<-diversity(wildbasin_T3_D$n_ind,"inv")
print(wildbasin_T3_SimD_recip) 
## [1] 3.039783
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

wildbasin_T3_SimpsonEM<-(wildbasin_T3_SimD_recip/wildbasin_T3_count)
print(wildbasin_T3_SimpsonEM)
## [1] 0.4342547
##Shannon Diversity Index 
wildbasin_T3_shannon<-diversity(wildbasin_T3_D$n_ind,"shannon") 
print(wildbasin_T3_shannon)
## [1] 1.358405
##Shannon's evenness H/(ln(S)) 

wildbasin_T3_shannon.evenness<-wildbasin_T3_shannon/(log(wildbasin_T3_count))
print(wildbasin_T3_shannon.evenness) 
## [1] 0.6980819
#Transect 4###################################################################
#S=species richness 
#S=specnumber() 

wildbasin_T4_D<-wildbasin_df%>%filter(line=='4')

wildbasin_T4_count<-specnumber(wildbasin_T4_D$Scientific_Name)
print(wildbasin_T4_count)
## [1] 11
##Simpsons Diversity 1-D 

wildbasin_T4_SimD<-diversity(wildbasin_T4_D$n_ind,'simpson')
print(wildbasin_T4_SimD) 
## [1] 0.4723937
#Simpsons Index

wildbasin_T4_sim.index<-(wildbasin_T4_SimD-1)/-1
print(wildbasin_T4_sim.index)
## [1] 0.5276063
##Simpsons Reciprocal 1/D (D')
wildbasin_T4_SimD_recip<-diversity(wildbasin_T4_D$n_ind,"inv")
print(wildbasin_T4_SimD_recip) 
## [1] 1.895353
##Simpsons D of evenness 
#evenness=(1 / D) /S))  

wildbasin_T4_SimpsonEM<-(wildbasin_T4_SimD_recip/wildbasin_T4_count)
print(wildbasin_T4_SimpsonEM)
## [1] 0.1723048
##Shannon Diversity Index 
wildbasin_T4_shannon<-diversity(wildbasin_T4_D$n_ind,"shannon") 
print(wildbasin_T4_shannon)
## [1] 1.108423
##Shannon's evenness H/(ln(S)) 

wildbasin_T4_shannon.evenness<-wildbasin_T4_shannon/(log(wildbasin_T4_count))
print(wildbasin_T4_shannon.evenness) 
## [1] 0.4622484
#####Mean of indices 

#Simpsons Diversity 1-D
wildbasin_Mean_SimD<-mean(wildbasin_T1_SimD,wildbasin_T2_SimD,wildbasin_T3_SimD,wildbasin_T4_SimD) 

print(wildbasin_Mean_SimD)
## [1] 0.795858
#Simpsons Index
wildbasin_Mean_sim.index<-mean(wildbasin_T1_sim.index,wildbasin_T2_sim.index,
                               wildbasin_T3_sim.index,wildbasin_T4_sim.index)

print(wildbasin_Mean_sim.index) 
## [1] 0.204142
#Simpsons Reciprocal Index 1/D

wildbasin_Mean_SimD_recip<-mean(wildbasin_T1_SimD_recip,wildbasin_T2_SimD_recip,wildbasin_T3_SimD_recip,
                                wildbasin_T4_SimD_recip)

print(wildbasin_Mean_SimD_recip)
## [1] 4.898551
#Simpsons Evenness

wildbasin_Mean_Simpson.evenness<-mean(wildbasin_T1_SimpsonEM,wildbasin_T2_SimpsonEM,wildbasin_T3_SimpsonEM,
                                      wildbasin_T4_SimpsonEM)


print(wildbasin_Mean_Simpson.evenness)
## [1] 0.6123188
##Shannon Diversity Index 

wildbasin_Mean_Shannon<-mean(wildbasin_T1_shannon,wildbasin_T2_shannon,
                             wildbasin_T3_shannon,wildbasin_T4_shannon)

print(wildbasin_Mean_Shannon)
## [1] 1.794653
##Shannon's evenness

wildbasin_Mean_Shannon.evenness<-mean(wildbasin_T1_shannon.evenness,wildbasin_T2_shannon.evenness,
                                      wildbasin_T3_shannon.evenness,wildbasin_T4_shannon.evenness)

print(wildbasin_Mean_Shannon.evenness)
## [1] 0.8630459
##########################Table comparison of values################################

#Simpsons Table

tid_simpson.table_rbind<-data.frame(rbind(Tid_Mean_sim.index,
                             Tid_Mean_SimD,
                             Tid_Mean_SimD_recip,
                             Tid_Mean_Simpson.evenness))

colnames(tid_simpson.table_rbind)<-'Tidroute' 

tid_simpson.table_rbind
##                           Tidroute
## Tid_Mean_sim.index            0.25
## Tid_Mean_SimD                 0.75
## Tid_Mean_SimD_recip           4.00
## Tid_Mean_Simpson.evenness     0.80
WB_simpson.table_rbind<-data.frame(rbind(wildbasin_Mean_sim.index,wildbasin_Mean_SimD,
                                         wildbasin_Mean_SimD_recip,wildbasin_Mean_Simpson.evenness))

colnames(WB_simpson.table_rbind)<-'Wild Basin'

tid_WB_simpson<-data.frame(cbind(tid_simpson.table_rbind$Tidroute,WB_simpson.table_rbind$`Wild Basin`))

rownames(tid_WB_simpson)<-c("Simpson's Index (D)","Simpson's Diversity (1-D)",
                            "Simpson's Reciprocal Index (D')","Simpson's index of diversity evenness (Esimpson)")

colnames(tid_WB_simpson)<-c('Tidroute','Wild Basin')

kable(head(tid_WB_simpson),caption= "Table 1. Simpson's Index (D),Simpson's Diversity (1-D),Simpson's Reciprocal Index (D'), and Simpson's index of diversity evenness (Esimpson) values for each site. Values were calculated using the means of all D,1-D,D',and Esimpson values of each transect at each site.",digits = 2)
Table 1. Simpson’s Index (D),Simpson’s Diversity (1-D),Simpson’s Reciprocal Index (D’), and Simpson’s index of diversity evenness (Esimpson) values for each site. Values were calculated using the means of all D,1-D,D’,and Esimpson values of each transect at each site.
Tidroute Wild Basin
Simpson’s Index (D) 0.25 0.20
Simpson’s Diversity (1-D) 0.75 0.80
Simpson’s Reciprocal Index (D’) 4.00 4.90
Simpson’s index of diversity evenness (Esimpson) 0.80 0.61
#Shannon Table 

tid_shannon_rbind<-rbind(Tid_Mean_Shannon,Tid_Mean_Shannon.evenness)

WB_shannon_rbind<-rbind(wildbasin_Mean_Shannon,wildbasin_Mean_Shannon.evenness)

tid_WB_shannon<-data.frame(tid_shannon_rbind,WB_shannon_rbind)
tid_WB_shannon
##                           tid_shannon_rbind WB_shannon_rbind
## Tid_Mean_Shannon                  1.4941751        1.7946535
## Tid_Mean_Shannon.evenness         0.9283832        0.8630459
colnames(tid_WB_shannon)<-c('Tidroute','Wild Basin')
rownames(tid_WB_shannon)<-c("Shannon's Diversity Index (H)","Shannon's Evenness (Eshannon)")
tid_WB_shannon  
##                                Tidroute Wild Basin
## Shannon's Diversity Index (H) 1.4941751  1.7946535
## Shannon's Evenness (Eshannon) 0.9283832  0.8630459
kable(head(tid_WB_shannon),caption= "Table 2. Shannon Diversity Index (H) and Shannon Evenness(Eshannon) values for each site. Values were calculated using the means of all H and Eshannon values of each transect at each site.",  digits = 2)
Table 2. Shannon Diversity Index (H) and Shannon Evenness(Eshannon) values for each site. Values were calculated using the means of all H and Eshannon values of each transect at each site.
Tidroute Wild Basin
Shannon’s Diversity Index (H) 1.49 1.79
Shannon’s Evenness (Eshannon) 0.93 0.86
require(terra)
require(raster)
require(leaflet)
require(stringr)
require(FedData) 
#NLCD rasters and site coordinates 

#2023 NLCD
NLCD2023<-rast("C:/Users/knoll/Downloads/Annual_NLCD_LndCov_2023_CU_C1V0 (1).tif")

#1985 NLCD 
NLCD1985<-rast("C:/Users/knoll/Downloads/Annual_NLCD_LndCov_1985_CU_C1V0 (1).tif")


#coordinates for transects
site_coords<-read_csv("C:/Users/knoll/Documents/RGTECH/Biodiversity Project/Data/site coords.csv")
## Rows: 2 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): site
## dbl (2): lat, long
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
site_coords_df<-as.data.frame(site_coords) 

#Tidroute coordinates
tid_coords<-data.frame(site_coords_df[1,]) 

#Wild Basin coordinates
WB_coords<-data.frame(site_coords_df[2,])
#Texas ecoprovince
texas_eco<-st_read("C:/Users/knoll/Documents/RGTECH/Biodiversity Project/Data/texas_ecoprov.shp")
## Reading layer `texas_ecoprov' from data source 
##   `C:\Users\knoll\Documents\RGTECH\Biodiversity Project\Data\texas_ecoprov.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 0 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -107.4506 ymin: 25.84485 xmax: -97.10797 ymax: 37.44279
## Geodetic CRS:  NAD83
st_crs(texas_eco)<-4326
## Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
## that
texas_eco_proj<-sf::st_transform(texas_eco,st_crs(5070))
plot(texas_eco_proj$geometry) 

#NLCD 2023 crop to Texas ecoprovince
texas_eco_rast_2023<-NLCD2023%>%crop(vect(texas_eco_proj))%>%mask(vect(texas_eco_proj))
## |---------|---------|---------|---------|=========================================                                          |---------|---------|---------|---------|=========================================                                          |---------|---------|---------|---------|=========================================                                          
## Warning: [mask] CRS do not match
#NLCD 1985 crop to Texas ecoprovince
texas_eco_rast_1985<-NLCD1985%>%crop(vect(texas_eco_proj))%>%mask(vect(texas_eco_proj))
## |---------|---------|---------|---------|=========================================                                          |---------|---------|---------|---------|=========================================                                          |---------|---------|---------|---------|=========================================                                          
## Warning: [mask] CRS do not match
#create point for Wild Basin transect
WB_point<-st_as_sf(WB_coords,coords = c("long", "lat"))
WB_crs<-st_set_crs(WB_point,4326)
WBT<- st_transform(WB_crs, crs = 5070)

#create Wild Basin buffers

#crop 1 hectare area 2023
WB_1_hectare<-sf::st_buffer(WBT,
              dist = 56.42,
              endCapStyle = "SQUARE")


WB_1_hect_crop_2023<-crop(texas_eco_rast_2023,WB_1_hectare,mask=T)
## Warning: [crop] CRS do not match
plot(WB_1_hect_crop_2023)

#crop .1 hectare area 2023 
#create buffer
WB_.1_hect<-sf::st_buffer(WBT,
                            dist = 17.84,
                            endCapStyle = "SQUARE")


WB_.1_hect_crop_2023<-crop(texas_eco_rast_2023,WB_.1_hect,mask=T)
## Warning: [crop] CRS do not match
plot(WB_.1_hect_crop_2023)

#crop 1 hectare area 1985

WB_1_hect_crop_1985<-crop(texas_eco_rast_1985,WB_1_hectare,mask=T)
## Warning: [crop] CRS do not match
plot(WB_1_hect_crop_1985)

#crop .1 hectare area 1985
WB_.1_hect_crop_1985<-crop(texas_eco_rast_2023,WB_.1_hect,mask=T)
## Warning: [crop] CRS do not match
plot(WB_.1_hect_crop_1985) 

#PA ecoprovince
PA_eco<-st_read("C:/Users/knoll/Documents/RGTECH/Biodiversity Project/Data/pen_ecoprov.shp")
## Reading layer `pen_ecoprov' from data source 
##   `C:\Users\knoll\Documents\RGTECH\Biodiversity Project\Data\pen_ecoprov.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 0 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -79.99375 ymin: 40.9308 xmax: -66.95723 ymax: 47.35978
## Geodetic CRS:  NAD83
st_crs(PA_eco)<-4326
## Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
## that
PA_eco_proj<-sf::st_transform(PA_eco,st_crs(5070))
plot(PA_eco_proj$geometry)  

#NLCD 2023 crop to PA ecoprovince
PA_eco_rast_2023<-NLCD2023%>%crop(vect(PA_eco_proj))%>%mask(vect(PA_eco_proj))
## |---------|---------|---------|---------|=========================================                                          |---------|---------|---------|---------|=========================================                                          |---------|---------|---------|---------|=========================================                                          
## Warning: [mask] CRS do not match
#NLCD 1985 crop to PA ecoprovince
PA_eco_rast_1985<-NLCD1985%>%crop(vect(PA_eco_proj))%>%mask(vect(PA_eco_proj))
## |---------|---------|---------|---------|=========================================                                          |---------|---------|---------|---------|=========================================                                          |---------|---------|---------|---------|=========================================                                          
## Warning: [mask] CRS do not match
#create point for Wild Basin transect
tid_point<-st_as_sf(tid_coords,coords = c("long", "lat"))
tid_crs<-st_set_crs(tid_point,4326)
tid_transform<- st_transform(tid_crs, crs = 5070)


#crop 1 hectare area 2023
#create buffer
tid_1_hectare<-sf::st_buffer(tid_transform,
                            dist = 56.42,
                            endCapStyle = "SQUARE")


tid_1_hect_crop_2023<-crop(PA_eco_rast_2023,tid_1_hectare,mask=T)
## Warning: [crop] CRS do not match
plot(tid_1_hect_crop_2023)

#crop .1 hectare area 2023 
#create buffer
tid_.1_hect<-sf::st_buffer(tid_transform,
                          dist = 17.84,
                          endCapStyle = "SQUARE")


tid_.1_hect_crop_2023<-crop(PA_eco_rast_2023,tid_.1_hect,mask=T)
## Warning: [crop] CRS do not match
plot(tid_.1_hect_crop_2023)

#crop 1 hectare area 1985
tid_1_hect_crop_1985<-crop(PA_eco_rast_1985,tid_1_hectare,mask=T)
## Warning: [crop] CRS do not match
plot(tid_1_hect_crop_1985)

#crop .1 hectare area 1985

tid_.1_hect_crop_1985<-crop(PA_eco_rast_1985,tid_.1_hect,mask=T)
## Warning: [crop] CRS do not match
plot(tid_.1_hect_crop_1985)

#Create interactive Map

#state shape geojson
us_sf<-read_sf("C:/Users/knoll/Documents/RGTECH/Biodiversity Project/Data/georef-united-states-of-america-state.geojson")

#texas border
texas_sf<-us_sf%>%filter(str_detect(ste_name,'Texas'))
st_crs(texas_sf)<-4326 


#PA border
PA_sf<-us_sf%>%filter(str_detect(ste_name,'Pennsylvania'))
st_crs(PA_sf)<-4326 

#combine for leaflet

states<-rbind(texas_sf, PA_sf)


#add 1985 ecoprov
WB_85<-project(texas_eco_rast_1985, "EPSG:4326", method = "near")
## |---------|---------|---------|---------|=========================================                                          |---------|---------|---------|---------|=========================================                                          
#texas ecoprov resampled for courser resolution due to raster size
WB_85_sample<-spatSample(texas_eco_rast_1985, 10000000, method="regular", as.raster=TRUE)

#PA ecoprov resampled for courser resolution due to raster size
tid_85_sample<-spatSample(PA_eco_rast_1985, 10000000, method="regular", as.raster=TRUE) 

#get NLCD legend data and colors
legend<-pal_nlcd()


leaflet(states)%>% addTiles()%>%
  addPolygons(
    weight = 3,
    opacity = 1,
    color = "blue",
    fillOpacity = 0)%>%
  addRasterImage(WB_85_sample,opacity = 1, group='ecoprovince')%>%
  addRasterImage(tid_85_sample,opacity = 1, group='ecoprovince')%>%
  addMarkers(data=WB_point,label = "Wild Basin",
             labelOptions = labelOptions(noHide = TRUE, textsize = 12))%>%
  addMarkers(data=tid_point,label = "Tidroute",
             labelOptions = labelOptions(noHide = TRUE, textsize = 12))%>%
  addRasterImage(WB_.1_hect_crop_1985, opacity = 1,group=".1 hectare transect 1985")%>%
  addRasterImage(WB_.1_hect_crop_2023, opacity = 1,group=".1 hectare transect 2023")%>%
  addRasterImage(WB_1_hect_crop_1985, opacity = 1,group="1 hectare surrounding area 1985")%>%
  addRasterImage(WB_1_hect_crop_2023, opacity = 1,group="1 hectare surrounding area 2023")%>%
  addRasterImage(tid_.1_hect_crop_1985, opacity = 1,group=".1 hectare transect 1985")%>%
  addRasterImage(tid_.1_hect_crop_2023, opacity = 1,group=".1 hectare transect 2023")%>%
  addRasterImage(tid_1_hect_crop_1985, opacity = 1,group="1 hectare surrounding area 1985")%>%
  addRasterImage(tid_1_hect_crop_2023, opacity = 1,group="1 hectare surrounding area 2023")%>%
  addLegend(position = "topleft",colors= legend$Color,labels=legend$Class,title="Land Cover Classification",opacity = 1)%>%
  addLayersControl(baseGroups = "OpenStreetMap",
                   overlayGroups = c(".1 hectare transect 1985",".1 hectare transect 2023",
                                     "1 hectare surrounding area 1985","1 hectare surrounding area 2023"),
                   options = layersControlOptions(collapsed = FALSE))%>%
  addScaleBar(position = "bottomleft")%>%
  groupOptions("ecoprovince", zoomLevels = 0:8)