Skip to content

Commit

Permalink
Harbour seals - explain confidence intervals, add coastwide numbers i…
Browse files Browse the repository at this point in the history
…n (waiting for low/high definition), document(), #38.
  • Loading branch information
andrew-edwards committed Oct 23, 2023
1 parent 3fea707 commit 0e795fa
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 48 deletions.
37 changes: 23 additions & 14 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -956,9 +956,15 @@
##' abundance was calculated for seven regions: the Strait of Georgia (SOG),
##' West Coast Vancouver Island (WCVI), Queen Charlotte Strait (QCS), Discovery
##' Passage (DP), Central Mainland Coast (CMC), Northern Mainland Coast (NMC),
##' and Haida Gwaii (HG); see Figure 1 of DFO (2022). The estimated abundances
##' were calculated using Generalised Additive Models, and are included here
##' (yielding the trends shown in Figure 3 of DFO, 2022). See that figure to see
##' and Haida Gwaii (HG); see Figure 1 of DFO (2022) for a map showing the
##' regions. The coastwide estimates are also given (labelled `Coastwide` so the
##' plot title is capitalised), for which the means are calculated as the sum of
##' the regions' means and the `low` and `high` values as TODO ???
##'
##'
##' The estimated abundances
##' were calculated using Generalised Additive Models (GAMs), and are included here
##' (reproducing the trends shown in Figure 3 of DFO, 2022). See that figure to see
##' the amount of data available in each region; these data can be added to
##' `pacea` if desired -- please let us know. The final year of data in each
##' region is saved in the object `harbour_seals_data_final_year`.
Expand All @@ -979,18 +985,22 @@
##' of about 250 days in the GAM, hence some years have more than one value
##' and so we give a date rather than a year}
##' \item{region:}{the region that the estimate corresponds to; one of SOG,
##' WCVI, QCS, DP, CMC, NMC, or HG}
##' WCVI, QCS, DP, CMC, NMC, HG, or Coastwide}
##' \item{low:}{low end of the estimate of abundance, defined as
##' the TODO CHECK}
##' the upper value of the 95% confidence interval (calculated on the log
##' scale) from the GAM}
##' \item{mean:}{mean estimate of abundance, numbers of seals (in that region)}
##' \item{high:}{high end of the estimate of abundance, defined TODO CHECK as
##' the TODO CHECK}
##' \item{high:}{high end of the estimate of abundance, defined as
##' the lower value of the 95% confidence interval (calculated on the log
##' scale) from the GAM}
##' }
##'
##' @examples
##' \dontrun{
##' harbour_seals
##' plot(harbour_seals) # TODO reproduce trends in Figure 3
##' plot(harbour_seals)
##' plot(harbour_seals, region = "SOG")
##' plot(harbour_seals, include_coastwide = FALSE)
##' }
##' @author Andrew Edwards
##' @source Estimates provided by Strahan Tucker, then wrangled and imported using
Expand All @@ -1015,18 +1025,17 @@
"harbour_seals_data_final_year"

#' 200m isobath for separating inshore and offshore Pacific using bathymetry
#'
#' These coordinates roughly follow the continental shelf (200m depth) to provide the inshore and offshore boundary. The line is coarse and therefore ignores canyons.
#'
#'
#' These coordinates roughly follow the continental shelf (200m depth) to provide the inshore and offshore boundary. The line is coarse and therefore ignores canyons.
#'
#' @format sf multilinestring object
#'
#' @examples
#' @examples
#' \dontrun{
#' isobath_200m
#' plot(isobath_200m)
#' plot(bccm_eez_poly, add = TRUE)
#' }
#'
#'
#' @source Generated from running `data-raw/roms/isobath_sfline.R`.
"isobath_200m"

36 changes: 21 additions & 15 deletions R/plot-pacea-harbour-seals.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
##' Plot the Pacific Harbour Seals abundance estimates
##'
##' Default is to plot all seven regions, to mostly replicate
##' (TODO CHECK) Figure 3 of DFO (2022; see `?harbour_seals`) in a `ggplot`
##' Default is to plot all seven regions plus the coastwide estimate, to mostly replicate
##' Figure 3 of DFO (2022; see `?harbour_seals`) in a `ggplot`
##' style. If a single region is
##' indicated selected then a time series plot is shown, in the same style as
##' indicated then a time series plot is shown, in the same style as
##' plots of object class `pacea_biomass` (such as `hake_biomass`).
##' Note that the values are means and standard errors (not medians and
##' Note that the values are means and confidence intervals (not medians and
##' confidence intervals like other objects); see `?harbour_seals`.
##'
##' @param obj `pacea_harbour_seals`, as this is of class `pacea_harbour_seals`
##' and the plotting is tailored for this data set.
##' @param region which region to plot for a single plot; must be one of SOG,
##' WCVI, QCS, DP, CMC, NMC, or HG. If `NULL` (the default) then do a panel
##' WCVI, QCS, DP, CMC, NMC, HG, or coastwide. If `NULL` (the default) then do a panel
##' plot showing all seven regions.
##' @param include_coastwide logical, whether to include the coastwide population in the
##' eight panel plot (keep `region = NULL`).
##' @param value the column to plot as a solid line, in this case always the
##' default `mean`
##' @param y_max maximum y value for certain types of plot (use this if you get
Expand All @@ -35,14 +37,14 @@
##' @author Andrew Edwards
##' @examples
##' \dontrun{
##' plot(hake_biomass) TODO
##' plot(hake_biomass,
##' xlim = c(lubridate::dmy(01011950),
##' lubridate::dmy(01012040))) # to expand x-axis
##' plot(harbour_seals)
##' plot(harbour_seals, region = "SOG")
##' plot(harbour_seals, include_coastwide = FALSE)
##' }
plot.pacea_harbour_seals <- function(obj,
region = NULL,
value = "mean",
include_coastwide = TRUE,
xlab = "Year",
ylab = attr(obj, "axis_name"),
y_tick_by = 1000,
Expand All @@ -64,8 +66,8 @@ plot.pacea_harbour_seals <- function(obj,
stopifnot("value must be a column of the object in the first argument" =
value %in% names(obj))

stopifnot("region must be one of SOG, WCVI, QCS, DP, CMC, NMC, HG" =
region %in% c("SOG", "WCVI", "QCS", "DP", "CMC", "NMC", "HG"))
stopifnot("region must be one of SOG, WCVI, QCS, DP, CMC, NMC, HG, Coastwide" =
region %in% c("SOG", "WCVI", "QCS", "DP", "CMC", "NMC", "HG", "Coastwide"))

if(!is.null(region)){
region_choice <- region # Else region == region in next line does not work
Expand Down Expand Up @@ -94,18 +96,22 @@ plot.pacea_harbour_seals <- function(obj,
x_tick_extra_years = x_tick_extra_years,
start_decade_ticks = start_decade_ticks)
} else {
ggplot(data = obj,
if(!include_coastwide){
obj <- dplyr::filter(obj, region != "Coastwide")
}

ggplot2::ggplot(data = obj,
aes(x = date,
y = mean)) +
geom_ribbon(aes(ymin = low,
ggplot2::geom_ribbon(aes(ymin = low,
ymax = high),
fill = uncertainty_shade_col) +
geom_line(col = uncertainty_line_col) +
ggplot2::geom_line(col = uncertainty_line_col) +
# Add back in if data get included
# geom_point(data = harbour_seals_data,
# aes(x = make_date(year),
# y = mean)) +
facet_wrap(~region,
ggplot2::facet_wrap(~region,
scales = "free_y",
ncol = 2)
}
Expand Down
19 changes: 17 additions & 2 deletions data-raw/mammals/harbour-seals/harbour-seals.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ p <- ggplot(data = harbour_seals_gam_new,
ncol = 2)
p

#Looks like matches SAR figure. Could add data, but TODO refer people to
#Looks like matches SAR figure. Could add data, but refer people to
# original SAR in the help file.
# TODO in help clarify what these are, as caption talks about se only


class(harbour_seals_gam_new) <- c("pacea_harbour_seals",
class(harbour_seals_gam_new))
Expand All @@ -98,6 +98,21 @@ harbour_seals <- harbour_seals_gam_new %>%
mean,
high) # not keeping year as duplicated for some years

# Calculate coastwide numbers and uncertainty

coastwide <- dplyr::summarise(group_by(harbour_seals, date),
low = sum(low), # TODO this and high are
# wrong, emailed Kurtis
mean = sum(mean),
high = sum(high)) %>%
dplyr::mutate(region = "Coastwide") %>%
dplyr::relocate(date, region)

stopifnot(names(harbour_seals) == names(coastwide))

harbour_seals <- rbind(harbour_seals,
coastwide)

# Also need the final date of the data, to then colour code the
# figures.

Expand Down
Binary file modified data/harbour_seals.rda
Binary file not shown.
26 changes: 18 additions & 8 deletions man/harbour_seals.Rd

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

21 changes: 12 additions & 9 deletions man/plot.pacea_harbour_seals.Rd

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

0 comments on commit 0e795fa

Please sign in to comment.