R/mpa_plot3.R

#' Plots results from biophysical indicator functions by site
#'
#' @description Plots results from the functions used to calculate biophysical indicators like: richness(), density(), ttrophic(), fish_biomass(), and fish_size().
#'
#' @param data dataframe generated by any of the following functions: richness(), density(), trophic(), fish_biomass(), and fish_size().
#'
#' @param reserve A quoted string that indicates which site should be used as a reserve.
#'
#' @param control A quoted string that indicates which site should be used as a control zone for the indicated reserve.
#'
#' @param error.bars A logical value indicating if error bars (1 SD) should be plotted.
#'
#' @param x.lab A string to be used as x-axis label
#'
#' @export
#'
#' @author Villasenor-Derbez, J.C.

mpa_plot3 <-
  function (data,
            reserve = NULL,
            control = NULL,
            error.bars = F,
            y.lab = "Indicador") {
    library(ggplot2)
    library(dplyr)
    library(tidyr)

    if (is.null(reserve) | is.null(control)) {
      stop("You must specify reserve and control sites")
    }

    colnames(data) <- c(
      "Ano",
      "Zonificacion",
      "Sitio",
      "Transecto",
      "Indicador",
      "Temperatura",
      "Visibilidad",
      "Profundidad"
    )

    data <- data %>% filter(Sitio == reserve | Sitio == control)

    p <-
      ggplot(data, aes(
        x = Ano,
        y = Indicador,
        color = Zonificacion,
        pch = Sitio,
        fill = Zonificacion
      )) +
      geom_jitter(width = 0.1, size = 1) +
      stat_summary(geom = "point", fun.y = mean, color = "black", size = 2, alpha = 0.5) +
      stat_summary(geom = "line", fun.y = mean, color = "black", alpha = 0.5) +
      stat_summary(fun.data = mean_se, geom = "ribbon", alpha = 0.5) +
      scale_color_brewer(palette = "Set1") +
      scale_fill_brewer(palette = "Set1") +
      theme_bw() +
      labs(x = "Ano", y = y.lab)

    if (error.bars) {
      p <- p + geom_errorbar(aes(ymin = Indicator - SD, ymax = Indicator +
                                   SD), width = 0.2)
    }

    p

  }
turfeffect/MPAtools documentation built on June 1, 2019, 2:55 a.m.