N_RHealth<-read.csv("Nthapo RangeMod.csv")
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
N_RHealth %>%
  group_by(data.time_date.site,data.rangel_form.rl_nearest_perennial_sp) %>%
  summarise(count=n()) %>%
  mutate(freq=count/sum(count)) %>%
  ggplot(aes(x=reorder(data.rangel_form.rl_nearest_perennial_sp, -freq),y=freq)) + geom_bar(stat="identity", fill= "seagreen") +theme_bw(base_size=9) +
  theme(axis.text.x = element_text(angle= 90, hjust=1)) + xlab("Species Name") + ylab("Relative Frequency")+facet_wrap(~data.time_date.site, ncol=2)
## `summarise()` has grouped output by 'data.time_date.site'. You can override
## using the `.groups` argument.

library(dplyr)
RYield<-N_RHealth%>%
  group_by(data.time_date.site,data.time_date.plot,data.rangel_form.rl_dpm_height)%>%
  summarise(MeanDPM= mean(data.rangel_form.rl_dpm_height,na.rm = TRUE))
## `summarise()` has grouped output by 'data.time_date.site',
## 'data.time_date.plot'. You can override using the `.groups` argument.
library(ggplot2)
RYield%>%
  mutate(Yield=(31.7176*0.3218^(1/MeanDPM)*MeanDPM^0.2834)^2)%>%
  group_by(data.time_date.site,Yield)%>%
  ggplot()+geom_boxplot(aes(x=data.time_date.site,y=Yield,fill=data.time_date.site)) +coord_flip() +
  ggtitle("Yield per grazing unit")+theme(legend.position = "none")+xlab("Grazing Unit")+ylab("Yield(Kg/ha)")
## Warning: Removed 1 rows containing non-finite values (`stat_boxplot()`).

N_RHealth%>%
  group_by(data.time_date.site)%>%
  mutate(count=n()) %>%
  filter(count>0 & !is.na(data.time_date.site)) %>%
  ggplot(aes(y =data.rangel_form.rl_dist_perennial, x = data.time_date.site)) + 
  geom_boxplot(fill="grey50", width=0.4, alpha=0.7) +
  geom_hline(aes(yintercept=8),lwd=0.5, lty=3, col="black")+ ggtitle('Distance to tuft  per grazing unit ') + theme_gray(base_size=10)+ ylab('Distance to the nearest tuft (cm)')+ xlab('grazing unit')+coord_flip()

N_RHealth%>%
  group_by(data.time_date.site)%>%
  mutate(count=n()) %>%
  filter(count>0 & !is.na(data.time_date.site)) %>%
  ggplot(aes(y =data.rangel_form.rl_leaflitter_depth_cm, x = data.time_date.site)) + 
  geom_boxplot(fill="grey50", width=0.4, alpha=0.7) +
  geom_hline(aes(yintercept=8),lwd=0.5, lty=3, col="black")+ ggtitle('Litter thickness  per grazing unit ') + theme_gray(base_size=10)+ ylab('Litter thickness (cm)')+ xlab('grazing unit')+coord_flip()

N_RHealth %>%
  group_by (data.time_date.site) %>%
  summarise(uniqueSpecies = length(unique(data.rangel_form.rl_nearest_perennial_sp)),
            meanElevation=mean(data.gps.altitude)) %>%
  ggplot(aes(x=reorder(data.time_date.site, -uniqueSpecies), y=uniqueSpecies,fill=data.time_date.site))+ geom_boxplot()+ theme_bw()+theme(legend.position = "none") +ggtitle("Unique Plant Species by grazing Unit")+ ylab("Number of Unique Herbaceous Species") + xlab("Grazing unit")+theme(axis.text.x = element_text(angle=45))

N_Browse<-read.csv("Nthapo Shrubs.csv")
sort(unique(N_Browse$data.browse_sp1.tree_shrub_sp1))
##  [1] " "                         "Aloe maculata"            
##  [3] "Chrysocoma ciliata"        "Cliffortia nitidula"      
##  [5] "Diospyros austro-africana" "Euryops annae"            
##  [7] "Felicia filifolia"         "Gymnopentzia bifurcata"   
##  [9] "Helichrysum splendidum"    "Helichrysum trilineatum"  
## [11] "Leucosidea sericea"        "Passerina montana"        
## [13] "Pentzia cooperi"           "Relhania dieterlenii"     
## [15] "Rhumnus prinoides"         "Searsia divericata"       
## [17] "Stachys rugosa"
N_Browse%>%
  mutate(AvShrubDensity=as.integer((data.browse_sp1.tree_shrub_sp_n1)/1*100))%>%
  group_by(data.time_date.site)%>%
  ggplot()+geom_boxplot(aes(x=data.time_date.site,y=AvShrubDensity,fill=data.time_date.site))  +ggtitle("Woody plant density per site")+theme(legend.position = "none")+xlab("Sample Site")+ylab("Woody Plant Density/(No./ha)")+theme(axis.text.x = element_text(angle=45))
## Warning: Removed 1 rows containing non-finite values (`stat_boxplot()`).

N_Browse%>%
  group_by(data.time_date.site,data.browse_sp1.tree_shrub_sp1)%>%
  mutate(ShrubVol=(pi*(sum(data.browse_sp1.tree_shrub_sp_height1,data.browse_sp1.shrub_average_width1)/2)^2*data.browse_sp1.shrub_average_height1)/4)%>%
  ggplot()+geom_boxplot(aes(x=data.browse_sp1.tree_shrub_sp1,y=ShrubVol,fill=data.browse_sp1.tree_shrub_sp1))  +
  ggtitle("Shrub volume per species in all grazing units")+ coord_flip()+theme(legend.position = "none")+xlab("Woody plant species")+ylab("Shrub Volume(m^3))") + facet_wrap(~data.time_date.site,nrow = 2)+theme(axis.text.x = element_text(angle=60))
## Warning: Removed 1 rows containing non-finite values (`stat_boxplot()`).

SP_List<-sort(unique(N_RHealth$data.rangel_form.rl_nearest_perennial_sp),decreasing = FALSE)# herbaceous Species Checklist
Herb_Sp<-as.data.frame(SP_List)
Herb_Sp
##                              SP_List
## 1                Agrostis lachnantha
## 2                 Aristida bipartita
## 3                  Aristida congesta
## 4                   Aristida diffusa
## 5                      Bidens pilosa
## 6                 Catalepis gracilis
## 7                  Cheilanthes hirta
## 8  Cotyledon orbiculata var. oblonga
## 9               Cymbopogon excavatus
## 10                  Cynodon dactylon
## 11                Cyperus marginatus
## 12                 Elionurus muticus
## 13               Eragrostis capensis
## 14                Eragrostis curvula
## 15                  Eragrostis plana
## 16               Eragrostis racemosa
## 17       Fingerhuthia sesleriiformis
## 18                 Gazania krebsiana
## 19                   Harpochloa falx
## 20          Helichrysum caespititium
## 21              Helichrysum ecklonis
## 22               Helichrysum krookii
## 23         Helichrysum odoratissimum
## 24             Heteropogon contortus
## 25                 Hyperrhenia hirta
## 26         Jamesbrettania breviflora
## 27         Jamesbrittenia breviflora
## 28       Jamesbrittenia prestisepala
## 29                  Malva parviflora
## 30                    Melenis repens
## 31           Melolobium microphyllum
## 32             Merxmuellera disticha
## 33                 Microchloa caffra
## 34        Pelargonium alchemelloides
## 35            Pennisetum sphacelatum
## 36             Pennisetum thunbergii
## 37            Psammotropha mucronata
## 38                      Salsola kali
## 39                Salvia stenophylla
## 40                 Senecio asperulus
## 41               Senecio dissimulans
## 42                     Sonchus nanus
## 43                    Tagetes minuta
## 44                  Themeda triandra
## 45              Trachypogon spicatus
## 46               Trifolium africanum
## 47                  Xanthium spinosa
SpEco<-Herb_Sp %>%
  mutate(
    EcoStatus = case_when(
      SP_List == "Fingerhuthia sesleriiformis"        ~ "Decreaser",
      SP_List == "Aristida diffusa"        ~ "Increaser3",
      SP_List == "Eragrostis plana"        ~ "Increaser2",
      SP_List == "Eragrostis racemosa"        ~ "Increaser2",
      SP_List == "Agrostis lachnantha"        ~ "Increaser2",
      SP_List == "Eragrostis capensis"        ~ "Increaser2",
      SP_List == "Themeda triandra"        ~ "Decreaser",
      SP_List == "Festuca caprina"        ~ "Decreaser",
      SP_List == "Eragrostis curvula"        ~ "Increaser2",
      SP_List == "Eragrostis caesia"        ~ "Increaser2",
      SP_List == "Trachypogon spicatus"        ~ "Increaser1",
      SP_List == "Merxmuellera disticha"        ~ "Increaser3",
      SP_List == "Cynodon dactylon"        ~ "Increaser2",
      SP_List == "Melica decumbens"        ~ "Increaser3",
      SP_List == "Aristida bipartita"        ~ "Increaser2",
      SP_List == "Aristida congesta"        ~ "Increaser2",
      SP_List == "Catalepis gracilis"        ~ "Increaser1",
      SP_List == "Cymbopogon excavatus"        ~ "Increaser1",
      SP_List == "Cyperus marginatus"        ~ "Increaser1",
      SP_List == "Elionurus muticus"        ~ "Increaser3",
      SP_List == "Harpochloa falx"        ~ "Increaser1",
      SP_List == "Heteropogon contortus"        ~ "Increaser2",
      SP_List == "Hyperrhenia hirta"        ~ "Increaser1",
      SP_List == "Melenis repens"        ~ "Increaser2",
      SP_List == "Microchloa caffra"        ~ "Increaser2",
      SP_List == "Pennisetum sphacelatum"        ~ "Increaser1",
      SP_List == "Pennisetum thunbergii"        ~ "Increaser1",      
      TRUE                      ~ "other"
    )
  )
SpEcoName<-rename(SpEco,data.rangel_form.rl_nearest_perennial_sp = SP_List)
SpEcoName  
##    data.rangel_form.rl_nearest_perennial_sp  EcoStatus
## 1                       Agrostis lachnantha Increaser2
## 2                        Aristida bipartita Increaser2
## 3                         Aristida congesta Increaser2
## 4                          Aristida diffusa Increaser3
## 5                             Bidens pilosa      other
## 6                        Catalepis gracilis Increaser1
## 7                         Cheilanthes hirta      other
## 8         Cotyledon orbiculata var. oblonga      other
## 9                      Cymbopogon excavatus Increaser1
## 10                         Cynodon dactylon Increaser2
## 11                       Cyperus marginatus Increaser1
## 12                        Elionurus muticus Increaser3
## 13                      Eragrostis capensis Increaser2
## 14                       Eragrostis curvula Increaser2
## 15                         Eragrostis plana Increaser2
## 16                      Eragrostis racemosa Increaser2
## 17              Fingerhuthia sesleriiformis  Decreaser
## 18                        Gazania krebsiana      other
## 19                          Harpochloa falx Increaser1
## 20                 Helichrysum caespititium      other
## 21                     Helichrysum ecklonis      other
## 22                      Helichrysum krookii      other
## 23                Helichrysum odoratissimum      other
## 24                    Heteropogon contortus Increaser2
## 25                        Hyperrhenia hirta Increaser1
## 26                Jamesbrettania breviflora      other
## 27                Jamesbrittenia breviflora      other
## 28              Jamesbrittenia prestisepala      other
## 29                         Malva parviflora      other
## 30                           Melenis repens Increaser2
## 31                  Melolobium microphyllum      other
## 32                    Merxmuellera disticha Increaser3
## 33                        Microchloa caffra Increaser2
## 34               Pelargonium alchemelloides      other
## 35                   Pennisetum sphacelatum Increaser1
## 36                    Pennisetum thunbergii Increaser1
## 37                   Psammotropha mucronata      other
## 38                             Salsola kali      other
## 39                       Salvia stenophylla      other
## 40                        Senecio asperulus      other
## 41                      Senecio dissimulans      other
## 42                            Sonchus nanus      other
## 43                           Tagetes minuta      other
## 44                         Themeda triandra  Decreaser
## 45                     Trachypogon spicatus Increaser1
## 46                      Trifolium africanum      other
## 47                         Xanthium spinosa      other
R_HealthJoin<-N_RHealth %>%
  mutate(
    EcoStatus = case_when(
      data.rangel_form.rl_nearest_perennial_sp == "Fingerhuthia sesleriiformis"        ~ "Decreaser",
      data.rangel_form.rl_nearest_perennial_sp == "Aristida diffusa"        ~ "Increaser3",
      data.rangel_form.rl_nearest_perennial_sp == "Eragrostis plana"        ~ "Increaser2",
      data.rangel_form.rl_nearest_perennial_sp == "Eragrostis racemosa"        ~ "Increaser2",
      data.rangel_form.rl_nearest_perennial_sp == "Agrostis lachnantha"        ~ "Increaser2",
      data.rangel_form.rl_nearest_perennial_sp == "Eragrostis capensis"        ~ "Increaser2",
      data.rangel_form.rl_nearest_perennial_sp == "Themeda triandra"        ~ "Decreaser",
      data.rangel_form.rl_nearest_perennial_sp == "Festuca caprina"        ~ "Decreaser",
      data.rangel_form.rl_nearest_perennial_sp == "Eragrostis curvula"        ~ "Increaser2",
      data.rangel_form.rl_nearest_perennial_sp == "Eragrostis caesia"        ~ "Increaser2",
      data.rangel_form.rl_nearest_perennial_sp == "Trachypogon spicatus"        ~ "Increaser1",
      data.rangel_form.rl_nearest_perennial_sp == "Merxmuellera disticha"        ~ "Increaser3",
      data.rangel_form.rl_nearest_perennial_sp == "Cynodon dactylon"        ~ "Increaser2",
      data.rangel_form.rl_nearest_perennial_sp == "Melica decumbens"        ~ "Increaser3",
      data.rangel_form.rl_nearest_perennial_sp == "Aristida bipartita"        ~ "Increaser2",
      data.rangel_form.rl_nearest_perennial_sp == "Aristida congesta"        ~ "Increaser2",
      data.rangel_form.rl_nearest_perennial_sp == "Catalepis gracilis"        ~ "Increaser1",
      data.rangel_form.rl_nearest_perennial_sp == "Cymbopogon excavatus"        ~ "Increaser1",
      data.rangel_form.rl_nearest_perennial_sp == "Cyperus marginatus"        ~ "Increaser1",
      data.rangel_form.rl_nearest_perennial_sp == "Elionurus muticus"        ~ "Increaser3",
      data.rangel_form.rl_nearest_perennial_sp == "Harpochloa falx"        ~ "Increaser1",
      data.rangel_form.rl_nearest_perennial_sp == "Heteropogon contortus"        ~ "Increaser2",
      data.rangel_form.rl_nearest_perennial_sp == "Hyperrhenia hirta"        ~ "Increaser1",
      data.rangel_form.rl_nearest_perennial_sp == "Melenis repens"        ~ "Increaser2",
      data.rangel_form.rl_nearest_perennial_sp == "Microchloa caffra"        ~ "Increaser2",
      data.rangel_form.rl_nearest_perennial_sp == "Pennisetum sphacelatum"        ~ "Increaser1",
      data.rangel_form.rl_nearest_perennial_sp == "Pennisetum thunbergii"        ~ "Increaser1",      
      TRUE                      ~ "other"
    )
  )
R_HealthJoin %>%
  group_by(data.time_date.site,EcoStatus) %>%
  summarise(count=n()) %>%
  mutate(freq=count/sum(count)*100) %>%
  ggplot(aes(x=reorder(EcoStatus, -freq),y=freq)) + geom_bar(stat="identity", fill= "tomato") +theme_bw(base_size=9) +
  theme(axis.text.x = element_text(angle= 90, hjust=1)) + xlab("Ecological Status") + ylab("Fractional Cover(%)")+facet_wrap(~data.time_date.site, ncol=5)+ theme_dark()+theme(axis.text.x = element_text(angle=90))
## `summarise()` has grouped output by 'data.time_date.site'. You can override
## using the `.groups` argument.

N_Browse%>%
  mutate(AvShrubDensity=as.integer((data.browse_sp1.tree_shrub_sp_n1)/1*100))%>%
  group_by(data.time_date.site)%>%
  ggplot()+geom_violin(aes(x=data.time_date.site,y=AvShrubDensity,
  stat = "ydensity",
  position = "dodge",draw_quantiles = TRUE,
  trim = TRUE,scale = "area",
  inherit.aes = TRUE,fill=data.time_date.site))  +ggtitle("Woody plant density per site")+theme(legend.position = "none")+xlab("Sample Site")+ylab("Woody Plant Density/(No./ha)")+theme(axis.text.x = element_text(angle=45))
## Warning in geom_violin(aes(x = data.time_date.site, y = AvShrubDensity, : Ignoring unknown aesthetics: stat, position, draw_quantiles, trim, scale, and
## inherit.aes
## Warning: Removed 1 rows containing non-finite values (`stat_ydensity()`).