Skip to content

Commit

Permalink
Add indicator analysis figures
Browse files Browse the repository at this point in the history
  • Loading branch information
gmyenni committed Apr 23, 2024
1 parent 090d920 commit 27133b4
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ Suggests:
testthat
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
export("%>%")
export(check_default_data_path)
export(check_for_newer_data)
export(coastal_analysis)
export(coastal_indicator)
export(download_observations)
export(dry_down)
export(foraging_analysis)
export(foraging_indicator)
export(format_code)
export(format_todo)
export(format_value)
export(get_default_data_path)
export(initiation_analysis)
export(initiation_indicator)
export(load_datafile)
export(load_indicator_data)
Expand All @@ -21,6 +24,7 @@ export(plot_coastal)
export(plot_foraging)
export(plot_initiation)
export(plot_supercolony)
export(supercolony_analysis)
export(supercolony_indicator)
export(use_default_data_path)
export(water_report)
Expand Down
183 changes: 183 additions & 0 deletions R/indicator_analysis.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#' @name foraging_analysis
#'
#' @title Produce status figures of tactile/visual foraging indicator data
#'
#' @description Analyze a table of rolling averages for the proportion of tactile/visual foragers,
#' by year, and plot with thresholds by year
#'
#' @param minyear Earliest year to include
#' @param maxyear Most recent year to include
#' @param window number of years over which to create a rolling average
#'
#' @inheritParams load_indicator_data
#'
#' @return a data.frame
#'
#' @export
#'
foraging_analysis <- function(path = get_default_data_path(),
minyear = 1986, maxyear = as.integer(format(Sys.Date(), "%Y")),
window = 3,
download_if_missing = TRUE)
{
library(ggplot2)
foraging <- foraging_indicator(minyear = 2004) %>%
dplyr::mutate(period="historic") %>%
dplyr::mutate(period = replace(period, year > 2016, "modern"))

A <- ggplot(foraging, aes(x=proportion_mean, fill=period)) +
geom_density(adjust = 1, alpha=0.5) +
xlim(0,10) +
labs(title="Average Foraging Ratio", x="tactile/visual", y = "density") +
theme_classic() +
theme(legend.position = c(.85,.9))

B <- ggplot(foraging, aes(x=year, y=proportion_mean,
ymin=proportion_mean-proportion_sd, ymax=proportion_mean+proportion_sd)) +
geom_line(color="black", show.legend = FALSE) +
geom_ribbon(alpha=0.2, fill="grey", show.legend = FALSE) +
geom_ribbon(alpha=0.5, aes(color=NULL, fill=period), show.legend = FALSE) +
labs(x="Year", y = "tactile/visual") +
theme_classic()

cowplot::plot_grid(A, B, labels = NULL)
}

#' @name initiation_analysis
#'
#' @title Produce status figures of wood stork nesting intiation indicator data
#'
#' @description Analyze a table of rolling averages for the date of wood stork nest initiation,
#' by year
#'
#' @param minyear Earliest year to include
#' @param maxyear Most recent year to include
#' @param window number of years over which to create a rolling average
#'
#' @inheritParams load_indicator_data
#'
#' @return a data.frame
#'
#' @export
#'
initiation_analysis <- function(path = get_default_data_path(),
minyear = 1986, maxyear = as.integer(format(Sys.Date(), "%Y")),
window = 3,
download_if_missing = TRUE)
{
library(ggplot2)
initiation <- initiation_indicator(minyear = 2004) %>%
dplyr::mutate(period="historic") %>%
dplyr::mutate(period = replace(period, year > 2016, "modern"))

A <- ggplot(initiation, aes(x=date_score_mean, fill=period)) +
geom_density(adjust = 1, alpha=0.5) +
scale_x_reverse(limits=c(5,-.2), breaks=c(4,3,2,1,0),labels=c("December","January","February","March","April")) +
labs(title="Average Stork Nest Initiation Date", x="Date Score", y = "density") +
theme_classic() +
theme(legend.position=c(.9,.8))

B <- ggplot(initiation, aes(x=year, y=date_score_mean,
ymin=date_score_mean-date_score_sd, ymax=date_score_mean+date_score_sd)) +
geom_line(color="black") +
geom_ribbon(alpha=0.2, fill="grey") +
geom_ribbon(alpha=0.5, aes(color=NULL, fill=period)) +
scale_y_reverse(limits=c(4.55,0), breaks=c(4,3,2,1,0),labels=c("December","January","February","March","April")) +
labs(x="Year", y = "Date Score") +
theme_classic() +
theme(legend.position="none")

cowplot::plot_grid(A, B, labels = NULL)
}

#' @name coastal_analysis
#'
#' @title Produce status figures of wood stork nesting intiation indicator data
#'
#' @description Analyze a table of rolling averages for the proportion of coastal nesters,
#' by year, and plot with thresholds
#'
#' @param minyear Earliest year to include
#' @param maxyear Most recent year to include
#' @param window number of years over which to create a rolling average
#'
#' @inheritParams load_indicator_data
#'
#' @return a data.frame
#'
#' @export
#'
coastal_analysis <- function(path = get_default_data_path(),
minyear = 1986, maxyear = as.integer(format(Sys.Date(), "%Y")),
window = 3,
download_if_missing = TRUE)
{
library(ggplot2)
coastal <- coastal_indicator(minyear = 2004) %>%
dplyr::mutate(period="historic") %>%
dplyr::mutate(period = replace(period, year > 2016, "modern"))

A <- ggplot(coastal, aes(x=proportion_mean, fill=period)) +
geom_density(adjust = 1.5, alpha=0.5) +
xlim(0,.5) +
labs(title="Average proportion nests in coastal colonies", x="proportion coastal", y = "density") +
theme_classic() +
theme(legend.position=c(.9,.8))

B <- ggplot(coastal, aes(x=year, y=proportion_mean,
ymin=proportion_mean-proportion_sd, ymax=proportion_mean+proportion_sd)) +
geom_line(color="black") +
geom_ribbon(alpha=0.2, fill="grey") +
geom_ribbon(alpha=0.5, aes(color=NULL, fill=period)) +
labs(x="Year", y = "proportion coastal") +
theme_classic() +
theme(legend.position="none")

cowplot::plot_grid(A, B, labels = NULL)
}

#' @name supercolony_analysis
#'
#' @title Produce status figures of ibis supercolony indicator data
#'
#' @description Analyze a table of rolling averages for the interval between ibis supercolony
#' events, by year
#'
#' @param minyear Earliest year to include
#' @param maxyear Most recent year to include
#' @param window number of years over which to create a rolling average
#'
#' @inheritParams load_indicator_data
#'
#' @return a data.frame
#'
#' @export
#'
supercolony_analysis <- function(path = get_default_data_path(),
minyear = 1986, maxyear = as.integer(format(Sys.Date(), "%Y")),
window = 3,
download_if_missing = TRUE)
{
library(ggplot2)
supercolony <- supercolony_indicator(minyear = 2004) %>%
dplyr::mutate(period="historic") %>%
dplyr::mutate(period = replace(period, year > 2016, "modern"))

A <- ggplot(supercolony, aes(x=interval_mean, fill=period)) +
geom_density(adjust = 1, alpha=0.5) +
xlim(0,3) +
labs(title="Ibis supercolony mean interval", x="mean interval", y = "density") +
theme_classic() +
theme(legend.position=c(.9,.8))

B <- ggplot(supercolony, aes(x=year, y=interval_mean,
ymin=interval_mean-interval_sd, ymax=interval_mean+interval_sd)) +
geom_line(color="black") +
geom_ribbon(alpha=0.2, fill="grey") +
geom_ribbon(alpha=0.5, aes(color=NULL, fill=period)) +
labs(x="Year", y = "mean interval") +
theme_classic() +
theme(legend.position="none")

cowplot::plot_grid(A, B, labels = NULL)
}
14 changes: 13 additions & 1 deletion R/report_work.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ initiation_indicator <- function(path = get_default_data_path(),
dplyr::filter(dplyr::between(.data$year, minyear, maxyear)) %>%
dplyr::arrange(.data$year) %>%
dplyr::mutate(date_score_mean = zoo::rollapply(.data$date_score, FUN="mean",
width=window, align="right",
partial = TRUE),
date_score_sd = zoo::rollapply(.data$date_score, FUN="sd",
width=window, align="right",
partial = TRUE))
}
Expand Down Expand Up @@ -98,6 +101,9 @@ coastal_indicator <- function(path = get_default_data_path(),
dplyr::filter(dplyr::between(.data$year, minyear, maxyear)) %>%
dplyr::arrange(.data$year) %>%
dplyr::mutate(proportion_mean = zoo::rollapply(.data$proportion, FUN="mean",
width=window, align="right",
partial = TRUE),
proportion_sd = zoo::rollapply(.data$proportion, FUN="sd",
width=window, align="right",
partial = TRUE))
}
Expand Down Expand Up @@ -136,6 +142,9 @@ foraging_indicator <- function(path = get_default_data_path(),
dplyr::ungroup() %>%
dplyr::arrange(.data$year) %>%
dplyr::mutate(proportion_mean = zoo::rollapply(.data$proportion, FUN="mean",
width=window, align="right",
partial = TRUE),
proportion_sd = zoo::rollapply(.data$proportion, FUN="sd",
width=window, align="right",
partial = TRUE))
}
Expand Down Expand Up @@ -168,7 +177,10 @@ supercolony_indicator <- function(path = get_default_data_path(),
dplyr::filter(dplyr::between(.data$year, minyear, maxyear)) %>%
dplyr::mutate(interval_mean = zoo::rollapply(.data$ibis_interval, FUN="mean",
width=window, align="right",
partial = TRUE))
partial = TRUE),
interval_sd = zoo::rollapply(.data$ibis_interval, FUN="sd",
width=window, align="right",
partial = TRUE))
}

#' @name plot_foraging
Expand Down
4 changes: 1 addition & 3 deletions R/wader-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#' It contains a set of functions to download, clean, and summarize the data.
#'
#' @name wader
#' @docType package
#' @keywords package
#'
#' @importFrom lubridate "%m+%"
Expand All @@ -14,8 +13,7 @@
#' @importFrom stats median na.omit sd setNames time
#' @importFrom httr content GET stop_for_status
#'

NULL
"_PACKAGE"

## quiets concerns of R CMD check re: variables used in NSE functions
if (getRversion() >= "2.15.1") utils::globalVariables(
Expand Down
34 changes: 34 additions & 0 deletions man/coastal_analysis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions man/foraging_analysis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions man/initiation_analysis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 27133b4

Please sign in to comment.