#Calculating the iBS metric #Step 1:load input rasters #load species diversity output Diversity<-raster('DiversityLucia.tif') #load natural index output NI<-raster('NaturalIndexLucia.tif') #Step 2: Normalize both inputs #normaize diversity to range 0 to 1 Diversity_norm<- rescale(Diversity, to = c(0, 1)) #normaize natural index to range 0 to 1 NI_norm<- rescale(NI, to = c(0, 1)) #sTEP 3: Multiply the normalized rasters to output the iBS iBS_Lucia <- Diversity_norm * NI_norm #export writeRaster(iBS_Lucia, 'iBS_Lucia.tif') #Extracting values for violin plots #STEP 1: generate random points for the three categories #in both study areas #first specify an extent for three areas NP, AF, P #kakamega NP<-randomPoints(Nonforest_KK, n=150, ext=Nonforest_KK) AF<-randomPoints(Agroforest_KK, n=150, Agroforest_KK) P<-randomPoints(forest_KK, n=150, ext=forest_KK) #lucia NP_lu<-randomPoints(Nonforest_lu, n=150, ext=Nonforest_lu) P_lu<-randomPoints(forest_lu, n=150, ext=forest_lu) #load the rasters(iBS ) that values will be extracted from iBS_KK<-raster('iBS_KK.tif') SB_KK<-raster('SB_KK.tif') iBS_Lucia<-raster('iBS_Lucia.tif') DF_FI<-raster('DF_FI.tif') #STEP 2: using the random points extract values from rasters #Kakamega #iBS NP_iBS_KK<-data.frame(raster::extract(iBS_KK, NP)) AF_iBS_KK<-data.frame(raster::extract(iBS_KK, AF)) P_iBS_KK<-data.frame(raster::extract(iBS_KK, P)) #SB NP_SB_KK<-data.frame(raster::extract(SB_KK, NP)) AF_SB_KK<-data.frame(raster::extract(SB_KK, AF)) P_SB_KK<-data.frame(raster::extract(SB_KK, P)) #Lucia #iBS NP_iBS_lu<-data.frame(raster::extract(iBS_Lucia, NP_lu)) P_iBS_lu<-data.frame(raster::extract(iBS_Lucia, P_lu)) #DF NP_DF_lu<-data.frame(raster::extract(DF_FI, NP_lu)) P_DF_lu<-data.frame(raster::extract(DF_FI, P_lu)) #STEP3: Combine dataframes #combine the dataframes from iBS ratsers in both sites Df1<-rbind(NP_iBS_KK,AF_iBS_KK,P_iBS_KK, NP_iBS_lu, P_iBS_lu) #combine all dataframes in the order they sould appear Df2<-rbind(NP_iBS_KK,AF_iBS_KK,P_iBS_KK, NP_SB_KK, AF_SB_KK, P_SB_KK NP_iBS_lu, P_iBS_lu, NP_DF_lu, P_DF_lu) #STEP 4: Violinplotters install.packages('violinplotters') library(violinplotter) violinplotter(RasterVals ~ Group, data=Df1, TITLE="", XLAB="", YLAB="", VIOLIN_COLOURS=c(c("black", "black", "black", "black","black")), PLOT_BARS=TRUE, ERROR_BAR_COLOURS=c("#636363","#1c9099","#de2d26"), SHOW_SAMPLE_SIZE=FALSE, SHOW_MEANS=TRUE, CATEGORICAL=TRUE, LOGX=FALSE, LOGX_BASE=10, MANN_WHITNEY=TRUE, HSD=TRUE, ALPHA=0.02, REGRESS=FALSE) violinplotter(RasterVals ~ Group, data=Df2, TITLE="", XLAB="", YLAB="", VIOLIN_COLOURS=c(c("black", "black", "black", "orange", "orange","orange", "black","black","blue", "blue")), PLOT_BARS=TRUE, ERROR_BAR_COLOURS=c("#636363","#1c9099","#de2d26"), SHOW_SAMPLE_SIZE=FALSE, SHOW_MEANS=TRUE, CATEGORICAL=TRUE, LOGX=FALSE, LOGX_BASE=10, MANN_WHITNEY=TRUE, HSD=TRUE, ALPHA=0.02, REGRESS=FALSE)