From 5bdfe2f54b02a42e2c5c38694f89b299cd197663 Mon Sep 17 00:00:00 2001 From: Jonathan Van Elslander Date: Thu, 17 Apr 2025 15:04:11 -0700 Subject: [PATCH 1/9] add a title parameter for `scfmDiagnostics` plots --- DESCRIPTION | 2 +- NEWS.md | 1 + R/comparePredictions.R | 111 ++++++++++++++++++++++++----------------- R/plot_wrappers.R | 7 ++- 4 files changed, 72 insertions(+), 49 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index adb85ad..872f0c9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,7 +9,7 @@ URL: https://scfmutils.predictiveecology.org, https://github.com/PredictiveEcology/scfmutils, https://predictiveecology.github.io/scfmutils/ Date: 2025-03-12 -Version: 2.0.9.9003 +Version: 2.0.9.9004 Authors@R: c( person("Steve", "Cumming", email = "Steve.Cumming@sbf.ulaval.ca", role = c("aut")), diff --git a/NEWS.md b/NEWS.md index 3ceb22b..2ba8d1b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,7 @@ - streamline `prepInputsFireRegimePolys()` for BEC; - `prepInputsFireRegimePolys()` gets a new argument `subsetType` to retrieve entire polygons bordering `studyArea` or only the spatial intersection (default, preserving previous behaviour) +- add a title parameter for `scfmDiagnostics` plots # scfmutils 2.0.8 diff --git a/R/comparePredictions.R b/R/comparePredictions.R index 624cbb1..01ad6a3 100644 --- a/R/comparePredictions.R +++ b/R/comparePredictions.R @@ -21,17 +21,19 @@ utils::globalVariables(c( #' @examples #' \dontrun{ #' ## assumes user has run scfm to produce the simList `mySimOut` -#' dt <- comparePredictions_summaryDT(fireRegimePoints = mySimOut$fireRegimePoints, -#' burnSummary = mySimOut$burnSummary, -#' fireRegimePolys = mySimOut$fireRegimePolys, -#' times = times(mySimOut)) +#' dt <- comparePredictions_summaryDT( +#' fireRegimePoints = mySimOut$fireRegimePoints, +#' burnSummary = mySimOut$burnSummary, +#' fireRegimePolys = mySimOut$fireRegimePolys, +#' times = times(mySimOut) +#' ) #' #' gg_mfs <- comparePredictions_meanFireSize(dt) #' gg_fri <- comparePredictions_fireReturnInterval(dt) #' gg_ign <- comparePredictions_annualIgnitions(dt) #' gg_frp <- plot_fireRegimePolys(mySimOut$fireRegimePolys) #' -#' gridExtra::grid.arrange(fps, gg_mfs, gg_fri, gg_ign, nrow = 2, ncol = 2) +#' gridExtra::grid.arrange(fps, gg_mfs, gg_fri, gg_ign, nrow = 2, ncol = 2) #' } #' #' @author Ian Eddy @@ -43,9 +45,11 @@ utils::globalVariables(c( #' @rdname comparePredictions comparePredictions_summaryDT <- function(fireRegimePoints = NULL, burnSummary = NULL, fireRegimePolys = NULL, times = NULL) { - if (any(is.null(fireRegimePolys$pSpread), is.null(fireRegimePolys$xBar), - is.null(fireRegimePolys$burnyArea), is.null(fireRegimePoints), - is.null(burnSummary), is.null(times))) { + if (any( + is.null(fireRegimePolys$pSpread), is.null(fireRegimePolys$xBar), + is.null(fireRegimePolys$burnyArea), is.null(fireRegimePoints), + is.null(burnSummary), is.null(times) + )) { stop("fireRegimePolys is missing columns or insufficient args provided") } @@ -62,14 +66,16 @@ comparePredictions_summaryDT <- function(fireRegimePoints = NULL, burnSummary = ## median fire size is not used by scfm but is worth recording ## regimes where mean is much greater than median will be hard to recreate escaped <- fireRegimePoints[fireRegimePoints$SIZE_HA > fireRegimePoly$cellSize, ] - medianFireSize <- median(escaped$SIZE_HA) #should be no need for na.rm + medianFireSize <- median(escaped$SIZE_HA) # should be no need for na.rm pSpread <- fireRegimePoly$pSpread pIg <- fireRegimePoly$ignitionRate if (!"grp" %in% names(burnSummary)) { - stop("burnSummary data.table does not have a 'grp' column.\n", - "Are you running a recent version of scfmSpread (>= 2.0.0)?") + stop( + "burnSummary data.table does not have a 'grp' column.\n", + "Are you running a recent version of scfmSpread (>= 2.0.0)?" + ) } ## burnSummary data.table from scfmSpread is coded as follows: @@ -81,12 +87,12 @@ comparePredictions_summaryDT <- function(fireRegimePoints = NULL, burnSummary = targetIgnitions <- pIg * fireRegimePoly$burnyArea achievedIgnitions <- nrow(burnSum[grp %in% 1, ]) / simLength ## incl grp 2 double counts igns - #escapes + # escapes targetEscapes <- fireRegimePoly$pEscape * targetIgnitions achievedEscapes <- nrow(burnSum[grp %in% 1 & N > 1]) / simLength ## mean fire size: mean size of all fires ignited and escaped in SAR, regardless of where spread - burnSum1 <- burnSum[grp %in% c(1, 2), lapply(.SD, sum), by = c("igLoc", "year"), .SDcols = "areaBurned"] # nolint + burnSum1 <- burnSum[grp %in% c(1, 2), lapply(.SD, sum), by = c("igLoc", "year"), .SDcols = "areaBurned"] # nolint burnSum1 <- burnSum1[areaBurned %>>% fireRegimePoly$cellSize, ] meanFireSize <- ifelse(nrow(burnSum1) == 0, 0, mean(burnSum1$areaBurned)) @@ -96,23 +102,25 @@ comparePredictions_summaryDT <- function(fireRegimePoints = NULL, burnSummary = MAAB <- sum(burnSum2$areaBurned) / simLength achievedFRI <- simLength / (sum(0, burnSum2$areaBurned) / fireRegimePoly$burnyArea) - targetFRI <- 1 / fireRegimePoly$empiricalBurnRate - - pred <- data.frame("PolyID" = x, - "histMeanSize" = fireRegimePoly$xBar, ## predicted (empirical) mean fire size - "histMedianSize" = medianFireSize, - "modMeanSize" = meanFireSize, - "achievedFRI" = achievedFRI, - "targetFRI" = targetFRI, - "burnableArea_ha" = fireRegimePoly$burnyArea, - "targetIgnitions" = targetIgnitions, - "achievedIgnitions" = achievedIgnitions, - "targetEscapes" = targetEscapes, - "achievedEscapes" = achievedEscapes, - "pEscape" = fireRegimePoly$pEscape, ## escape prob (no. fires > cellSize / no. fires) - "p0" = fireRegimePoly$p0, ## p0 and pEscape may indicate something incorrect - "pSpread" = pSpread, ## spread probability estimated from the SCAM model - "pIgnition" = pIg) ## ignition probability of a single pixel + targetFRI <- 1 / fireRegimePoly$empiricalBurnRate + + pred <- data.frame( + "PolyID" = x, + "histMeanSize" = fireRegimePoly$xBar, ## predicted (empirical) mean fire size + "histMedianSize" = medianFireSize, + "modMeanSize" = meanFireSize, + "achievedFRI" = achievedFRI, + "targetFRI" = targetFRI, + "burnableArea_ha" = fireRegimePoly$burnyArea, + "targetIgnitions" = targetIgnitions, + "achievedIgnitions" = achievedIgnitions, + "targetEscapes" = targetEscapes, + "achievedEscapes" = achievedEscapes, + "pEscape" = fireRegimePoly$pEscape, ## escape prob (no. fires > cellSize / no. fires) + "p0" = fireRegimePoly$p0, ## p0 and pEscape may indicate something incorrect + "pSpread" = pSpread, ## spread probability estimated from the SCAM model + "pIgnition" = pIg + ) ## ignition probability of a single pixel return(pred) }) return(rbindlist(out)) @@ -120,11 +128,13 @@ comparePredictions_summaryDT <- function(fireRegimePoints = NULL, burnSummary = #' @param dt scfm summary `data.table` produced by `comparePredictions_summaryDT()` #' +#' @param title character, the plot title +#' #' @export #' @importFrom ggplot2 aes geom_abline geom_point geom_text ggplot labs #' @importFrom ggplot2 scale_x_continuous scale_y_continuous theme_bw xlab ylab #' @rdname comparePredictions -comparePredictions_meanFireSize <- function(dt) { +comparePredictions_meanFireSize <- function(dt, title) { if (any(is.null(dt))) { stop("all arguments must be provided and cannot be NULL.") } @@ -136,12 +146,13 @@ comparePredictions_meanFireSize <- function(dt) { scale_y_continuous(limits = c(0, NA)) + scale_x_continuous(limits = c(0, NA)) + geom_text(aes(label = PolyID), vjust = "inward", hjust = "inward") + - geom_abline(slope = 1) + geom_abline(slope = 1) + + ggtitle(title) } #' @export #' @rdname comparePredictions -comparePredictions_fireReturnInterval <- function(dt, times) { +comparePredictions_fireReturnInterval <- function(dt, times, title) { if (any(is.null(dt), is.null(times))) { stop("all arguments must be provided and cannot be NULL.") } @@ -155,25 +166,28 @@ comparePredictions_fireReturnInterval <- function(dt, times) { } ## TODO: remove targetFRI filter below. plot those points differently to indicate poor estimates - ggplot(dt[!is.infinite(achievedFRI) & targetFRI < c(times$end - times$start) * 4], - aes(x = targetFRI, y = achievedFRI)) + + ggplot( + dt[!is.infinite(achievedFRI) & targetFRI < c(times$end - times$start) * 4], + aes(x = targetFRI, y = achievedFRI) + ) + geom_point() + labs(y = "simulation FRI (years)", x = "estimated FRI (years)") + theme_bw() + geom_abline(slope = 1) + scale_y_continuous(limits = c(0, NA)) + scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + + ggtitle(title) } #' @export #' @rdname comparePredictions -comparePredictions_annualIgnitions <- function(dt) { +comparePredictions_annualIgnitions <- function(dt, title) { if (any(is.null(dt))) { stop("all arguments must be provided and cannot be NULL.") } - dt <- copy(dt) #avoid adding per ha cols (or add them?) + dt <- copy(dt) # avoid adding per ha cols (or add them?) dt[, targetIgnitions_Mha := targetIgnitions / burnableArea_ha * 1e6] dt[, achievedIgnitions_Mha := achievedIgnitions / burnableArea_ha * 1e6] @@ -185,17 +199,18 @@ comparePredictions_annualIgnitions <- function(dt) { geom_abline(slope = 1) + scale_y_continuous(limits = c(0, NA)) + scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + + ggtitle(title) } #' @export #' @rdname comparePredictions -comparePredictions_annualEscapes <- function(dt) { +comparePredictions_annualEscapes <- function(dt, title) { if (any(is.null(dt))) { stop("all arguments must be provided and cannot be NULL.") } - dt <- copy(dt) #avoid adding per ha cols (or add them?) + dt <- copy(dt) # avoid adding per ha cols (or add them?) dt[, targetEscapes_Mha := targetEscapes / burnableArea_ha * 1e6] dt[, achievedEscapes_Mha := achievedEscapes / burnableArea_ha * 1e6] @@ -207,19 +222,22 @@ comparePredictions_annualEscapes <- function(dt) { geom_abline(slope = 1) + scale_y_continuous(limits = c(0, NA)) + scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + + ggtitle(title) } #' @param size minimum fire size (ha) #' +#' @param title character, the plot title +#' #' @export #' @importFrom data.table as.data.table setnames #' @importFrom fpCompare %>>% #' @importFrom ggplot2 facet_wrap geom_histogram #' @rdname comparePredictions -comparePredictions_fireDistribution <- function(fireRegimePoints = NULL, burnSummary = NULL, size) { +comparePredictions_fireDistribution <- function(fireRegimePoints = NULL, burnSummary = NULL, size, title) { histDistribution <- fireRegimePoints[fireRegimePoints$SIZE_HA %>>% size, ] - if (nrow(histDistribution) < 1){ + if (nrow(histDistribution) < 1) { warning("no historical fires > escape size - showing all fires instead") histDistribution <- fireRegimePoints } @@ -235,11 +253,12 @@ comparePredictions_fireDistribution <- function(fireRegimePoints = NULL, burnSum simDistribution[, source := "simulated"] allFires <- rbind(simDistribution, histDistribution, fill = TRUE) - allFires[ , PolyID := as.factor(PolyID)] + allFires[, PolyID := as.factor(PolyID)] ggplot(allFires, aes(x = log(areaBurned), fill = PolyID)) + geom_histogram() + xlab("log of escaped fire size (ha)") + theme_bw() + - facet_wrap(~source, scales = "free_y", nrow = 2) + facet_wrap(~source, scales = "free_y", nrow = 2) + + ggtitle(title) } diff --git a/R/plot_wrappers.R b/R/plot_wrappers.R index 6372b2f..19b2e96 100644 --- a/R/plot_wrappers.R +++ b/R/plot_wrappers.R @@ -4,13 +4,15 @@ utils::globalVariables(c( #' Plot fire regime polygons #' -#' @template fireRegimePolys +#' @template fireRegimePolys#' +#' +#' @param title character, the plot title #' #' @returns a `ggplot` object #' #' @export #' @importFrom ggplot2 aes geom_sf ggplot scale_fill_discrete theme_minimal -plot_fireRegimePolys <- function(fireRegimePolys) { +plot_fireRegimePolys <- function(fireRegimePolys, title) { if (!is.factor(fireRegimePolys$PolyID)) { fireRegimePolys$PolyID <- as.factor(fireRegimePolys$PolyID) } @@ -18,6 +20,7 @@ plot_fireRegimePolys <- function(fireRegimePolys) { ggplot(fireRegimePolys) + geom_sf(aes(fill = PolyID)) + scale_fill_discrete() + ## TODO: use same palette as plot_fireRegimeRas ?? + ggtitle(title) + theme_bw() } From cf0227273beb8d5180830becdbc6f5b6a889c611 Mon Sep 17 00:00:00 2001 From: Jonathan Van Elslander Date: Thu, 17 Apr 2025 15:26:27 -0700 Subject: [PATCH 2/9] ran document and rcmdcheck --- R/plot_wrappers.R | 2 +- man/comparePredictions.Rd | 25 +++++++++++++++---------- man/plot_fireRegimePolys.Rd | 4 +++- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/R/plot_wrappers.R b/R/plot_wrappers.R index 19b2e96..902e540 100644 --- a/R/plot_wrappers.R +++ b/R/plot_wrappers.R @@ -4,7 +4,7 @@ utils::globalVariables(c( #' Plot fire regime polygons #' -#' @template fireRegimePolys#' +#' @template fireRegimePolys #' #' @param title character, the plot title #' diff --git a/man/comparePredictions.Rd b/man/comparePredictions.Rd index 784f8fc..39a38a6 100644 --- a/man/comparePredictions.Rd +++ b/man/comparePredictions.Rd @@ -16,18 +16,19 @@ comparePredictions_summaryDT( times = NULL ) -comparePredictions_meanFireSize(dt) +comparePredictions_meanFireSize(dt, title) -comparePredictions_fireReturnInterval(dt, times) +comparePredictions_fireReturnInterval(dt, times, title) -comparePredictions_annualIgnitions(dt) +comparePredictions_annualIgnitions(dt, title) -comparePredictions_annualEscapes(dt) +comparePredictions_annualEscapes(dt, title) comparePredictions_fireDistribution( fireRegimePoints = NULL, burnSummary = NULL, - size + size, + title ) } \arguments{ @@ -41,6 +42,8 @@ comparePredictions_fireDistribution( \item{dt}{scfm summary \code{data.table} produced by \code{comparePredictions_summaryDT()}} +\item{title}{character, the plot title} + \item{size}{minimum fire size (ha)} } \value{ @@ -53,17 +56,19 @@ Create \code{data.table} to compare scfm predictions with historical observation \examples{ \dontrun{ ## assumes user has run scfm to produce the simList `mySimOut` -dt <- comparePredictions_summaryDT(fireRegimePoints = mySimOut$fireRegimePoints, - burnSummary = mySimOut$burnSummary, - fireRegimePolys = mySimOut$fireRegimePolys, - times = times(mySimOut)) +dt <- comparePredictions_summaryDT( + fireRegimePoints = mySimOut$fireRegimePoints, + burnSummary = mySimOut$burnSummary, + fireRegimePolys = mySimOut$fireRegimePolys, + times = times(mySimOut) +) gg_mfs <- comparePredictions_meanFireSize(dt) gg_fri <- comparePredictions_fireReturnInterval(dt) gg_ign <- comparePredictions_annualIgnitions(dt) gg_frp <- plot_fireRegimePolys(mySimOut$fireRegimePolys) -gridExtra::grid.arrange(fps, gg_mfs, gg_fri, gg_ign, nrow = 2, ncol = 2) +gridExtra::grid.arrange(fps, gg_mfs, gg_fri, gg_ign, nrow = 2, ncol = 2) } } diff --git a/man/plot_fireRegimePolys.Rd b/man/plot_fireRegimePolys.Rd index 1e53826..cee5fe8 100644 --- a/man/plot_fireRegimePolys.Rd +++ b/man/plot_fireRegimePolys.Rd @@ -4,10 +4,12 @@ \alias{plot_fireRegimePolys} \title{Plot fire regime polygons} \usage{ -plot_fireRegimePolys(fireRegimePolys) +plot_fireRegimePolys(fireRegimePolys, title) } \arguments{ \item{fireRegimePolys}{\code{sf} polygon or multipolygon object defining the fire regime polygons} + +\item{title}{character, the plot title} } \value{ a \code{ggplot} object From a68e768ffad4bfd2e74c867ff664e87721f4f5bb Mon Sep 17 00:00:00 2001 From: Jonathan Van Elslander Date: Tue, 29 Apr 2025 11:40:17 -0700 Subject: [PATCH 3/9] added ifElse to allow plots to run if runName not provided --- R/comparePredictions.R | 150 +++++++++++++++++++++++++++-------------- R/plot_wrappers.R | 19 ++++-- 2 files changed, 114 insertions(+), 55 deletions(-) diff --git a/R/comparePredictions.R b/R/comparePredictions.R index 01ad6a3..2c753ef 100644 --- a/R/comparePredictions.R +++ b/R/comparePredictions.R @@ -139,22 +139,33 @@ comparePredictions_meanFireSize <- function(dt, title) { stop("all arguments must be provided and cannot be NULL.") } - ggplot(dt, aes(x = histMeanSize, y = modMeanSize)) + - geom_point(aes(histMeanSize, modMeanSize)) + - labs(x = "historical mean fire size (ha)", y = "modeled mean fire size (ha)") + - theme_bw() + - scale_y_continuous(limits = c(0, NA)) + - scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID), vjust = "inward", hjust = "inward") + - geom_abline(slope = 1) + - ggtitle(title) + if (is.null(title)) { + ggplot(dt, aes(x = histMeanSize, y = modMeanSize)) + + geom_point(aes(histMeanSize, modMeanSize)) + + labs(x = "historical mean fire size (ha)", y = "modeled mean fire size (ha)") + + theme_bw() + + scale_y_continuous(limits = c(0, NA)) + + scale_x_continuous(limits = c(0, NA)) + + geom_text(aes(label = PolyID), vjust = "inward", hjust = "inward") + + geom_abline(slope = 1) + } else { + ggplot(dt, aes(x = histMeanSize, y = modMeanSize)) + + geom_point(aes(histMeanSize, modMeanSize)) + + labs(x = "historical mean fire size (ha)", y = "modeled mean fire size (ha)") + + theme_bw() + + scale_y_continuous(limits = c(0, NA)) + + scale_x_continuous(limits = c(0, NA)) + + geom_text(aes(label = PolyID), vjust = "inward", hjust = "inward") + + geom_abline(slope = 1) + + ggtitle(title) + } } #' @export #' @rdname comparePredictions comparePredictions_fireReturnInterval <- function(dt, times, title) { if (any(is.null(dt), is.null(times))) { - stop("all arguments must be provided and cannot be NULL.") + stop("dt and times must be provided and cannot be NULL.") } ## remove the infinite FRI caused by no simulated fires @@ -164,20 +175,33 @@ comparePredictions_fireReturnInterval <- function(dt, times, title) { ## TODO: confirm wording of this, with comment above warning("achievedFRI may be off where targetFRI is less than 4x the simulated time.") } - ## TODO: remove targetFRI filter below. plot those points differently to indicate poor estimates - ggplot( - dt[!is.infinite(achievedFRI) & targetFRI < c(times$end - times$start) * 4], - aes(x = targetFRI, y = achievedFRI) - ) + - geom_point() + - labs(y = "simulation FRI (years)", x = "estimated FRI (years)") + - theme_bw() + - geom_abline(slope = 1) + - scale_y_continuous(limits = c(0, NA)) + - scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + - ggtitle(title) + if (is.null(title)) { + ggplot( + dt[!is.infinite(achievedFRI) & targetFRI < c(times$end - times$start) * 4], + aes(x = targetFRI, y = achievedFRI) + ) + + geom_point() + + labs(y = "simulation FRI (years)", x = "estimated FRI (years)") + + theme_bw() + + geom_abline(slope = 1) + + scale_y_continuous(limits = c(0, NA)) + + scale_x_continuous(limits = c(0, NA)) + + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + } else { + ggplot( + dt[!is.infinite(achievedFRI) & targetFRI < c(times$end - times$start) * 4], + aes(x = targetFRI, y = achievedFRI) + ) + + geom_point() + + labs(y = "simulation FRI (years)", x = "estimated FRI (years)") + + theme_bw() + + geom_abline(slope = 1) + + scale_y_continuous(limits = c(0, NA)) + + scale_x_continuous(limits = c(0, NA)) + + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + + ggtitle(title) + } } #' @export @@ -191,16 +215,26 @@ comparePredictions_annualIgnitions <- function(dt, title) { dt[, targetIgnitions_Mha := targetIgnitions / burnableArea_ha * 1e6] dt[, achievedIgnitions_Mha := achievedIgnitions / burnableArea_ha * 1e6] - - ggplot(dt, aes(x = targetIgnitions_Mha, y = achievedIgnitions_Mha)) + - geom_point() + - labs(y = "simulation annual ignitions (per Mha)", x = "estimated annual ignitions (per Mha)") + - theme_bw() + - geom_abline(slope = 1) + - scale_y_continuous(limits = c(0, NA)) + - scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + - ggtitle(title) + if (is.null(title)) { + ggplot(dt, aes(x = targetIgnitions_Mha, y = achievedIgnitions_Mha)) + + geom_point() + + labs(y = "simulation annual ignitions (per Mha)", x = "estimated annual ignitions (per Mha)") + + theme_bw() + + geom_abline(slope = 1) + + scale_y_continuous(limits = c(0, NA)) + + scale_x_continuous(limits = c(0, NA)) + + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + } else { + ggplot(dt, aes(x = targetIgnitions_Mha, y = achievedIgnitions_Mha)) + + geom_point() + + labs(y = "simulation annual ignitions (per Mha)", x = "estimated annual ignitions (per Mha)") + + theme_bw() + + geom_abline(slope = 1) + + scale_y_continuous(limits = c(0, NA)) + + scale_x_continuous(limits = c(0, NA)) + + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + + ggtitle(title) + } } #' @export @@ -214,16 +248,26 @@ comparePredictions_annualEscapes <- function(dt, title) { dt[, targetEscapes_Mha := targetEscapes / burnableArea_ha * 1e6] dt[, achievedEscapes_Mha := achievedEscapes / burnableArea_ha * 1e6] - - ggplot(dt, aes(x = targetEscapes_Mha, y = achievedEscapes_Mha)) + - geom_point() + - labs(y = "simulation annual escapes (per Mha)", x = "estimated annual escapes (per Mha)") + - theme_bw() + - geom_abline(slope = 1) + - scale_y_continuous(limits = c(0, NA)) + - scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + - ggtitle(title) + if (is.null(title)) { + ggplot(dt, aes(x = targetEscapes_Mha, y = achievedEscapes_Mha)) + + geom_point() + + labs(y = "simulation annual escapes (per Mha)", x = "estimated annual escapes (per Mha)") + + theme_bw() + + geom_abline(slope = 1) + + scale_y_continuous(limits = c(0, NA)) + + scale_x_continuous(limits = c(0, NA)) + + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + } else { + ggplot(dt, aes(x = targetEscapes_Mha, y = achievedEscapes_Mha)) + + geom_point() + + labs(y = "simulation annual escapes (per Mha)", x = "estimated annual escapes (per Mha)") + + theme_bw() + + geom_abline(slope = 1) + + scale_y_continuous(limits = c(0, NA)) + + scale_x_continuous(limits = c(0, NA)) + + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + + ggtitle(title) + } } #' @param size minimum fire size (ha) @@ -255,10 +299,18 @@ comparePredictions_fireDistribution <- function(fireRegimePoints = NULL, burnSum allFires <- rbind(simDistribution, histDistribution, fill = TRUE) allFires[, PolyID := as.factor(PolyID)] - ggplot(allFires, aes(x = log(areaBurned), fill = PolyID)) + - geom_histogram() + - xlab("log of escaped fire size (ha)") + - theme_bw() + - facet_wrap(~source, scales = "free_y", nrow = 2) + - ggtitle(title) + if (is.null(title)) { + ggplot(allFires, aes(x = log(areaBurned), fill = PolyID)) + + geom_histogram() + + xlab("log of escaped fire size (ha)") + + theme_bw() + + facet_wrap(~source, scales = "free_y", nrow = 2) + } else { + ggplot(allFires, aes(x = log(areaBurned), fill = PolyID)) + + geom_histogram() + + xlab("log of escaped fire size (ha)") + + theme_bw() + + facet_wrap(~source, scales = "free_y", nrow = 2) + + ggtitle(title) + } } diff --git a/R/plot_wrappers.R b/R/plot_wrappers.R index 902e540..2e6c5d8 100644 --- a/R/plot_wrappers.R +++ b/R/plot_wrappers.R @@ -16,12 +16,19 @@ plot_fireRegimePolys <- function(fireRegimePolys, title) { if (!is.factor(fireRegimePolys$PolyID)) { fireRegimePolys$PolyID <- as.factor(fireRegimePolys$PolyID) } - - ggplot(fireRegimePolys) + - geom_sf(aes(fill = PolyID)) + - scale_fill_discrete() + ## TODO: use same palette as plot_fireRegimeRas ?? - ggtitle(title) + - theme_bw() + if (is.null(title)) { + ggplot(fireRegimePolys) + + geom_sf(aes(fill = PolyID)) + + scale_fill_discrete() + ## TODO: use same palette as plot_fireRegimeRas ?? + theme_bw() + } + else { + ggplot(fireRegimePolys) + + geom_sf(aes(fill = PolyID)) + + scale_fill_discrete() + ## TODO: use same palette as plot_fireRegimeRas ?? + ggtitle(title) + + theme_bw() + } } #' Plot fire regime raster From 73ecf75034bb80d1c20bab747fb4bb9d2c52c97d Mon Sep 17 00:00:00 2001 From: Jonathan Van Elslander Date: Tue, 29 Apr 2025 14:40:01 -0700 Subject: [PATCH 4/9] changed diagnostic plot titles default to NULL --- R/comparePredictions.R | 10 +++++----- R/plot_wrappers.R | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/comparePredictions.R b/R/comparePredictions.R index 2c753ef..fed71e7 100644 --- a/R/comparePredictions.R +++ b/R/comparePredictions.R @@ -134,7 +134,7 @@ comparePredictions_summaryDT <- function(fireRegimePoints = NULL, burnSummary = #' @importFrom ggplot2 aes geom_abline geom_point geom_text ggplot labs #' @importFrom ggplot2 scale_x_continuous scale_y_continuous theme_bw xlab ylab #' @rdname comparePredictions -comparePredictions_meanFireSize <- function(dt, title) { +comparePredictions_meanFireSize <- function(dt, title = NULL) { if (any(is.null(dt))) { stop("all arguments must be provided and cannot be NULL.") } @@ -163,7 +163,7 @@ comparePredictions_meanFireSize <- function(dt, title) { #' @export #' @rdname comparePredictions -comparePredictions_fireReturnInterval <- function(dt, times, title) { +comparePredictions_fireReturnInterval <- function(dt, times, title = NULL) { if (any(is.null(dt), is.null(times))) { stop("dt and times must be provided and cannot be NULL.") } @@ -206,7 +206,7 @@ comparePredictions_fireReturnInterval <- function(dt, times, title) { #' @export #' @rdname comparePredictions -comparePredictions_annualIgnitions <- function(dt, title) { +comparePredictions_annualIgnitions <- function(dt, title = NULL) { if (any(is.null(dt))) { stop("all arguments must be provided and cannot be NULL.") } @@ -239,7 +239,7 @@ comparePredictions_annualIgnitions <- function(dt, title) { #' @export #' @rdname comparePredictions -comparePredictions_annualEscapes <- function(dt, title) { +comparePredictions_annualEscapes <- function(dt, title = NULL) { if (any(is.null(dt))) { stop("all arguments must be provided and cannot be NULL.") } @@ -279,7 +279,7 @@ comparePredictions_annualEscapes <- function(dt, title) { #' @importFrom fpCompare %>>% #' @importFrom ggplot2 facet_wrap geom_histogram #' @rdname comparePredictions -comparePredictions_fireDistribution <- function(fireRegimePoints = NULL, burnSummary = NULL, size, title) { +comparePredictions_fireDistribution <- function(fireRegimePoints = NULL, burnSummary = NULL, size, title = NULL) { histDistribution <- fireRegimePoints[fireRegimePoints$SIZE_HA %>>% size, ] if (nrow(histDistribution) < 1) { warning("no historical fires > escape size - showing all fires instead") diff --git a/R/plot_wrappers.R b/R/plot_wrappers.R index 2e6c5d8..d34e42e 100644 --- a/R/plot_wrappers.R +++ b/R/plot_wrappers.R @@ -12,7 +12,7 @@ utils::globalVariables(c( #' #' @export #' @importFrom ggplot2 aes geom_sf ggplot scale_fill_discrete theme_minimal -plot_fireRegimePolys <- function(fireRegimePolys, title) { +plot_fireRegimePolys <- function(fireRegimePolys, title = NULL) { if (!is.factor(fireRegimePolys$PolyID)) { fireRegimePolys$PolyID <- as.factor(fireRegimePolys$PolyID) } From 3f1c08dd3df7e1e280891484f6966e41b87ec334 Mon Sep 17 00:00:00 2001 From: Jonathan Van Elslander Date: Fri, 30 May 2025 14:52:45 -0700 Subject: [PATCH 5/9] Updates coding for plot titles for concision --- R/comparePredictions.R | 146 ++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 96 deletions(-) diff --git a/R/comparePredictions.R b/R/comparePredictions.R index fed71e7..6b8e985 100644 --- a/R/comparePredictions.R +++ b/R/comparePredictions.R @@ -138,26 +138,16 @@ comparePredictions_meanFireSize <- function(dt, title = NULL) { if (any(is.null(dt))) { stop("all arguments must be provided and cannot be NULL.") } - - if (is.null(title)) { - ggplot(dt, aes(x = histMeanSize, y = modMeanSize)) + - geom_point(aes(histMeanSize, modMeanSize)) + - labs(x = "historical mean fire size (ha)", y = "modeled mean fire size (ha)") + - theme_bw() + - scale_y_continuous(limits = c(0, NA)) + - scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID), vjust = "inward", hjust = "inward") + - geom_abline(slope = 1) - } else { - ggplot(dt, aes(x = histMeanSize, y = modMeanSize)) + - geom_point(aes(histMeanSize, modMeanSize)) + - labs(x = "historical mean fire size (ha)", y = "modeled mean fire size (ha)") + - theme_bw() + - scale_y_continuous(limits = c(0, NA)) + - scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID), vjust = "inward", hjust = "inward") + - geom_abline(slope = 1) + - ggtitle(title) + gg_mfs <- ggplot(dt, aes(x = histMeanSize, y = modMeanSize)) + + geom_point(aes(histMeanSize, modMeanSize)) + + labs(x = "historical mean fire size (ha)", y = "modeled mean fire size (ha)") + + theme_bw() + + scale_y_continuous(limits = c(0, NA)) + + scale_x_continuous(limits = c(0, NA)) + + geom_text(aes(label = PolyID), vjust = "inward", hjust = "inward") + + geom_abline(slope = 1) + if (!is.null(title)) { + gg_mfs + ggtitle(title) } } @@ -176,31 +166,19 @@ comparePredictions_fireReturnInterval <- function(dt, times, title = NULL) { warning("achievedFRI may be off where targetFRI is less than 4x the simulated time.") } ## TODO: remove targetFRI filter below. plot those points differently to indicate poor estimates - if (is.null(title)) { - ggplot( - dt[!is.infinite(achievedFRI) & targetFRI < c(times$end - times$start) * 4], - aes(x = targetFRI, y = achievedFRI) - ) + - geom_point() + - labs(y = "simulation FRI (years)", x = "estimated FRI (years)") + - theme_bw() + - geom_abline(slope = 1) + - scale_y_continuous(limits = c(0, NA)) + - scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) - } else { - ggplot( - dt[!is.infinite(achievedFRI) & targetFRI < c(times$end - times$start) * 4], - aes(x = targetFRI, y = achievedFRI) - ) + - geom_point() + - labs(y = "simulation FRI (years)", x = "estimated FRI (years)") + - theme_bw() + - geom_abline(slope = 1) + - scale_y_continuous(limits = c(0, NA)) + - scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + - ggtitle(title) + gg_fri <- ggplot( + dt[!is.infinite(achievedFRI) & targetFRI < c(times$end - times$start) * 4], + aes(x = targetFRI, y = achievedFRI) + ) + + geom_point() + + labs(y = "simulation FRI (years)", x = "estimated FRI (years)") + + theme_bw() + + geom_abline(slope = 1) + + scale_y_continuous(limits = c(0, NA)) + + scale_x_continuous(limits = c(0, NA)) + + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + if (!is.null(title)) { + gg_fri + ggtitle(title) } } @@ -215,25 +193,16 @@ comparePredictions_annualIgnitions <- function(dt, title = NULL) { dt[, targetIgnitions_Mha := targetIgnitions / burnableArea_ha * 1e6] dt[, achievedIgnitions_Mha := achievedIgnitions / burnableArea_ha * 1e6] - if (is.null(title)) { - ggplot(dt, aes(x = targetIgnitions_Mha, y = achievedIgnitions_Mha)) + - geom_point() + - labs(y = "simulation annual ignitions (per Mha)", x = "estimated annual ignitions (per Mha)") + - theme_bw() + - geom_abline(slope = 1) + - scale_y_continuous(limits = c(0, NA)) + - scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) - } else { - ggplot(dt, aes(x = targetIgnitions_Mha, y = achievedIgnitions_Mha)) + - geom_point() + - labs(y = "simulation annual ignitions (per Mha)", x = "estimated annual ignitions (per Mha)") + - theme_bw() + - geom_abline(slope = 1) + - scale_y_continuous(limits = c(0, NA)) + - scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + - ggtitle(title) + gg_ai <- ggplot(dt, aes(x = targetIgnitions_Mha, y = achievedIgnitions_Mha)) + + geom_point() + + labs(y = "simulation annual ignitions (per Mha)", x = "estimated annual ignitions (per Mha)") + + theme_bw() + + geom_abline(slope = 1) + + scale_y_continuous(limits = c(0, NA)) + + scale_x_continuous(limits = c(0, NA)) + + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + if (!is.null(title)) { + gg_ai + ggtitle(title) } } @@ -248,25 +217,16 @@ comparePredictions_annualEscapes <- function(dt, title = NULL) { dt[, targetEscapes_Mha := targetEscapes / burnableArea_ha * 1e6] dt[, achievedEscapes_Mha := achievedEscapes / burnableArea_ha * 1e6] - if (is.null(title)) { - ggplot(dt, aes(x = targetEscapes_Mha, y = achievedEscapes_Mha)) + - geom_point() + - labs(y = "simulation annual escapes (per Mha)", x = "estimated annual escapes (per Mha)") + - theme_bw() + - geom_abline(slope = 1) + - scale_y_continuous(limits = c(0, NA)) + - scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) - } else { - ggplot(dt, aes(x = targetEscapes_Mha, y = achievedEscapes_Mha)) + - geom_point() + - labs(y = "simulation annual escapes (per Mha)", x = "estimated annual escapes (per Mha)") + - theme_bw() + - geom_abline(slope = 1) + - scale_y_continuous(limits = c(0, NA)) + - scale_x_continuous(limits = c(0, NA)) + - geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + - ggtitle(title) + gg_ae <- ggplot(dt, aes(x = targetEscapes_Mha, y = achievedEscapes_Mha)) + + geom_point() + + labs(y = "simulation annual escapes (per Mha)", x = "estimated annual escapes (per Mha)") + + theme_bw() + + geom_abline(slope = 1) + + scale_y_continuous(limits = c(0, NA)) + + scale_x_continuous(limits = c(0, NA)) + + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) + if (!is.null(title)) { + gg_ae + ggtitle(title) } } @@ -299,18 +259,12 @@ comparePredictions_fireDistribution <- function(fireRegimePoints = NULL, burnSum allFires <- rbind(simDistribution, histDistribution, fill = TRUE) allFires[, PolyID := as.factor(PolyID)] - if (is.null(title)) { - ggplot(allFires, aes(x = log(areaBurned), fill = PolyID)) + - geom_histogram() + - xlab("log of escaped fire size (ha)") + - theme_bw() + - facet_wrap(~source, scales = "free_y", nrow = 2) - } else { - ggplot(allFires, aes(x = log(areaBurned), fill = PolyID)) + - geom_histogram() + - xlab("log of escaped fire size (ha)") + - theme_bw() + - facet_wrap(~source, scales = "free_y", nrow = 2) + - ggtitle(title) + gg_fd <- ggplot(allFires, aes(x = log(areaBurned), fill = PolyID)) + + geom_histogram() + + xlab("log of escaped fire size (ha)") + + theme_bw() + + facet_wrap(~source, scales = "free_y", nrow = 2) + if (!is.null(title)) { + gg_fd + ggtitle(title) } } From 70f6c513af9a497dcbe8c02ca7282b1b2620d183 Mon Sep 17 00:00:00 2001 From: vanelslander-ecology Date: Tue, 10 Jun 2025 15:41:09 -0700 Subject: [PATCH 6/9] updating coding for plot titles for concision pt2 --- R/plot_wrappers.R | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/R/plot_wrappers.R b/R/plot_wrappers.R index d34e42e..dd0c7de 100644 --- a/R/plot_wrappers.R +++ b/R/plot_wrappers.R @@ -16,18 +16,13 @@ plot_fireRegimePolys <- function(fireRegimePolys, title = NULL) { if (!is.factor(fireRegimePolys$PolyID)) { fireRegimePolys$PolyID <- as.factor(fireRegimePolys$PolyID) } - if (is.null(title)) { - ggplot(fireRegimePolys) + + gg_frp <- ggplot(fireRegimePolys) + geom_sf(aes(fill = PolyID)) + scale_fill_discrete() + ## TODO: use same palette as plot_fireRegimeRas ?? theme_bw() - } - else { - ggplot(fireRegimePolys) + - geom_sf(aes(fill = PolyID)) + - scale_fill_discrete() + ## TODO: use same palette as plot_fireRegimeRas ?? - ggtitle(title) + - theme_bw() + + if (!is.null(title)) { + gg_frp + ggtitle(title) } } @@ -43,12 +38,15 @@ plot_fireRegimePolys <- function(fireRegimePolys, title = NULL) { #' @importFrom ggplot2 ggplot ggtitle scale_fill_brewer theme_bw #' @importFrom tidyterra geom_spatraster plot_fireRegimeRas <- function(x, title) { - ggplot() + + gg_frr <- ggplot() + geom_spatraster(data = terra::as.factor(x)) + scale_fill_brewer(palette = "Paired", type = "qual", na.value = "transparent") + - ggtitle(title) + theme_bw() -} + + if (!is.null(title)) { + gg_frr + ggtitle(title) + } + } #' Plot age map #' @@ -66,11 +64,14 @@ plot_fireRegimeRas <- function(x, title) { plot_ageMap <- function(x, title, maxAge) { x[x > maxAge] <- maxAge - ggplot() + + gg_am <- ggplot() + geom_spatraster(data = x) + scale_fill_distiller(palette = "Greens", direction = 1, na.value = "transparent") + - ggtitle(title) + theme_bw() + + if (!is.null(title)) { + gg_am + ggtitle(title) + } } #' Plot burn maps @@ -86,11 +87,15 @@ plot_ageMap <- function(x, title, maxAge) { #' @importFrom tidyterra geom_spatraster #' @importFrom viridis scale_fill_viridis plot_burnMap <- function(x, title) { - ggplot() + + gg_bm <- ggplot() + geom_spatraster(data = x) + scale_fill_viridis(na.value = "transparent") + - ggtitle(title) + theme_bw() + + if (!is.null(title)) { + gg_bm + ggtitle(title) + } + } #' Plot flammable map @@ -105,9 +110,12 @@ plot_burnMap <- function(x, title) { #' @importFrom ggplot2 ggplot ggtitle scale_fill_distiller theme_bw #' @importFrom tidyterra geom_spatraster plot_flammableMap <- function(x, title) { - ggplot() + + gg_fm <- ggplot() + geom_spatraster(data = x) + scale_fill_distiller(palette = "RdBu", na.value = "transparent") + - ggtitle(title) + theme_bw() + + if (!is.null(title)) { + gg_fm + ggtitle(title) + } } From c34851003c97f0d0c560559a876c2a77851aabbc Mon Sep 17 00:00:00 2001 From: vanelslander-ecology Date: Tue, 10 Jun 2025 15:45:38 -0700 Subject: [PATCH 7/9] update coding for plot title for concision pt 2 --- R/plot_wrappers.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/plot_wrappers.R b/R/plot_wrappers.R index dd0c7de..7144caf 100644 --- a/R/plot_wrappers.R +++ b/R/plot_wrappers.R @@ -17,9 +17,9 @@ plot_fireRegimePolys <- function(fireRegimePolys, title = NULL) { fireRegimePolys$PolyID <- as.factor(fireRegimePolys$PolyID) } gg_frp <- ggplot(fireRegimePolys) + - geom_sf(aes(fill = PolyID)) + - scale_fill_discrete() + ## TODO: use same palette as plot_fireRegimeRas ?? - theme_bw() + geom_sf(aes(fill = PolyID)) + + scale_fill_discrete() + ## TODO: use same palette as plot_fireRegimeRas ?? + theme_bw() if (!is.null(title)) { gg_frp + ggtitle(title) @@ -46,7 +46,7 @@ plot_fireRegimeRas <- function(x, title) { if (!is.null(title)) { gg_frr + ggtitle(title) } - } +} #' Plot age map #' @@ -63,7 +63,7 @@ plot_fireRegimeRas <- function(x, title) { #' @importFrom tidyterra geom_spatraster plot_ageMap <- function(x, title, maxAge) { x[x > maxAge] <- maxAge - + gg_am <- ggplot() + geom_spatraster(data = x) + scale_fill_distiller(palette = "Greens", direction = 1, na.value = "transparent") + @@ -118,4 +118,4 @@ plot_flammableMap <- function(x, title) { if (!is.null(title)) { gg_fm + ggtitle(title) } -} +} \ No newline at end of file From e8d9824c99112ac88430cfa22d91632315fa1ba9 Mon Sep 17 00:00:00 2001 From: vanelslander-ecology Date: Wed, 11 Jun 2025 10:43:59 -0700 Subject: [PATCH 8/9] Updated diagnostic plot functions so allow for runName = NULL --- R/comparePredictions.R | 15 ++++++++++----- R/plot_wrappers.R | 16 ++++++++++------ man/comparePredictions.Rd | 10 +++++----- man/plot_fireRegimePolys.Rd | 2 +- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/R/comparePredictions.R b/R/comparePredictions.R index db5d568..e3b81c9 100644 --- a/R/comparePredictions.R +++ b/R/comparePredictions.R @@ -147,8 +147,9 @@ comparePredictions_meanFireSize <- function(dt, title = NULL) { geom_text(aes(label = PolyID), vjust = "inward", hjust = "inward") + geom_abline(slope = 1) if (!is.null(title)) { - gg_mfs + ggtitle(title) + gg_mfs <- gg_mfs + ggtitle(title) } + return(gg_mfs) } #' @export @@ -178,8 +179,9 @@ comparePredictions_fireReturnInterval <- function(dt, times, title = NULL) { scale_x_continuous(limits = c(0, NA)) + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) if (!is.null(title)) { - gg_fri + ggtitle(title) + gg_fri <- gg_fri + ggtitle(title) } + return(gg_fri) } #' @export @@ -202,8 +204,9 @@ comparePredictions_annualIgnitions <- function(dt, title = NULL) { scale_x_continuous(limits = c(0, NA)) + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) if (!is.null(title)) { - gg_ai + ggtitle(title) + gg_ai <- gg_ai + ggtitle(title) } + return(gg_ai) } #' @export @@ -226,8 +229,9 @@ comparePredictions_annualEscapes <- function(dt, title = NULL) { scale_x_continuous(limits = c(0, NA)) + geom_text(aes(label = PolyID, vjust = "inward", hjust = "inward")) if (!is.null(title)) { - gg_ae + ggtitle(title) + gg_ae <- gg_ae + ggtitle(title) } + return(gg_ae) } #' @param size minimum fire size (ha) @@ -265,6 +269,7 @@ comparePredictions_fireDistribution <- function(fireRegimePoints = NULL, burnSum theme_bw() + facet_wrap(~source, scales = "free_y", nrow = 2) if (!is.null(title)) { - gg_fd + ggtitle(title) + gg_fd <- gg_fd + ggtitle(title) } + return(gg_fd) } diff --git a/R/plot_wrappers.R b/R/plot_wrappers.R index 7144caf..2fecb78 100644 --- a/R/plot_wrappers.R +++ b/R/plot_wrappers.R @@ -22,8 +22,9 @@ plot_fireRegimePolys <- function(fireRegimePolys, title = NULL) { theme_bw() if (!is.null(title)) { - gg_frp + ggtitle(title) + gg_frp <- gg_frp + ggtitle(title) } + return(gg_frp) } #' Plot fire regime raster @@ -44,8 +45,9 @@ plot_fireRegimeRas <- function(x, title) { theme_bw() if (!is.null(title)) { - gg_frr + ggtitle(title) + gg_frr <- gg_frr + ggtitle(title) } + return(gg_frr) } #' Plot age map @@ -70,8 +72,9 @@ plot_ageMap <- function(x, title, maxAge) { theme_bw() if (!is.null(title)) { - gg_am + ggtitle(title) + gg_am <- gg_am + ggtitle(title) } + return(gg_am) } #' Plot burn maps @@ -93,9 +96,9 @@ plot_burnMap <- function(x, title) { theme_bw() if (!is.null(title)) { - gg_bm + ggtitle(title) + gg_bm <- gg_bm + ggtitle(title) } - + return(gg_bm) } #' Plot flammable map @@ -116,6 +119,7 @@ plot_flammableMap <- function(x, title) { theme_bw() if (!is.null(title)) { - gg_fm + ggtitle(title) + gg_fm <- gg_fm + ggtitle(title) } + return(gg_fm) } \ No newline at end of file diff --git a/man/comparePredictions.Rd b/man/comparePredictions.Rd index 39a38a6..4dae0d1 100644 --- a/man/comparePredictions.Rd +++ b/man/comparePredictions.Rd @@ -16,19 +16,19 @@ comparePredictions_summaryDT( times = NULL ) -comparePredictions_meanFireSize(dt, title) +comparePredictions_meanFireSize(dt, title = NULL) -comparePredictions_fireReturnInterval(dt, times, title) +comparePredictions_fireReturnInterval(dt, times, title = NULL) -comparePredictions_annualIgnitions(dt, title) +comparePredictions_annualIgnitions(dt, title = NULL) -comparePredictions_annualEscapes(dt, title) +comparePredictions_annualEscapes(dt, title = NULL) comparePredictions_fireDistribution( fireRegimePoints = NULL, burnSummary = NULL, size, - title + title = NULL ) } \arguments{ diff --git a/man/plot_fireRegimePolys.Rd b/man/plot_fireRegimePolys.Rd index cee5fe8..5392428 100644 --- a/man/plot_fireRegimePolys.Rd +++ b/man/plot_fireRegimePolys.Rd @@ -4,7 +4,7 @@ \alias{plot_fireRegimePolys} \title{Plot fire regime polygons} \usage{ -plot_fireRegimePolys(fireRegimePolys, title) +plot_fireRegimePolys(fireRegimePolys, title = NULL) } \arguments{ \item{fireRegimePolys}{\code{sf} polygon or multipolygon object defining the fire regime polygons} From 567c0c1fe79d903d9670861ea6c985c7f76ef4ef Mon Sep 17 00:00:00 2001 From: Jonathan Van Elslander Date: Fri, 4 Jul 2025 12:58:07 -0700 Subject: [PATCH 9/9] added runName subtitle to burnmaps --- R/plot_wrappers.R | 21 +++++++++++---------- man/plot_burnMap.Rd | 4 +++- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/R/plot_wrappers.R b/R/plot_wrappers.R index 2fecb78..a6b77d0 100644 --- a/R/plot_wrappers.R +++ b/R/plot_wrappers.R @@ -20,7 +20,7 @@ plot_fireRegimePolys <- function(fireRegimePolys, title = NULL) { geom_sf(aes(fill = PolyID)) + scale_fill_discrete() + ## TODO: use same palette as plot_fireRegimeRas ?? theme_bw() - + if (!is.null(title)) { gg_frp <- gg_frp + ggtitle(title) } @@ -43,7 +43,7 @@ plot_fireRegimeRas <- function(x, title) { geom_spatraster(data = terra::as.factor(x)) + scale_fill_brewer(palette = "Paired", type = "qual", na.value = "transparent") + theme_bw() - + if (!is.null(title)) { gg_frr <- gg_frr + ggtitle(title) } @@ -65,12 +65,12 @@ plot_fireRegimeRas <- function(x, title) { #' @importFrom tidyterra geom_spatraster plot_ageMap <- function(x, title, maxAge) { x[x > maxAge] <- maxAge - + gg_am <- ggplot() + geom_spatraster(data = x) + scale_fill_distiller(palette = "Greens", direction = 1, na.value = "transparent") + theme_bw() - + if (!is.null(title)) { gg_am <- gg_am + ggtitle(title) } @@ -83,22 +83,23 @@ plot_ageMap <- function(x, title, maxAge) { #' #' @param title character, the plot title #' +#' @param subtitle character, the plot subtitle +#' #' @returns `ggplot` object #' #' @export #' @importFrom ggplot2 ggplot ggtitle theme_bw #' @importFrom tidyterra geom_spatraster #' @importFrom viridis scale_fill_viridis -plot_burnMap <- function(x, title) { +plot_burnMap <- function(x, title = NULL, subtitle = NULL) { gg_bm <- ggplot() + geom_spatraster(data = x) + scale_fill_viridis(na.value = "transparent") + theme_bw() - - if (!is.null(title)) { - gg_bm <- gg_bm + ggtitle(title) - } - return(gg_bm) + + gg_bm <- gg_bm + ggtitle(title) + labs(subtitle = subtitle) + + return(gg_bm) } #' Plot flammable map diff --git a/man/plot_burnMap.Rd b/man/plot_burnMap.Rd index d07f7a8..2fd1744 100644 --- a/man/plot_burnMap.Rd +++ b/man/plot_burnMap.Rd @@ -4,12 +4,14 @@ \alias{plot_burnMap} \title{Plot burn maps} \usage{ -plot_burnMap(x, title) +plot_burnMap(x, title = NULL, subtitle = NULL) } \arguments{ \item{x}{\code{SpatRaster} object corresponding to a current or cumulative burn map.} \item{title}{character, the plot title} + +\item{subtitle}{character, the plot subtitle} } \value{ \code{ggplot} object