Skip to content

Commit

Permalink
Grid Seasons
Browse files Browse the repository at this point in the history
  • Loading branch information
William Augustine McLean authored and William Augustine McLean committed Oct 11, 2024
1 parent 562e58b commit 8fcadc7
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions R/GridSeasons.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#' Grid Seasons
#'
#' This function arranges and formats seasonal plots generated by the `PlotSeason` function into a 2x2 grid layout. It uses the output from `process_model_data()` and customizes the plot titles and captions based on the data source.
#'
#' @param result A list containing seasonal data frames for each season, typically output from the `process_model_data()` function. The list should include data frames for "Winter", "Spring", "Summer", and "Fall".
#'
#' @return A `gtable` object representing a grid of seasonal plots. The plots are arranged in a 2x2 grid and include a title and a figure caption that describe the percent error analysis for the Dangermond Preserve.
#'
#' @details
#' The `GridSeasons` function performs the following operations:
#' \itemize{
#' \item **Plot Generation**: Uses the `PlotSeason` function to create plots for each season based on the input data.
#' \item **Grid Arrangement**: Arranges the plots in a 2x2 grid using `grid.arrange`.
#' \item **Caption Handling**: Dynamically assigns a caption based on the source of the data using `dplyr::case_when`.
#' \item **Title and Caption**: Adds a common title and figure caption to the grid.
#' \item **Drawing**: Draws the final grid plot using `grid.draw`.
#' }
#'@export



GridSeasons <- function(result){
# Generate plots for each season and store them in a list
plots <- lapply(names(result), function(season_name) {
PlotSeason(result[[season_name]], season_name)
})

# Convert the list of plots to grobs and arrange them in a 2x2 grid
SeasonsPlot <- do.call(grid.arrange, c(lapply(plots, ggplotGrob), ncol = 2, nrow = 2))

# Identify the source
source_value <- unique(result[[1]]$source) # Assumes all elements have the same source

# Dynamically assign a figure caption based on the input data
figurecaption1 <- dplyr::case_when(
"cabcm_v8" %in% source_value ~ "Percent error in non-coastal divides of the Dangermond Preserve. Error is \nfound by solving PPT - AET - RCH - RUN - ∆STR = ERR, averaged to a monthly basis, and recalculated \nas a percentage of PPT. Extreme values in Summer months are attributed to low rainfall.",
"terraclim" %in% source_value ~ "Percent error in non-coastal divides of the Dangermond Preserve. Error is
found by solving PPT - AET - RUN - ∆STR = ERR, averaged to a monthly basis, and recalculated
as a percentage of PPT.",
TRUE ~ "NEXTGEN CAPTION PENDING"
)

# Add a title and figure caption for CABCM Plot
SeasonsPlot <- arrangeGrob(
SeasonsPlot,
top = textGrob("Model % Error by Season", gp = gpar(fontsize = 15, fontface = "bold")),
left = textGrob("Latitude", gp = gpar(fontsize = 10), rot = 90),
bottom = textGrob("Longitude", gp = gpar(fontsize = 10), just = "center", x = 0.5)
)

SeasonsPlot <- arrangeGrob(
SeasonsPlot,
bottom = textGrob(glue("Figure 1: {figurecaption1}"), gp = gpar(fontsize = 8), just = "left", x = 0.09)
)


# Draw the combined plot
grid.newpage()
grid.draw(SeasonsPlot)

return(SeasonsPlot)
}

0 comments on commit 8fcadc7

Please sign in to comment.