R/plot_IVI.R

Defines functions plot_IVI

Documented in plot_IVI

#' Mangrove Importance Value Index visualization
#'
#' This function will output the same type of visualization "stacked horizontal barplot"
#' submitted to the report
#'
#'
#'
#'
#' @param data data Processed data frame/s obtained from \code{\link{compute_IVI}}.
#'
#'
#' @return Output is an object named 'importancevalues', a horizontal stacked bar graph
#' showing RD, RDom, and RF stacked together, with IVI printed at the end
#'
#' @keywords importance value, plot
#'
#'
#'
#' @export

# Function to plot the importance value indices visually
plot_IVI<- function(data = data){

  # Setup variables
  df = data

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

  # Check if the input for data frame(s) is more than one, then merge them
  if(is.vector(df)){

    df<- Reduce(function(x,y) merge(x = x, y = y, by = "Species"),
                 df)
  }

      ## Converts the data frame into a long format
      df<- suppressWarnings(reshape2::melt(df, id="Species"))

      ## Converts no data "-" to NA
      df$value<- gsub("-",NA,df$value)

      ## Removes NAs fromt he data frame
      df<- df[complete.cases(df$value),]

      ## Converts values row into a numeric column
      df$value<- as.numeric(df$value)

      ## Separates variable row into two: cluster elements and type of index
      df<- df %>%
        tidyr::separate(variable, c("Location", "variable"), "_")

  ## Rename the data frame
  ivi.df <- df

  ## Take away all IVI, to be added later
  ivrs<- subset(ivi.df, variable!="IVI")

  ## Subset IVs only
  ivonly<- subset(ivi.df, variable=="IVI")

  ## Transforms data frame of IV into long format
  ivonly<- plyr::ddply(ivonly,
                       plyr::.(Location, Species),
                       plyr::summarize,
                       IV = value)

  ## Merge the two data frames: RD/Dom/Fs and IVIs
  ivs<- merge(ivrs, ivonly, by=c("Location", "Species"))

  ## Calculate a ceiling value for plotting
  ceil<- ceiling(max(ivs$IV)/10)*10

  ## Plotting
  p<-

    ggplot2::ggplot(data = ivs,
                    ggplot2::aes(x = factor(Species, levels=rev(levels(factor(Species)))),
                                      y = value, fill = variable)) +
                    ggplot2::geom_bar(stat="identity", color="black") +
                    ggplot2::coord_flip() +
                    ggplot2::labs(x="Species", y="Importance Value Indices")+
                    ggplot2::geom_text(
                      ggplot2::aes(x=Species, y=IV+ 10,
                                   label=paste0(IV, "%"))) +
                    ggplot2::theme_classic() +
                    ggplot2::scale_y_continuous(limits=c(0,ceil+10),expand = c(0, 0)) +
                    ggplot2::theme(
                      axis.text.y = ggplot2::element_text(face = "italic", hjust = 1,size=10, color="black"),
                      plot.title =  ggplot2::element_text(hjust = 0.5),
                      legend.position = "bottom",
                      legend.title = ggplot2::element_blank()) +
                    ggplot2::scale_fill_brewer(palette="Set2", name="Importance \nValue \nIndices",
                                               labels=c("Relative\nDensity",
                                                        "Relative\nDominance",
                                                        "Relative\nFrequency"))

  ## IF there are more than 1 location, the graph will be split into multiple frames
  if(length(unique(ivs$Location))>1){

    p <- p +
            ggplot2::facet_wrap(~Location, scales = "free")
  }

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

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