Skip to contents

Plot RoC scores through time with an upper uncertainty envelope and, optionally, a superimposed trend curve and peak-point markers.

Usage

plot_roc(
  data_source,
  age_threshold = NULL,
  roc_threshold = NULL,
  peaks = FALSE,
  trend = NULL,
  silent = FALSE,
  theme_default = FALSE
)

Arguments

data_source

tibble. Output of estimate_roc() or detect_peak_points().

age_threshold

numeric. Optional. Upper (oldest) age cut-off; samples older than this value are excluded from the plot.

roc_threshold

numeric. Optional. Upper RoC cut-off; values above this are clipped.

peaks

logical. If TRUE and a Peak column is present in data_source, peak points are highlighted on the plot (default = FALSE).

trend

character or NULL. When peaks = TRUE, optionally overlay the trend curve used during peak detection. One of "threshold", "trend_linear", or "trend_non_linear". NULL (default) shows no trend line.

silent

logical. If TRUE, suppress all console output (default = FALSE).

theme_default

logical. If FALSE (default), the figure uses the RRatepol theme and the colours of the RRatepol logo. If TRUE, the figure has no package styling: it uses the ggplot2 theme of the session (the ggplot2 default, or the theme set with ggplot2::theme_set()), the default colours of the ggplot2 layers with a see-through uncertainty envelope (alpha = 0.3), and no age grid lines.

Value

A ggplot2 object.

Details

If ROC contains NA in the plotted rows, those not older than age_threshold, the function warns unless silent = TRUE. The trend lines and the median line of trend = "threshold" are computed from the rows whose RoC is not NA, as in detect_peak_points(). The trend lines of "trend_linear" and "trend_non_linear" are fitted to all rows, also those older than age_threshold. If NA occurs only in those older rows, the function warns about them when it draws such a trend line, unless silent = TRUE.

By default, the colours come from the RRatepol logo: the RoC line is grey on a light-grey uncertainty envelope, peak points are red with a white outline, and the trend or threshold line is blue, which stays distinct from the red points for readers with colour-vision deficiency. With theme_default = TRUE, all layers take the default colours of ggplot2, so peak points and trend lines are no longer told apart by colour.

Examples

# \donttest{
data("example_data", package = "RRatepol")

# RoC of the first sequence with 100 runs of age uncertainty
data_roc <-
  estimate_roc(
    data_source_community = purrr::chuck(example_data, "pollen_data", 1),
    data_source_age = purrr::chuck(example_data, "sample_age", 1),
    age_uncertainty = purrr::chuck(example_data, "age_uncertainty", 1),
    smooth_method = "shep",
    dissimilarity_coefficient = "chisq",
    rand = 100,
    seed = 900723,
    silent = TRUE
  )

# RoC with its 95 % interval across runs
plot_roc(data_source = data_roc)


# Only levels younger than 6000 cal yr BP, RoC clipped at 5
plot_roc(
  data_source = data_roc,
  age_threshold = 6000,
  roc_threshold = 5
)


# Peak points with the trend used to detect them. `trend` takes
#    "threshold" (median RoC), "trend_linear", or "trend_non_linear".
data_peaks <-
  detect_peak_points(
    data_source = data_roc,
    sel_method = "trend_non_linear"
  )

plot_roc(
  data_source = data_peaks,
  peaks = TRUE,
  trend = "trend_non_linear"
)


# The same figure for each trend, with peaks detected by that method
list_plots <-
  c("threshold", "trend_linear", "trend_non_linear") %>%
  purrr::set_names() %>%
  purrr::map(
    .f = ~ data_roc %>%
      detect_peak_points(sel_method = .x) %>%
      plot_roc(
        peaks = TRUE,
        trend = .x
      )
  )

purrr::chuck(list_plots, "trend_linear")


# The result is a ggplot2 object that can be modified further
plot_roc(
  data_source = data_peaks,
  peaks = TRUE
) +
  ggplot2::labs(
    title = "Rate of change",
    y = "RoC score (chi-squared per 500 years)"
  ) +
  ggplot2::theme(
    plot.title = ggplot2::element_text(face = "bold")
  )


# Without the RRatepol styling: the ggplot2 theme of the session
plot_roc(
  data_source = data_peaks,
  peaks = TRUE,
  theme_default = TRUE
)

# }