#'Plot Timing of First Observations Relative to Sunset
#'
#'\code{first_observations_plot} creates a plot showing the first observation of
#'the specified species relative to local sunset for each site withn a project.
#'
#'@family Activity Timing
#'
#'@param data_path Character. Path to an existing RData file.
#'@param species Charater. Code for a single species in the datset to use for
#' graph generation (e.g. "Epfu").
#'@param timezone Character. Specify a timezone for calculating sunset times,
#' e.g. "America/Toronto"
#'@param monitoring_start Character, the date the monitoring began (e.g.
#' "2019-01-01"), leave as NULL to use the earliest data point as the monitoring
#' start.
#'@param monitoring_end Character, the date the monitoring ceased (e.g.
#' "2019-01-01"), leave as NULL to use the latest data point as the monitoring
#' end
#'@param gaps Object: a \code{gaps_projectname} data frame generated by the
#' \code{log_file_parser} function. Defaults to \code{null}.
#'@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_label Date value: adjusts the formatting of the month labels on
#' the y-axis. See https://www.statmethods.net/input/dates.html for formatting.
#'@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.
#'
#'
#'@return A plot as an object in the current envrionment, and a saved image if
#' selected.
#'@export
first_observations_plot <- function(data_path, species, timezone,
monitoring_start = NULL,
monitoring_end = NULL,
gaps = NULL,
width = 15.9, height = 8.43,
text_size = 10,
date_label = "%b",
save_directory = NULL) {
.check_data_path(data_path)
load(data_path)
dataset <- .location_subsetter(data_path)
dataset <- dataset[which(dataset$Species==species),] #subset the species
dataset <- dplyr::select(dataset, Night, Species, Location, Latitude, Longitude, Timestamp) # trim column for sanity
if (is.null(monitoring_start)) {
monitoring_start <- .monitoring_start_finder(dataset)
}
if (is.null(monitoring_end)) {
monitoring_end <- .monitoring_end_finder(dataset)
}
#dataset$Timestamp2 <- lubridate::force_tz(dataset$Timestamp, timezone)
dataset$Time_PM <- dataset$Timestamp
dataset$Time_PM <- ifelse(lubridate::hour(dataset$Time_PM) < 12, NA, dataset$Time_PM) # Change morning observations to NA
dataset <- dataset[!is.na(dataset$Time_PM),] # Remove NA values
dataset$Time_PM <- as.POSIXct(dataset$Time_PM, origin = "1970-01-01") # Return to date/time format
#if (isTRUE(DST)) {
# dataset$Time_PM <- dataset$Time_PM - 3600 # Correct for DST
#}
names(dataset)[names(dataset) == "Night"] <- "date" # Sets a column name that suncalc will recognise
names(dataset)[names(dataset) == "Latitude"] <- "lat" # As above
names(dataset)[names(dataset) == "Longitude"] <- "lon" # As above
dataset <- aggregate(Time_PM ~ date + Location + lat + lon, dataset, function(x) min(x)) # narrow to first observations on each night
# names(species_subset)[names(species_subset) == "Location2"] <- "Location" # As above
locations <- dataset[!duplicated(dataset$Location),]
locations$Time_PM <- NULL
locations$date <- NULL
dataset <- padr::pad(dataset, by = "date", start_val = as.Date(monitoring_start),
end_val = as.Date(monitoring_end), group = "Location")
dataset <- merge(dataset, locations, by = "Location", all.x = T)
dataset$lat.x <- NULL; dataset$lon.x <- NULL
names(dataset)[names(dataset) == "lat.y"] <- "lat"
names(dataset)[names(dataset) == "lon.y"] <- "lon"
dataset$sunset <- suncalc::getSunlightTimes(data = dataset, keep = "sunset", tz = timezone) # Adds a column of sunset
dataset$sunset2 <- dataset$sunset$sunset
dataset$sunset <- NULL
names(dataset)[names(dataset) == "sunset2"] <- "sunset"
#if (isTRUE(DST)) {
# dataset$sunset <- dataset$sunset + 3600 # Correct for DST
#}
dataset$sunset_time <- format(dataset$sunset, format = "%H:%M:%S") # Remove date infromation from sunset time
dataset$sunset_time <- as.POSIXct(dataset$sunset_time, format = "%H:%M:%S") # Return sunset time to POSIXct format with date infromatino removed
dataset$ob_time <- format(dataset$Time_PM, format = "%H:%M:%S") # Remove date infromation from sunset time
dataset$ob_time <- as.POSIXct(dataset$ob_time, format = "%H:%M:%S") # Return sunset time to POSIXct format with date infromatino removed
dataset$date <- as.Date(dataset$date)
if (!is.null(gaps)) {
gaps$ymax <- max(dataset$ob_time, na.rm = T)
gaps$ymin <- min(dataset$sunset_time)
}
#assign(paste("Sunset_FO_", project, sep = ""), species_subset, envir = globalenv()) # Save as new data.frame outside the function
first_observations_plot <- ggplot2::ggplot() +
ggplot2::geom_line(data = dataset, mapping = ggplot2::aes(x = date, y = sunset_time, group = Location)) +
ggplot2::geom_point(data = dataset, mapping = ggplot2::aes(x = date, y = ob_time)) +
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::ylab("Time") +
ggplot2::facet_wrap(
~Location, ncol = 2, scales = "fixed", strip.position = "top") +#, labeller=location_labeller) +
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 (!is.null(save_directory)) {
ggplot2::ggsave(paste(save_directory, "/", species, "_first_observations_plot.png", sep = ""), width = width, height = height, units = "cm")
}
# Save plot to environment
#assign(paste(species, "_first_observation_plot_", project_name, sep = ""), species_site_aggregated_plot, envir=globalenv())
# Output plot to environment
return(first_observations_plot)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.