#'Plot Species Activity Data for a Project
#'
#'\code{species_site_aggregated_plot} creates a plot of nightly activity for
#'each species in a dataset. Different sites within the dataset are aggregated.
#'Monitor activity can be plotted by including a gaps dataset (generated by
#'\code{log_file_parser}if desired.
#'
#'@section Note: Because all sites are aggregated, if gaps are plotted these
#' will be applied to each facet of the plot. Overlapping plots are indicated
#' by denser colour.
#'
#'@family Basic Plots
#'
#'@param dataset Object: a \code{species_night_site_projectname} data frame
#' generated by the \code{GUANO_loader} function.
#'@param project_name Character, a brief and relevant reference phrase that will
#' be used to name the text file. Ideally matching project name used in other
#' functions.
#'@param monitoring_start Character, the date the monitoring began (e.g.
#' "2019-01-01")
#'@param monitoring_end Character, the date the monitoring ceased (e.g.
#' "2019-01-01")
#'@param gaps Object: a \code{gaps_projectname} data frame generated by the
#' \code{log_file_parser} function. Defaults to \code{null}.
#'@param title Logical vector. Defaults \code{TRUE}, if \code{FALSE} title is
#' omitted
#'@param save.directory Character: if provided a .png image of the plot will be
#' saved in the folder specified. Defaults to \code{NULL}: no output saved.
#'@param text_size Numeric: adjusts the size of text in the figure.
#'@param date_breaks Date value: adjusts the formatting of the month labels on
#' the y-axis. See https://www.statmethods.net/input/dates.html for formatting.
#'
#'
#'@return A plot as an object in the current environment, and a saved image if
#' selected.
#'
#'@examples
#'\dontrun{
#' species_site_aggregated_plot(species_night_site_projectname, "Project Name", "2019-01-01", "2019-12-31")
#'}
#'@export
species_site_aggregated_plot <- function(dataset, project_name, monitoring_start, monitoring_end, gaps = NULL,
title = "TRUE", save_directory = NULL, text_size = 8, date_label = "%b") {
# Create labeller to provide more verbose labels on the plot facets
species.labs <- c("Big Brown Bat", "Eastern Red Bat", "Hoary Bat", "Silver-haired Bat", "Myotis Spp.",
"Tri-colored Bat", "Eastern Small-footed Myotis", "Little Brown Myotis", "Northern Myotis",
"All Myotis Combined")
names(species.labs) <- c("Epfu", "Labo", "Laci", "Lano", "Mysp", "Pesu", "Myle", "Mylu", "Myse", "Mysp_all")
# Create plot
species_site_aggregated_plot <- ggplot2::ggplot() +
ggplot2::geom_bar(data = dataset, mapping = ggplot2::aes(x = Night, y = Count), stat = "identity", fill = "black") +
ggplot2::scale_y_continuous(name = "Calls per Night", breaks = scales::pretty_breaks(n = 2)) +
ggplot2::scale_x_date(limits = c(as.Date(monitoring_start),as.Date(monitoring_end)), breaks = scales::pretty_breaks(), date_breaks = "1 month", date_labels = date_label) +
#date_breaks = "1 month", date_labels = "%b %Y") + # limits = c(as.Date("2017-05-01."),as.Date("2017-11-30")),
#ggplot2::ggtitle("Total Activity of Bats") +
ggplot2::facet_wrap(
~Species, ncol = 1, scales = "free_y", strip.position = "top",
labeller = ggplot2::labeller(Species = species.labs)) +
ggplot2::geom_hline(yintercept=0) +
ggplot2::theme_classic() +
ggplot2::theme(
plot.title = ggplot2::element_text(hjust = 0.5),
strip.background = ggplot2::element_blank(),
strip.text = ggplot2::element_text(hjust = 0),
text = ggplot2::element_text(size=text_size)
) +
if(!is.null(gaps)) {
ggplot2::geom_rect(data=gaps, ggplot2::aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, alpha=0.9),
show.legend = FALSE) # Add gaps if available
}
if(isTRUE(title)) {
species_daily_site_plot <- species_daily_site_plot + ggplot2::ggtitle("Total Activity of Bats")
}
# Save plot to a specified folder if requested
if (!is.null(save_directory)) {
ggplot2::ggsave(paste(save_directory, "/", "Aggregated_Seasonal_Activity_by_Species.png", sep = ""),
width = 25, height = 20, units = "cm")
}
# Save plot to environment
assign(paste("species_site_aggregated_plot_", project_name, sep = ""), species_site_aggregated_plot, envir=globalenv())
# Plot plot
return(species_site_aggregated_plot)
}
#'Plot Activity for a Species at Each Site Within a Project
#'
#'\code{species_daily_site_plot} creates a plot of nightly activity for a chosen
#'species at all sites within a project.
#'
#'@family Basic Plots
#'
#'@param dataset Object: a \code{species_night_site_projectname} data frame
#' genereted by the \code{GUANO_loader} function.
#'@param project_name Character, a brief and relevant reference phrase that will
#' be used to name the text file. Ideally matching project name used in other
#' functions.
#'@param species Character: the four letter species code for the species you
#' wish to plot. E.g. "Epfu".
#'@param monitoring_start Character, the date the monitoring began (e.g.
#' "2019-01-01")
#'@param monitoring_end Character, the date the monitoring ceased (e.g.
#' "2019-01-01")
#'@param gaps Object: a \code{gaps_projectname} data frame generated by the
#' \code{log_file_parser} function. Defaults to \code{null}.
#'@param survey_year Character: for projects with data from multiple years, to
#' select year
#'@param save.directory Character: if provided a .png image of the plot will be
#' saved in the folder specified. Defaults to \code{NULL}: no output saved.
#'@param title Logical vector. Defaults \code{TRUE}, if \code{FALSE} title is
#' omitted
#'@param y_scale Character. Determines whether scales on the y-axis are matched
#' or free. Defaults to "free_y" for unmatched axis, set to "fixed" for matched
#' axis.
#'@param width Number. The width in cm of the plot if saved to file. Default is
#' 25 cm.
#'@param height Number. The height of the plot if saved to file. Default is 20
#' cm.
#'@param text_size Numeric: adjusts the size of text in the figure.
#'@param date_breaks Date value: adjusts the formatting of the month labels on
#' the y-axis. See https://www.statmethods.net/input/dates.html for formatting.
#'
#'@return A plot as an object in the current environment, and a saved image if
#' selected.
#'
#'@examples
#'\dontrun{species_daily_site_plot(species_night_site_projectname, "Project Name", "Mylu", "2019-01-01", "2019-12-31")}
#'
#'@export
old_species_daily_site_plot <- function(dataset, project_name, species, monitoring_start, monitoring_end,
gaps = NULL, survey_year = NULL, save_directory = NULL, title = TRUE,
y_scale = "free_y", width = 25, height = 20, text_size = 8, date_label = "%b") {
# Subset to selected species
species_subset <- dataset[which(dataset$Species==species),]
if (!is.null(survey_year)) {
species_subset <- species_subset[which(lubridate::year(species_subset$Night)==survey_year),]
}
# Create Plot
species_daily_site_plot <- ggplot2::ggplot() +
ggplot2::geom_bar(data = species_subset, mapping = ggplot2::aes(x = Night, y = Count), stat = "identity", fill = "black") +
ggplot2::scale_y_continuous(name = "Calls per Night") +
ggplot2::scale_x_date(limits = c(as.Date(monitoring_start),as.Date(monitoring_end)), breaks = scales::pretty_breaks(), date_breaks = "1 month", date_labels = date_label) +
ggplot2::facet_wrap(
~Location, ncol = 1, scales = y_scale, strip.position = "top") +#, labeller=location_labeller) +
ggplot2::geom_hline(yintercept=0) +
ggplot2::theme_classic() +
ggplot2::theme(
plot.title = ggplot2::element_text(hjust = 0.5),
strip.background = ggplot2::element_blank(),
strip.text = ggplot2::element_text(hjust = 0),
text = ggplot2::element_text(size=text_size)
) +
if(!is.null(gaps)) {
ggplot2::geom_rect(data=gaps, ggplot2::aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, alpha=0.9),
show.legend = FALSE)
}
if(isTRUE(title)) {
species_daily_site_plot <- species_daily_site_plot + ggplot2::ggtitle(paste("Total Activity of ", species, " by Site", sep = ""))
}
# Save plot to a specified folder if requested
if (!is.null(save_directory)) {
ggplot2::ggsave(paste(save_directory, "/", species, "_daily_site_plot_", project_name, ".png", sep = ""), width = width, height = height, units = "cm")
}
# Save plot to a specified folder if requested
if (!is.null(save_directory)) {
ggplot2::ggsave(paste(save_directory, "/", species, "_daily_site_plot_", project_name, ".png", sep = ""), width = width, height = height, units = "cm")
}
# Save plot to environment
assign(paste(species, "_daily_site_plot_", project_name, sep = ""), species_site_aggregated_plot, envir=globalenv())
# Plot plot
return(species_daily_site_plot)
}
#'Plot Activity for All Species at Each Site Within a Project
#'
#'\code{multi_species_daily_site_plot} is a wrapper to apply \code{species_daily_site_plot} creates a series plot of nightly activity for a chosen
#'species at all sites within a project.
#'
#'@family Basic Plots
#'
#'@param dataset Object: a \code{species_night_site_projectname} data frame
#' genereted by the \code{GUANO_loader} function.
#'@param project_name Character, a brief and relevant reference phrase that will
#' be used to name the text file. Ideally matching project name used in other
#' functions.
#'@param monitoring_start Character, the date the monitoring began (e.g.
#' "2019-01-01")
#'@param monitoring_end Character, the date the monitoring ceased (e.g.
#' "2019-01-01")
#'@param gaps Object: a \code{gaps_projectname} data frame generated by the
#' \code{log_file_parser} function. Defaults to \code{null}.
#'@param survey_year Character: for projects with
#'@param save.directory Character: if provided a .png image of the plot will be
#' saved in the folder specified. Defaults to \code{NULL}: no output saved.
#'@param text_size Numeric: adjusts the size of text in the figure.
#'@param date_breaks Date value: adjusts the formatting of the month labels on
#' the y-axis. See https://www.statmethods.net/input/dates.html for formatting.
#'
#'@return A plot as an object in the current environment, and a saved image if
#' selected.
#'
#'@examples
#'\dontrun{
#' species_daily_site_plot(species_night_site_projectname, "Project Name", "Mylu", "2019-01-01", "2019-12-31")
#'}
#'@export
multi_species_daily_site_plot <- function(dataset, project_name, monitoring_start, monitoring_end, gaps = NULL,
survey_year = NULL, save_directory = NULL) {
species_list <- dataset$Species # Extract species list
species_list <- c(species_list, "Epfu", "Labo", "Laci", "Lano", "Myle", "Mylu", "Myse",
"Mysp", "Pesu", "HiF", "HiFrag", "HiLo", "LoFrag", "NOISE", "Social", "NoID") # Add any missing elements
species_list <- unique(species_list) # Remove duplicate list elements
species_list <- species_list[!is.element(species_list, c("HiF","HiFrag","HiLo","LoF","noID","NoID","LoFrag",
"Social","NOISE"))] # Remove non-species list elements
for (i in species_list) {
species_daily_site_plot(dataset, i, monitoring_start, monitoring_end, gaps, survey_year, save_directory)
} # Loop to plot graph for each species
}
#'Plot of Monitoring Effort by Site
#'
#'\code{monitoring_effort_plot} creates a plot showing monitor uptime at each
#'site within the project.
#'
#'@family Basic Plots
#'
#'@param gaps Object: a \code{gaps_projectname} data frame genereted by the
#' \code{log_file_parser} function. Defaults to \code{null}.
#'@param project_name Character, a brief and relevant reference phrase that will
#' be used to name the text file. Ideally mathcing project name used in other
#' functions.
#'@param monitoring_start Character, the date the monitoring began (e.g.
#' "2019-01-01")
#'@param monitoring_end Character, the date the monitoring ceased (e.g.
#' "2019-01-01")
#'@param survey_year Character: for projects with data from multiple years, to
#' select year
#'@param save.directory Character: if provided a .png image of the plot will be
#' saved in the folder specified. Defaults to \code{NULL}: no output saved.
#'@param title Logical vector. Defaults \code{TRUE}, if \code{FALSE} title is
#' omitted
#'@param width Number. The width in cm of the plot if saved to file. Default is
#' 25 cm.
#'@param height Number. The height of the plot if saved to file. Default is 20
#' cm.
#'@param text_size Numeric: adjusts the size of text in the figure.
#'@param date_breaks Date value: adjusts the formatting of the month labels on
#' the y-axis. See https://www.statmethods.net/input/dates.html for formatting.
#'
#'@return A plot as an object in the current envrionment, and a saved image if
#' selected.
#'
#'@examples
#'\dontrun{
#' species_daily_site_plot(species_night_site_projectname, "Project Name", "Mylu", "2019-01-01", "2019-12-31")
#'}
#'@export
monitoring_effort_plot <- function(gaps, project_name, monitoring_start, monitoring_end,
survey_year = NULL, save_directory = NULL, title = TRUE,
width = 25, height = 20, text_size = 8, date_label = "%b") {
# Create Plot
monitoring_effort_plot <- ggplot2::ggplot() +
ggplot2::geom_rect(data=gaps, ggplot2::aes(xmin=xmin, xmax=xmax, ymin=0, ymax=1, alpha=0.9),
show.legend = FALSE) +
ggplot2::scale_x_date(limits = c(as.Date(monitoring_start),as.Date(monitoring_end)), breaks = scales::pretty_breaks(), date_breaks = "1 month", date_labels = date_label) +
ggplot2::facet_wrap(
~Location, ncol = 1, scales = "fixed", strip.position = "top") +
ggplot2::geom_hline(yintercept=0) +
ggplot2::theme_classic() +
ggplot2::theme(
plot.title = ggplot2::element_text(hjust = 0.5),
strip.background = ggplot2::element_blank(),
strip.text = ggplot2::element_text(hjust = 0),
axis.line.y = ggplot2::element_blank(),
axis.text.y = ggplot2::element_blank(),
axis.ticks.y = ggplot2::element_blank(),
text = ggplot2::element_text(size=text_size)
)
if(isTRUE(title)) {
monitoring_effort_plot <- monitoring_effort_plot + ggplot2::ggtitle(paste("Total Monitor Uptime by Site", sep = ""))
}
# Save plot to a specified folder if requested
#if (!is.null(save_directory)) {
# ggplot2::ggsave(paste(save_directory, "/", species, "monitoring_uptime_plot_", project_name, ".png", sep = ""), width = width, height = height, units = "cm")
#}
# Save plot to a specified folder if requested
if (!is.null(save_directory)) {
ggplot2::ggsave(paste(save_directory, "/", "monitoring_uptime_plot_", project_name, ".png", sep = ""), width = width, height = height, units = "cm")
}
# Save plot to environment
assign(paste("monitoring_uptime_plot", project_name, sep = ""), monitoring_effort_plot, envir=globalenv())
# Plot plot
return(monitoring_effort_plot)
}
#'Monthly Activity Plot
#'
#'\code{monthly_actiivty_plot} creates a plot showing average nightly activity
#'by month and species.
#'
#'@family Basic Plots
#'
#'@param gaps Object: a \code{gaps_projectname} data frame genereted by the
#' \code{log_file_parser} function. Defaults to \code{null}.
#'@param project_name Character, a brief and relevant reference phrase that will
#' be used to name the text file. Ideally mathcing project name used in other
#' functions.
#'@param monitoring_start Character, the date the monitoring began (e.g.
#' "2019-01-01")
#'@param monitoring_end Character, the date the monitoring ceased (e.g.
#' "2019-01-01")
#'@param survey_year Character: for projects with data from multiple years, to
#' select year
#'@param save.directory Character: if provided a .png image of the plot will be
#' saved in the folder specified. Defaults to \code{NULL}: no output saved.
#'@param title Logical vector. Defaults \code{TRUE}, if \code{FALSE} title is
#' omitted
#'@param width Number. The width in cm of the plot if saved to file. Default is
#' 25 cm.
#'@param height Number. The height of the plot if saved to file. Default is 20
#' cm.
#'@param text_size Numeric: adjusts the size of text in the figure.
#'@param date_breaks Date value: adjusts the formatting of the month labels on
#' the y-axis. See https://www.statmethods.net/input/dates.html for formatting.
#'
#'@return A plot as an object in the current envrionment, and a saved image if
#' selected.
#'
#'@examples
#'\dontrun{
#' species_daily_site_plot(species_night_site_projectname, "Project Name", "Mylu", "2019-01-01", "2019-12-31")
#'}
#'@export
monthly_activity_plot <- function(species_night_site, monthly_active_nights, exclude_species = NULL, species_codes = c("Epfu", "Labo", "Laci", "Lano", "Myle", "Mylu", "Myse", "Mysp", "Mysp_all", "Pesu"),
species_names = c("Big Brown Bat", "Eastern Red Bat", "Hoary Bat", "Silver-haired Bat", "Eastern Small-footed Myotis", "Little Brown Myotis", "Northern Myotis",
"Unidentified Myotis", "Combined Myotis", "Tri-colored Bat")) {
monthly_species <- reshape2::dcast(species_night_site, lubridate::year(Night) + Location + Species ~ lubridate::month(Night), value.var = "Count", fun.aggregate = sum) # Sum monthly observations by species and site
colnames(monthly_species)[4:length(monthly_species)] <- paste("Observations", colnames(monthly_species)[4:length(monthly_species)], sep = "_") # Prepend month names to differentiate once merged
names(monthly_species)[names(monthly_species) == "lubridate::year(Night)"] <- "Year" # Fix year name
colnames(monthly_active_nights)[3:(length(monthly_active_nights)-1)] <- paste("SurveyEffort", colnames(monthly_active_nights)[3:(length(monthly_active_nights)-1)], sep = "_") # Prepend month names to differentiate once merged
data <- merge(monthly_species, monthly_active_nights) # Merge monthly species and monthly active
species_start <- 4 # Set iterator based on first month in dataset
active_start <- 3 # Set iterator based on first month in dataset
for (month in month.name) {
if (any(names(data) == paste("Observations_", as.integer(factor(month, levels = month.name)), sep = ""))) {
data$mean <- data[,colnames(monthly_species)[species_start]] / data[,colnames(monthly_active_nights)[active_start]]
names(data)[names(data) == 'mean'] <- month
species_start <- species_start + 1
active_start <- active_start + 1
} # Calculate means if month exists in dataset
} # Loop to calculate means for months that exists
data <- data[, -grep("Observation", colnames(data))] # Remove observation columns
data <- data[, -grep("SurveyEffort", colnames(data))] # Remove survey effort columns
data <- data[, -grep("Total_Nights", colnames(data))] # Remove survey effort columns
if (!is.null(exclude_species)) {
data <- data[!data$Species %in% exclude_species,]
} # Exclude any rows containing species listed in the 'exclude_species' argument
data_melt <- reshape2::melt(data, id=c("Year","Location", "Species")) # Melt to narrow data format
names(data_melt)[names(data_melt) == "variable"] <- "Month" # Fix column name
names(data_melt)[names(data_melt) == "value"] <- "x" # Fix column name
spec.labs <- species_names # Create species labeller
names(spec.labs) <- species_codes # Associate species codes with labeller
ggplot2::ggplot() +
ggplot2::geom_bar(data = data_melt, mapping = ggplot2::aes(x = Month, y = x), stat = "identity") +
ggplot2::facet_wrap(~Species, ncol = 1, scales = "free", strip.position = "top", labeller = ggplot2::labeller(Species = spec.labs)) +
ggplot2::scale_y_continuous(name = "Mean Nightly Observations") +
ggplot2::theme_classic() +
ggplot2::theme(
plot.title = ggplot2::element_text(hjust = 0.5),
strip.background = ggplot2::element_blank(),
strip.text = ggplot2::element_text(hjust = 0, face = "bold"),
text = ggplot2::element_text(size=25),
axis.title.x = ggplot2::element_blank(),
axis.title.y = ggplot2::element_text(face = "bold")
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.