R/plot_DIV.R

Defines functions plot_DIV

Documented in plot_DIV

#' Mangrove Indices of Diversity visualization
#'
#' This function will output the same type of visualization "boxplot" submitted to the report
#'
#'
#'
#' @param data Processed data frame/s obtained from \code{\link{compute_DIV}}.
#'
#'
#' @return Output is an object named 'diversityindex', a box-plot that shows the diversity indices computed for each site per cluster levels
#'
#' @keywords index of diversity, plot
#'
#'
#'
#' @export

# Function to plot the diversity indices visually
plot_DIV<-function(data = data){

  # Setup variables
  div.df = data

  # Defines the `%>%` operator to the current environment
  `%>%` <- dplyr::`%>%`

  # Check if the input for data frame(s) is more than one
  if(is.vector(div.df)){

    ## Store temporarily the data frames
    dataframes = div.df

    ## Make a catchment data frame to collect several dataframes
    div.df<- data.frame()

    ## Make a for loop in collecting several dataframes
    for(i in 1:length(dataframes)){
      df<- dataframes[[i]]
      colnames(df)[1]<- "LOCATION"
      div.df<-rbind(div.df,df)
    }
  }

  ## Renaming column names so it will be more pleasant to see
  colnames(div.df)<- c("Location", "Site", "Shannon diversity", "Simpson diversity",
                   "Pielou evenness", "Margalef richness")

  ## Transform data frame into a long format
  div.df.melt<- reshape::melt(div.df, id=c("Location", "Site"))


  locationlvls<- unique(div.df.melt$Location)

  ## Set level of factors for location (arrangement in the plot) to appropriate order
  div.df.melt<-
    div.df.melt %>%
    dplyr::mutate(Location = factor(Location,
                                    levels = locationlvls))

  ## Adds a 'next line' for elements of variable
  div.df.melt$variable <-
    gsub(" ", "\n", div.df.melt$variable)

  ## Set level of factors for indices (arrangement in the plot) to appropriate order
  div.df.melt$variable<-
    factor(div.df.melt$variable, levels = c("Shannon\ndiversity", "Simpson\ndiversity",
                                                 "Pielou\nevenness", "Margalef\nrichness"))
  ## Plotting boxplots using ggplot
  {
    box<-
      ggplot2::ggplot(div.df.melt,
                      ggplot2::aes(x = factor(Location),
                                   y = value,
                                   color = variable)) +
                      ggplot2::geom_boxplot(lwd=1) +
                      ggplot2::theme_classic() +
                      ggplot2::labs(x = "Location", y = "Indices") +
                      ggplot2::scale_color_brewer(palette = "Dark2", name="Indices") +
                      ggplot2::theme(axis.text.x = ggplot2::element_text(angle=45, hjust=1, color = "black", size = 10),
                                     legend.text = ggplot2::element_text(margin = ggplot2::margin(t = 10, unit = "pt")),
                                     legend.key.size = ggplot2::unit(1.15, 'lines'))
    }

  ## Returns the plot object back to the global environment
  assign("diversityindex", box, pos = .GlobalEnv)

}
ppcadelina/bucs documentation built on April 4, 2020, 5:52 a.m.